@@ -2,7 +2,7 @@ use std::net::SocketAddr;
2
2
3
3
use http_body_util:: Full ;
4
4
use hyper:: {
5
- body:: { self , Bytes , Incoming } ,
5
+ body:: { Bytes , Incoming } ,
6
6
header:: { HeaderValue , CONTENT_TYPE } ,
7
7
server:: conn:: http1:: Builder as HyperHttpBuilder ,
8
8
service:: service_fn,
@@ -67,10 +67,8 @@ impl HttpListeningExporter {
67
67
fn process_tcp_stream ( & self , stream : TcpStream ) {
68
68
let is_allowed = self . check_tcp_allowed ( & stream) ;
69
69
let handle = self . handle . clone ( ) ;
70
- let service = service_fn ( move |req : Request < body:: Incoming > | {
71
- let handle = handle. clone ( ) ;
72
- async move { Ok :: < _ , hyper:: Error > ( Self :: handle_http_request ( is_allowed, & handle, & req) ) }
73
- } ) ;
70
+ let service =
71
+ service_fn ( move |req| Self :: handle_http_request ( is_allowed, handle. clone ( ) , req) ) ;
74
72
75
73
tokio:: spawn ( async move {
76
74
if let Err ( err) =
@@ -115,10 +113,7 @@ impl HttpListeningExporter {
115
113
#[ cfg( feature = "uds-listener" ) ]
116
114
fn process_uds_stream ( & self , stream : UnixStream ) {
117
115
let handle = self . handle . clone ( ) ;
118
- let service = service_fn ( move |req : Request < body:: Incoming > | {
119
- let handle = handle. clone ( ) ;
120
- async move { Ok :: < _ , hyper:: Error > ( Self :: handle_http_request ( true , & handle, & req) ) }
121
- } ) ;
116
+ let service = service_fn ( move |req| Self :: handle_http_request ( true , handle. clone ( ) , req) ) ;
122
117
123
118
tokio:: spawn ( async move {
124
119
if let Err ( err) =
@@ -129,26 +124,26 @@ impl HttpListeningExporter {
129
124
} ) ;
130
125
}
131
126
132
- fn handle_http_request (
127
+ async fn handle_http_request (
133
128
is_allowed : bool ,
134
- handle : & PrometheusHandle ,
135
- req : & Request < Incoming > ,
136
- ) -> Response < Full < Bytes > > {
129
+ handle : PrometheusHandle ,
130
+ req : Request < Incoming > ,
131
+ ) -> Result < Response < Full < Bytes > > , hyper :: Error > {
137
132
if is_allowed {
138
133
let mut response = Response :: new ( match req. uri ( ) . path ( ) {
139
134
"/health" => "OK" . into ( ) ,
140
- _ => handle. render ( ) . into ( ) ,
135
+ _ => tokio :: task :: spawn_blocking ( move || handle. render ( ) ) . await . unwrap ( ) . into ( ) ,
141
136
} ) ;
142
137
response. headers_mut ( ) . append ( CONTENT_TYPE , HeaderValue :: from_static ( "text/plain" ) ) ;
143
- response
138
+ Ok ( response)
144
139
} else {
145
140
// This unwrap should not fail because we don't use any function that
146
141
// can assign an Err to it's inner such as `Builder::header``. A unit test
147
142
// will have to suffice to detect if this fails to hold true.
148
- Response :: builder ( )
143
+ Ok ( Response :: builder ( )
149
144
. status ( StatusCode :: FORBIDDEN )
150
145
. body ( Full :: < Bytes > :: default ( ) )
151
- . unwrap ( )
146
+ . unwrap ( ) )
152
147
}
153
148
}
154
149
}
0 commit comments