drogue_client/core/client/
mod.rs

1mod r#impl;
2
3pub(crate) use r#impl::CoreClient;
4
5pub trait PropagateCurrentContext {
6    fn propagate_current_context(self) -> Self
7    where
8        Self: Sized;
9}
10
11#[cfg(not(feature = "telemetry"))]
12impl PropagateCurrentContext for reqwest::RequestBuilder {
13    #[inline]
14    fn propagate_current_context(self) -> Self
15    where
16        Self: Sized,
17    {
18        self
19    }
20}
21
22#[cfg(feature = "telemetry")]
23impl PropagateCurrentContext for reqwest::RequestBuilder {
24    #[inline]
25    fn propagate_current_context(self) -> Self
26    where
27        Self: Sized,
28    {
29        self.propagate_context(&opentelemetry::Context::current())
30    }
31}
32
33#[cfg(feature = "telemetry")]
34pub use self::tracing::*;
35
36#[cfg(feature = "telemetry")]
37mod tracing {
38    use http::HeaderMap;
39    use opentelemetry::propagation::Injector;
40    use opentelemetry::Context;
41    use reqwest::RequestBuilder;
42
43    pub trait WithTracing {
44        fn propagate_context(self, cx: &Context) -> Self;
45    }
46
47    impl WithTracing for RequestBuilder {
48        fn propagate_context(self, cx: &Context) -> Self {
49            let headers = opentelemetry::global::get_text_map_propagator(|prop| {
50                let mut injector = HeaderInjector::new();
51                prop.inject_context(cx, &mut injector);
52                injector.0
53            });
54            self.headers(headers)
55        }
56    }
57
58    struct HeaderInjector(HeaderMap);
59
60    impl HeaderInjector {
61        pub fn new() -> Self {
62            Self(Default::default())
63        }
64    }
65
66    impl Injector for HeaderInjector {
67        /// Set a key and value in the HeaderMap.  Does nothing if the key or value are not valid inputs.
68        fn set(&mut self, key: &str, value: String) {
69            if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) {
70                if let Ok(val) = http::header::HeaderValue::from_str(&value) {
71                    self.0.insert(name, val);
72                }
73            }
74        }
75    }
76}