-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Upgrade to hyper 1.0 (#1858)
## Description This upgrades our direct use of hyper to version 1.0 of the crate. ## Notes & open questions This is adds a little duplication to our deps for now since reqwest is still using hyper 1.0. ## Change checklist - [x] Self-review. - [x] Documentation updates if relevant. - [x] Tests if relevant.
- Loading branch information
Showing
9 changed files
with
348 additions
and
293 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,45 @@ | ||
use std::{future::Future, io, net::SocketAddr, pin::Pin}; | ||
use std::net::SocketAddr; | ||
|
||
use hyper::{ | ||
service::{make_service_fn, service_fn}, | ||
Body, Error, Request, Response, Server, | ||
}; | ||
|
||
use tracing::info; | ||
use anyhow::{anyhow, Result}; | ||
use hyper::service::service_fn; | ||
use hyper::{Request, Response}; | ||
use tokio::net::TcpListener; | ||
use tracing::{error, info}; | ||
|
||
use crate::core::Core; | ||
|
||
type BytesBody = http_body_util::Full<hyper::body::Bytes>; | ||
|
||
/// Start a HTTP server to report metrics. | ||
pub async fn run(metrics_addr: SocketAddr) -> Result<(), Error> { | ||
pub async fn run(metrics_addr: SocketAddr) -> Result<()> { | ||
info!("Starting metrics server on {metrics_addr}"); | ||
Server::bind(&metrics_addr) | ||
.serve(make_service_fn(move |_conn| async move { | ||
let handler = make_handler(); | ||
Ok::<_, io::Error>(service_fn(handler)) | ||
})) | ||
.await | ||
let listener = TcpListener::bind(metrics_addr).await?; | ||
loop { | ||
let (stream, _addr) = listener.accept().await?; | ||
let io = hyper_util::rt::TokioIo::new(stream); | ||
tokio::spawn(async move { | ||
if let Err(err) = hyper::server::conn::http1::Builder::new() | ||
.serve_connection(io, service_fn(handler)) | ||
.await | ||
{ | ||
error!("Error serving metrics connection: {err:#}"); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/// This function returns an HTTP handler fn that will respond with the | ||
/// OpenMetrics encoding of our metrics. | ||
fn make_handler( | ||
) -> impl Fn(Request<Body>) -> Pin<Box<dyn Future<Output = io::Result<Response<Body>>> + Send>> { | ||
// This closure accepts a request and responds with the OpenMetrics encoding of our metrics. | ||
move |_req: Request<Body>| { | ||
Box::pin(async move { | ||
let core = Core::get() | ||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "metrics disabled"))?; | ||
core.encode() | ||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)) | ||
.map(|r| { | ||
let body = Body::from(r); | ||
Response::builder() | ||
.header(hyper::header::CONTENT_TYPE, "text/plain; charset=utf-8") | ||
.body(body) | ||
.expect("Failed to build response") | ||
}) | ||
}) | ||
} | ||
/// HTTP handler that will respond with the OpenMetrics encoding of our metrics. | ||
async fn handler(_req: Request<hyper::body::Incoming>) -> Result<Response<BytesBody>> { | ||
let core = Core::get().ok_or_else(|| anyhow!("metrics disabled"))?; | ||
core.encode().map_err(anyhow::Error::new).map(|r| { | ||
Response::builder() | ||
.header(hyper::header::CONTENT_TYPE, "text/plain; charset=utf-8") | ||
.body(body_full(r)) | ||
.expect("Failed to build response") | ||
}) | ||
} | ||
|
||
/// Creates a new [`BytesBody`] with given content. | ||
fn body_full(content: impl Into<hyper::body::Bytes>) -> BytesBody { | ||
http_body_util::Full::new(content.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.