axum_tracing_opentelemetry/middleware/
response_injector.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use futures_core::future::BoxFuture;
use http::{Request, Response};
use std::task::{Context, Poll};
use tower::{Layer, Service};
use tracing_opentelemetry_instrumentation_sdk as otel;
use tracing_opentelemetry_instrumentation_sdk::http as otel_http;

#[deprecated(
    since = "0.12.0",
    note = "keep for transition, replaced by OtelInResponseLayer"
)]
#[must_use]
pub fn response_with_trace_layer() -> OtelInResponseLayer {
    OtelInResponseLayer {}
}

#[derive(Default, Debug, Clone)]
pub struct OtelInResponseLayer;

impl<S> Layer<S> for OtelInResponseLayer {
    type Service = OtelInResponseService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        OtelInResponseService { inner }
    }
}

#[derive(Default, Debug, Clone)]
pub struct OtelInResponseService<S> {
    inner: S,
}

impl<S, B, B2> Service<Request<B>> for OtelInResponseService<S>
where
    S: Service<Request<B>, Response = Response<B2>> + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    // `BoxFuture` is a type alias for `Pin<Box<dyn Future + Send + 'a>>`
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    #[allow(unused_mut)]
    fn call(&mut self, mut request: Request<B>) -> Self::Future {
        let future = self.inner.call(request);

        Box::pin(async move {
            let mut response = future.await?;
            // inject the trace context into the response (optional but useful for debugging and client)
            otel_http::inject_context(&otel::find_current_context(), response.headers_mut());
            Ok(response)
        })
    }
}