@@ -60,15 +60,13 @@ use hyper::{header, Method, Request, Response, StatusCode};
60
60
use hyper_util:: rt:: TokioIo ;
61
61
pub use lazy_static:: lazy_static;
62
62
use log:: { error, warn} ;
63
- pub use prometheus:: register_int_counter;
64
- use prometheus:: Encoder ;
65
- pub use prometheus:: IntCounter ;
63
+ pub use prometheus:: * ;
66
64
use tokio:: net:: TcpListener ;
67
65
use tokio:: task:: JoinHandle ;
68
66
69
67
use crate :: error:: DshError ;
70
68
71
- type Result < T > = std:: result:: Result < T , DshError > ;
69
+ type DshResult < T > = std:: result:: Result < T , DshError > ;
72
70
type BoxBody = http_body_util:: combinators:: BoxBody < Bytes , hyper:: Error > ;
73
71
74
72
static NOTFOUND : & [ u8 ] = b"404: Not Found" ;
@@ -121,7 +119,7 @@ static NOTFOUND: &[u8] = b"404: Not Found";
121
119
/// }
122
120
/// }
123
121
/// ```
124
- pub fn start_http_server ( port : u16 ) -> JoinHandle < Result < ( ) > > {
122
+ pub fn start_http_server ( port : u16 ) -> JoinHandle < DshResult < ( ) > > {
125
123
tokio:: spawn ( async move {
126
124
let result = run_server ( port) . await ;
127
125
warn ! ( "HTTP server stopped: {:?}" , result) ;
@@ -130,15 +128,15 @@ pub fn start_http_server(port: u16) -> JoinHandle<Result<()>> {
130
128
}
131
129
132
130
/// Encode metrics to a string (UTF8)
133
- pub fn metrics_to_string ( ) -> Result < String > {
131
+ pub fn metrics_to_string ( ) -> DshResult < String > {
134
132
let encoder = prometheus:: TextEncoder :: new ( ) ;
135
133
136
134
let mut buffer = Vec :: new ( ) ;
137
135
encoder. encode ( & prometheus:: gather ( ) , & mut buffer) ?;
138
136
Ok ( String :: from_utf8 ( buffer) ?)
139
137
}
140
138
141
- async fn run_server ( port : u16 ) -> Result < ( ) > {
139
+ async fn run_server ( port : u16 ) -> DshResult < ( ) > {
142
140
let addr = SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , port) ) ;
143
141
let listener = TcpListener :: bind ( addr) . await ?;
144
142
@@ -157,21 +155,21 @@ async fn handle_connection(stream: tokio::net::TcpStream) {
157
155
}
158
156
}
159
157
160
- async fn routes ( req : Request < Incoming > ) -> Result < Response < BoxBody > > {
158
+ async fn routes ( req : Request < Incoming > ) -> DshResult < Response < BoxBody > > {
161
159
match ( req. method ( ) , req. uri ( ) . path ( ) ) {
162
160
( & Method :: GET , "/metrics" ) => get_metrics ( ) ,
163
161
( _, _) => not_found ( ) ,
164
162
}
165
163
}
166
164
167
- fn get_metrics ( ) -> Result < Response < BoxBody > > {
165
+ fn get_metrics ( ) -> DshResult < Response < BoxBody > > {
168
166
Ok ( Response :: builder ( )
169
167
. status ( StatusCode :: OK )
170
168
. header ( header:: CONTENT_TYPE , prometheus:: TEXT_FORMAT )
171
169
. body ( full ( metrics_to_string ( ) . unwrap_or_default ( ) ) ) ?)
172
170
}
173
171
174
- fn not_found ( ) -> Result < Response < BoxBody > > {
172
+ fn not_found ( ) -> DshResult < Response < BoxBody > > {
175
173
Ok ( Response :: builder ( )
176
174
. status ( StatusCode :: NOT_FOUND )
177
175
. body ( full ( NOTFOUND ) ) ?)
0 commit comments