Skip to content

Commit 92f1513

Browse files
committed
Replace Either usage with BoxBody
1 parent 1f58af5 commit 92f1513

File tree

1 file changed

+21
-15
lines changed
  • examples/tracing-http-propagator/src

1 file changed

+21
-15
lines changed

examples/tracing-http-propagator/src/server.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use http_body_util::{Either, Full};
1+
use http_body_util::{combinators::BoxBody, BodyExt, Full};
22
use hyper::{body::Incoming, service::service_fn, Request, Response, StatusCode};
33
use hyper_util::rt::{TokioExecutor, TokioIo};
44
use opentelemetry::{
@@ -21,32 +21,44 @@ fn extract_context_from_request(req: &Request<Incoming>) -> Context {
2121
}
2222

2323
// Separate async function for the handle endpoint
24-
async fn handle_health_check(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
24+
async fn handle_health_check(
25+
_req: Request<Incoming>,
26+
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, Infallible> {
2527
let tracer = global::tracer("example/server");
2628
let mut span = tracer
2729
.span_builder("health_check")
2830
.with_kind(SpanKind::Internal)
2931
.start(&tracer);
3032
span.add_event("Health check accessed", vec![]);
31-
let res = Response::new(Full::new(Bytes::from_static(b"Server is up and running!")));
33+
34+
let res = Response::new(
35+
Full::new(Bytes::from_static(b"Server is up and running!"))
36+
.map_err(|err| match err {})
37+
.boxed(),
38+
);
39+
3240
Ok(res)
3341
}
3442

3543
// Separate async function for the echo endpoint
36-
async fn handle_echo(req: Request<Incoming>) -> Result<Response<Incoming>, Infallible> {
44+
async fn handle_echo(
45+
req: Request<Incoming>,
46+
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, Infallible> {
3747
let tracer = global::tracer("example/server");
3848
let mut span = tracer
3949
.span_builder("echo")
4050
.with_kind(SpanKind::Internal)
4151
.start(&tracer);
4252
span.add_event("Echoing back the request", vec![]);
43-
let res = Response::new(req.into_body());
53+
54+
let res = Response::new(req.into_body().boxed());
55+
4456
Ok(res)
4557
}
4658

4759
async fn router(
4860
req: Request<Incoming>,
49-
) -> Result<Response<Either<Full<Bytes>, Incoming>>, Infallible> {
61+
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, Infallible> {
5062
// Extract the context from the incoming request headers
5163
let parent_cx = extract_context_from_request(&req);
5264
let response = {
@@ -61,18 +73,12 @@ async fn router(
6173

6274
let cx = Context::default().with_span(span);
6375
match (req.method(), req.uri().path()) {
64-
(&hyper::Method::GET, "/health") => handle_health_check(req)
65-
.with_context(cx)
66-
.await
67-
.map(|response| response.map(Either::Left)),
68-
(&hyper::Method::GET, "/echo") => handle_echo(req)
69-
.with_context(cx)
70-
.await
71-
.map(|response| response.map(Either::Right)),
76+
(&hyper::Method::GET, "/health") => handle_health_check(req).with_context(cx).await,
77+
(&hyper::Method::GET, "/echo") => handle_echo(req).with_context(cx).await,
7278
_ => {
7379
cx.span()
7480
.set_attribute(KeyValue::new(trace::HTTP_RESPONSE_STATUS_CODE, 404));
75-
let mut not_found = Response::new(Either::Left(Full::default()));
81+
let mut not_found = Response::new(BoxBody::default());
7682
*not_found.status_mut() = StatusCode::NOT_FOUND;
7783
Ok(not_found)
7884
}

0 commit comments

Comments
 (0)