Skip to content

Commit

Permalink
chore(metrics): upgrade to hyper 1.x
Browse files Browse the repository at this point in the history
a brief note; this commit happened to tickle an unfortunate sharp edge
in `BoxBody` and `Full`'s respective constructors. type inference could
not figure out how to construct the body, so we refrain from boxing the
response body now.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Jan 7, 2025
1 parent 1badee7 commit ccdc75a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2038,9 +2038,11 @@ dependencies = [
name = "linkerd-metrics"
version = "0.1.0"
dependencies = [
"bytes",
"deflate",
"http 1.2.0",
"http-body",
"http-body-util",
"hyper",
"linkerd-http-box",
"linkerd-stack",
Expand Down
2 changes: 2 additions & 0 deletions linkerd/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ stack = ["linkerd-stack"]
test_util = []

[dependencies]
bytes = { workspace = true }
deflate = { version = "1", features = ["gzip"] }
http = { workspace = true }
http-body = { workspace = true }
http-body-util = { workspace = true }
hyper = { workspace = true, features = ["http1", "http2"] }
linkerd-http-box = { path = "../http/box" }
linkerd-stack = { path = "../stack", optional = true }
Expand Down
13 changes: 9 additions & 4 deletions linkerd/metrics/src/serve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytes::Bytes;
use deflate::{write::GzEncoder, CompressionOptions};
use linkerd_http_box::BoxBody;
use std::io::Write;
use tracing::trace;

Expand Down Expand Up @@ -33,22 +33,27 @@ impl<M> Serve<M> {
}

impl<M: FmtMetrics> Serve<M> {
pub fn serve<B>(&self, req: http::Request<B>) -> std::io::Result<http::Response<BoxBody>> {
pub fn serve<B>(
&self,
req: http::Request<B>,
) -> std::io::Result<http::Response<http_body_util::Full<Bytes>>> {
if Self::is_gzip(&req) {
trace!("gzipping metrics");
let mut writer = GzEncoder::new(Vec::<u8>::new(), CompressionOptions::fast());
write!(&mut writer, "{}", self.metrics.as_display())?;
Ok(http::Response::builder()
.header(http::header::CONTENT_ENCODING, "gzip")
.header(http::header::CONTENT_TYPE, "text/plain")
.body(BoxBody::new(hyper::Body::from(writer.finish()?)))
.body(http_body_util::Full::from(
writer.finish().map(Bytes::from)?,
))
.expect("Response must be valid"))
} else {
let mut writer = Vec::<u8>::new();
write!(&mut writer, "{}", self.metrics.as_display())?;
Ok(http::Response::builder()
.header(http::header::CONTENT_TYPE, "text/plain")
.body(BoxBody::new(hyper::Body::from(writer)))
.body(http_body_util::Full::from(Bytes::from(writer)))
.expect("Response must be valid"))
}
}
Expand Down

0 comments on commit ccdc75a

Please sign in to comment.