Skip to content

Commit

Permalink
WIP: don't include body transfer to root span lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Aug 10, 2023
1 parent 79495dc commit e65af95
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions casper-server/src/middleware/trace.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::error::Error;
// use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use ntex::http::body::{Body, BodySize, MessageBody, ResponseBody};
// use ntex::http::body::{Body, BodySize, MessageBody, ResponseBody};
use ntex::service::{
forward_poll_ready, forward_poll_shutdown, Middleware, Service, ServiceCall, ServiceCtx,
};
use ntex::util::Bytes;
// use ntex::util::Bytes;
use ntex::web::{ErrorRenderer, WebRequest, WebResponse};

use opentelemetry::trace::TraceContextExt as _;
Expand Down Expand Up @@ -68,7 +68,7 @@ where
req.host = %connection_info.host(),
req.peer_addr = %req.peer_addr().map(|addr| addr.to_string()).unwrap_or_default(),
resp.status_code = Empty,
otel.name = %format!("HTTP {} {}", req.method(), req.uri()),
otel.name = %format!("HTTP {} {}", req.method(), req.uri().path()),
otel.kind = "server",
otel.status_code = Empty,
trace_id = Empty,
Expand Down Expand Up @@ -131,6 +131,8 @@ where
this.span.in_scope(move || {
let res = futures::ready!(this.fut.poll(cx));

// span.context().span().end();

// Request is done
match res {
Ok(ref response) => {
Expand All @@ -147,32 +149,7 @@ where
span.record("otel.status_code", "ERROR");
}
}

Poll::Ready(res.map(|response| {
response.map_body(move |_, body| {
ResponseBody::Other(Body::from_message(StreamSpan { body, span }))
})
}))
Poll::Ready(res)
})
}
}

struct StreamSpan {
body: ResponseBody<Body>,
span: Span,
}

impl MessageBody for StreamSpan {
#[inline]
fn size(&self) -> BodySize {
self.body.size()
}

#[inline]
fn poll_next_chunk(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Box<dyn Error>>>> {
self.span.in_scope(|| self.body.poll_next_chunk(cx))
}
}

0 comments on commit e65af95

Please sign in to comment.