Skip to content

Commit 7cbc917

Browse files
authored
Bump the hyper crate to v1.4.1 and rework prometheus server handling (#778)
Bump hyper to v1.4.1 and rework prometheus server handling
1 parent 2c8b2f0 commit 7cbc917

File tree

3 files changed

+92
-50
lines changed

3 files changed

+92
-50
lines changed

Cargo.lock

Lines changed: 51 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ base64 = "0.21"
2929
stringprep = "0.1"
3030
tokio-rustls = "0.24"
3131
rustls-pemfile = "1"
32-
hyper = { version = "0.14", features = ["full"] }
32+
http-body-util = "0.1.2"
33+
hyper = { version = "1.4.1", features = ["full"] }
34+
hyper-util = { version = "0.1.7", features = ["tokio"] }
3335
phf = { version = "0.11.1", features = ["macros"] }
3436
exitcode = "1.1.2"
3537
futures = "0.3"

src/prometheus.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
use hyper::service::{make_service_fn, service_fn};
2-
use hyper::{Body, Method, Request, Response, Server, StatusCode};
1+
use http_body_util::Full;
2+
use hyper::body;
3+
use hyper::body::Bytes;
4+
5+
use hyper::server::conn::http1;
6+
use hyper::service::service_fn;
7+
use hyper::{Method, Request, Response, StatusCode};
8+
use hyper_util::rt::TokioIo;
39
use log::{debug, error, info};
410
use phf::phf_map;
511
use std::collections::HashMap;
612
use std::fmt;
713
use std::net::SocketAddr;
814
use std::sync::atomic::Ordering;
915
use std::sync::Arc;
16+
use tokio::net::TcpListener;
1017

1118
use crate::config::Address;
1219
use crate::pool::{get_all_pools, PoolIdentifier};
@@ -243,7 +250,9 @@ impl<Value: fmt::Display> PrometheusMetric<Value> {
243250
}
244251
}
245252

246-
async fn prometheus_stats(request: Request<Body>) -> Result<Response<Body>, hyper::http::Error> {
253+
async fn prometheus_stats(
254+
request: Request<body::Incoming>,
255+
) -> Result<Response<Full<Bytes>>, hyper::http::Error> {
247256
match (request.method(), request.uri().path()) {
248257
(&Method::GET, "/metrics") => {
249258
let mut lines = Vec::new();
@@ -374,14 +383,35 @@ fn push_server_stats(lines: &mut Vec<String>) {
374383
}
375384

376385
pub async fn start_metric_server(http_addr: SocketAddr) {
377-
let http_service_factory =
378-
make_service_fn(|_conn| async { Ok::<_, hyper::Error>(service_fn(prometheus_stats)) });
379-
let server = Server::bind(&http_addr).serve(http_service_factory);
386+
let listener = TcpListener::bind(http_addr);
387+
let listener = match listener.await {
388+
Ok(listener) => listener,
389+
Err(e) => {
390+
error!("Failed to bind prometheus server to HTTP address: {}.", e);
391+
return;
392+
}
393+
};
380394
info!(
381395
"Exposing prometheus metrics on http://{}/metrics.",
382396
http_addr
383397
);
384-
if let Err(e) = server.await {
385-
error!("Failed to run HTTP server: {}.", e);
398+
loop {
399+
let stream = match listener.accept().await {
400+
Ok((stream, _)) => stream,
401+
Err(e) => {
402+
error!("Error accepting connection: {}", e);
403+
continue;
404+
}
405+
};
406+
let io = TokioIo::new(stream);
407+
408+
tokio::task::spawn(async move {
409+
if let Err(err) = http1::Builder::new()
410+
.serve_connection(io, service_fn(prometheus_stats))
411+
.await
412+
{
413+
eprintln!("Error serving HTTP connection for metrics: {:?}", err);
414+
}
415+
});
386416
}
387417
}

0 commit comments

Comments
 (0)