reqwest_datadog_tracing/
middleware.rs

1// MIT License
2//
3// Copyright (c) 2021 TrueLayer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23use http::Extensions;
24use reqwest::{Request, Response};
25use reqwest_middleware::{Middleware, Next, Result};
26use tracing::Instrument;
27
28use crate::{DefaultSpanBackend, ReqwestOtelSpanBackend};
29
30/// Middleware for tracing requests using the current Opentelemetry Context.
31pub struct TracingMiddleware<S: ReqwestOtelSpanBackend> {
32    span_backend: std::marker::PhantomData<S>,
33}
34
35impl<S: ReqwestOtelSpanBackend> TracingMiddleware<S> {
36    pub fn new() -> TracingMiddleware<S> {
37        TracingMiddleware {
38            span_backend: Default::default(),
39        }
40    }
41}
42
43impl<S: ReqwestOtelSpanBackend> Clone for TracingMiddleware<S> {
44    fn clone(&self) -> Self {
45        Self::new()
46    }
47}
48
49impl Default for TracingMiddleware<DefaultSpanBackend> {
50    fn default() -> Self {
51        TracingMiddleware::new()
52    }
53}
54
55#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
56#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
57impl<ReqwestOtelSpan> Middleware for TracingMiddleware<ReqwestOtelSpan>
58where
59    ReqwestOtelSpan: ReqwestOtelSpanBackend + Sync + Send + 'static,
60{
61    async fn handle(
62        &self,
63        req: Request,
64        extensions: &mut Extensions,
65        next: Next<'_>,
66    ) -> Result<Response> {
67        let request_span = ReqwestOtelSpan::on_request_start(&req, extensions);
68
69        let outcome_future = async {
70            #[cfg(any(feature = "opentelemetry_0_30",))]
71            let req = if extensions.get::<crate::DisableOtelPropagation>().is_none() {
72                // Adds tracing headers to the given request to propagate the OpenTelemetry context to downstream revivers of the request.
73                // Spans added by downstream consumers will be part of the same trace.
74                crate::otel::inject_opentelemetry_context_into_request(req)
75            } else {
76                req
77            };
78
79            // Run the request
80            let outcome = next.run(req, extensions).await;
81            ReqwestOtelSpan::on_request_end(&request_span, &outcome, extensions);
82            outcome
83        };
84
85        outcome_future.instrument(request_span.clone()).await
86    }
87}