tracing_datadog/
layer.rs

1use crate::{
2    log::{DatadogLog, FieldVisitor},
3    span::{DatadogSpan, SpanAttributeVisitor, SpanLink},
4};
5use jiff::Zoned;
6use reqwest::header::HeaderValue;
7use std::{
8    borrow::Cow,
9    collections::HashMap,
10    fmt::{Display, Formatter},
11    marker::PhantomData,
12    sync::{Arc, Mutex, mpsc},
13    thread::spawn,
14    time::{SystemTime, UNIX_EPOCH},
15};
16use tracing_core::{
17    Event, Subscriber,
18    span::{Attributes, Id, Record},
19};
20use tracing_subscriber::{
21    Layer,
22    layer::Context,
23    registry::{LookupSpan, Scope},
24};
25
26/// A [`Layer`] that sends traces to Datadog.
27///
28/// ```
29/// # use tracing_subscriber::prelude::*;
30/// # use tracing_datadog::DatadogTraceLayer;
31/// tracing_subscriber::registry()
32///    .with(
33///        DatadogTraceLayer::builder()
34///            .service("my-service")
35///            .env("production")
36///            .version("git sha")
37///            .agent_address("localhost:8126")
38///            .build()
39///            .expect("failed to build DatadogTraceLayer"),
40///    )
41///    .init();
42/// ```
43#[derive(Debug)]
44pub struct DatadogTraceLayer<S> {
45    buffer: Arc<Mutex<Vec<DatadogSpan>>>,
46    service: String,
47    default_tags: HashMap<Cow<'static, str>, String>,
48    logging_enabled: bool,
49    #[cfg(feature = "http")]
50    with_context: crate::http::WithContext,
51    shutdown: mpsc::Sender<()>,
52    _registry: PhantomData<S>,
53}
54
55impl<S> DatadogTraceLayer<S>
56where
57    S: Subscriber + for<'a> LookupSpan<'a>,
58{
59    /// Creates a builder to construct a [`DatadogTraceLayer`].
60    pub fn builder() -> DatadogTraceLayerBuilder<S> {
61        DatadogTraceLayerBuilder {
62            service: None,
63            default_tags: HashMap::from_iter([("span.kind".into(), "internal".to_string())]),
64            agent_address: None,
65            container_id: None,
66            logging_enabled: false,
67            phantom_data: Default::default(),
68        }
69    }
70
71    #[cfg(feature = "http")]
72    fn get_context(
73        dispatch: &tracing_core::Dispatch,
74        id: &Id,
75        f: &mut dyn FnMut(&mut DatadogSpan),
76    ) {
77        let subscriber = dispatch
78            .downcast_ref::<S>()
79            .expect("Subscriber did not downcast to expected type, this is a bug");
80        let span = subscriber.span(id).expect("Span not found, this is a bug");
81
82        let mut extensions = span.extensions_mut();
83        if let Some(dd_span) = extensions.get_mut::<DatadogSpan>() {
84            f(dd_span);
85        }
86    }
87}
88
89impl<S> Drop for DatadogTraceLayer<S> {
90    fn drop(&mut self) {
91        let _ = self.shutdown.send(());
92    }
93}
94
95impl<S> Layer<S> for DatadogTraceLayer<S>
96where
97    S: Subscriber + for<'a> LookupSpan<'a>,
98{
99    fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
100        let span = ctx.span(id).expect("Span not found, this is a bug");
101        let mut extensions = span.extensions_mut();
102
103        let trace_id = span
104            .parent()
105            .map(|parent| {
106                parent
107                    .extensions()
108                    .get::<DatadogSpan>()
109                    .expect("Parent span didn't have a DatadogSpan extension, this is a bug")
110                    .trace_id
111            })
112            .unwrap_or(rand::random_range(1..=u64::MAX));
113
114        debug_assert!(trace_id != 0, "Trace ID is zero, this is a bug");
115
116        let mut dd_span = DatadogSpan {
117            name: span.name().to_string(),
118            service: self.service.clone(),
119            r#type: "custom".into(),
120            span_id: span.id().into_u64(),
121            start: epoch_ns(),
122            parent_id: span
123                .parent()
124                .map(|parent| parent.id().into_u64())
125                .unwrap_or_default(),
126            trace_id,
127            meta: self.default_tags.clone(),
128            metrics: {
129                let mut m = HashMap::new();
130                if span.parent().is_none() {
131                    // Special tag to mark the service entry span.
132                    m.insert("_dd.top_level", 1.0);
133                    m.insert("_dd.agent_psr", 1.0);
134                    m.insert("_dd.rule_psr", 1.0);
135                    m.insert("_dd.limit_psr", 1.0);
136                    m.insert("_sample_rate", 1.0);
137                }
138                m.insert("_sampling_priority_v1", 1.0);
139                m.insert("process_id", std::process::id() as f64);
140                m
141            },
142            ..Default::default()
143        };
144
145        attrs.record(&mut SpanAttributeVisitor::new(&mut dd_span));
146
147        extensions.insert(dd_span);
148    }
149
150    fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
151        let span = ctx.span(id).expect("Span not found, this is a bug");
152        let mut extensions = span.extensions_mut();
153
154        if let Some(dd_span) = extensions.get_mut::<DatadogSpan>() {
155            values.record(&mut SpanAttributeVisitor::new(dd_span));
156        }
157    }
158
159    fn on_follows_from(&self, id: &Id, follows: &Id, ctx: Context<'_, S>) {
160        let span = ctx.span(id).expect("Span not found, this is a bug");
161        let mut extensions = span.extensions_mut();
162
163        let Some(other_span) = ctx.span(follows) else {
164            // The other span might be filtered or closed, so we can't access it.
165            return;
166        };
167
168        if let Some(dd_span) = extensions.get_mut::<DatadogSpan>()
169            && let Some(other_dd_span) = other_span.extensions().get::<DatadogSpan>()
170        {
171            dd_span.span_links.push(SpanLink {
172                trace_id: other_dd_span.trace_id,
173                span_id: other_dd_span.span_id,
174            })
175        }
176    }
177
178    fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
179        if !self.logging_enabled {
180            return;
181        }
182
183        let mut fields = {
184            let mut visitor = FieldVisitor::default();
185            event.record(&mut visitor);
186            visitor.finish()
187        };
188
189        fields.extend(
190            ctx.event_scope(event)
191                .into_iter()
192                .flat_map(Scope::from_root)
193                .flat_map(|span| match span.extensions().get::<DatadogSpan>() {
194                    Some(dd_span) => dd_span.meta.clone(),
195                    None => panic!("DatadogSpan extension not found, this is a bug"),
196                }),
197        );
198
199        let message = fields.remove("message").unwrap_or_default();
200
201        let (trace_id, span_id) = ctx
202            .lookup_current()
203            .and_then(|span| {
204                span.extensions()
205                    .get::<DatadogSpan>()
206                    .map(|dd_span| (Some(dd_span.trace_id), Some(dd_span.span_id)))
207            })
208            .unwrap_or_default();
209
210        let log = DatadogLog {
211            timestamp: Zoned::now().timestamp(),
212            level: event.metadata().level().to_owned(),
213            message,
214            trace_id,
215            span_id,
216            fields,
217        };
218
219        let serialized = serde_json::to_string(&log).expect("Failed to serialize log");
220
221        println!("{serialized}");
222    }
223
224    fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
225        let span = ctx.span(id).expect("Span not found, this is a bug");
226        let mut extensions = span.extensions_mut();
227
228        let now = epoch_ns();
229
230        match extensions.get_mut::<DatadogSpan>() {
231            Some(dd_span) if dd_span.start == 0 => dd_span.start = now,
232            _ => {}
233        }
234    }
235
236    fn on_exit(&self, id: &Id, ctx: Context<'_, S>) {
237        let span = ctx.span(id).expect("Span not found, this is a bug");
238        let mut extensions = span.extensions_mut();
239
240        let now = epoch_ns();
241
242        if let Some(dd_span) = extensions.get_mut::<DatadogSpan>() {
243            dd_span.duration = now - dd_span.start
244        }
245    }
246
247    fn on_close(&self, id: Id, ctx: Context<'_, S>) {
248        let span = ctx.span(&id).expect("Span not found, this is a bug");
249        let mut extensions = span.extensions_mut();
250
251        if let Some(mut dd_span) = extensions.remove::<DatadogSpan>() {
252            // Enable trace metrics for select span kinds.
253            if let Some("server" | "client" | "consumer" | "producer") =
254                dd_span.meta.get("span.kind").map(String::as_str)
255            {
256                dd_span.metrics.insert("_dd.measured", 1.0);
257                dd_span.metrics.insert("_dd1.sr.eausr", 1.0);
258            }
259
260            self.buffer.lock().unwrap().push(dd_span);
261        }
262    }
263
264    // SAFETY: This is safe because the `WithContext` function pointer is valid
265    // for the lifetime of `&self`.
266    #[cfg(feature = "http")]
267    unsafe fn downcast_raw(&self, id: std::any::TypeId) -> Option<*const ()> {
268        match id {
269            id if id == std::any::TypeId::of::<Self>() => Some(self as *const _ as *const ()),
270            id if id == std::any::TypeId::of::<crate::http::WithContext>() => {
271                Some(&self.with_context as *const _ as *const ())
272            }
273            _ => None,
274        }
275    }
276}
277
278/// A builder for [`DatadogTraceLayer`].
279pub struct DatadogTraceLayerBuilder<S> {
280    service: Option<String>,
281    default_tags: HashMap<Cow<'static, str>, String>,
282    agent_address: Option<String>,
283    container_id: Option<String>,
284    logging_enabled: bool,
285    phantom_data: PhantomData<S>,
286}
287
288/// An error that can occur when building a [`DatadogTraceLayer`].
289#[derive(Debug)]
290pub struct BuilderError(&'static str);
291
292impl Display for BuilderError {
293    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
294        f.write_str(self.0)
295    }
296}
297
298impl std::error::Error for BuilderError {}
299
300impl<S> DatadogTraceLayerBuilder<S>
301where
302    S: Subscriber + for<'a> LookupSpan<'a>,
303{
304    /// Sets the `service`. This is required.
305    pub fn service(mut self, service: impl Into<String>) -> Self {
306        self.service = Some(service.into());
307        self
308    }
309
310    /// Sets the `env`. This is required.
311    pub fn env(mut self, env: impl Into<String>) -> Self {
312        self.default_tags.insert("env".into(), env.into());
313        self
314    }
315
316    /// Sets the `version`. This is required.
317    pub fn version(mut self, version: impl Into<String>) -> Self {
318        self.default_tags.insert("version".into(), version.into());
319        self
320    }
321
322    /// Sets the `agent_address`. This is required.
323    pub fn agent_address(mut self, agent_address: impl Into<String>) -> Self {
324        self.agent_address = Some(agent_address.into());
325        self
326    }
327
328    /// Adds a fixed default tag to all spans.
329    ///
330    /// This can be used multiple times for several tags.
331    ///
332    /// Default tags are overridden by tags set explicitly on a span.
333    pub fn default_tag(
334        mut self,
335        key: impl Into<Cow<'static, str>>,
336        value: impl Into<String>,
337    ) -> Self {
338        let _ = self.default_tags.insert(key.into(), value.into());
339        self
340    }
341
342    /// Sets the container ID. This enables infrastructure metrics in APM for supported platforms.
343    pub fn container_id(mut self, container_id: impl Into<String>) -> Self {
344        self.container_id = Some(container_id.into());
345        self
346    }
347
348    /// Enables or disables structured logging with trace correlation to stdout.
349    /// Disabled by default.
350    pub fn enable_logs(mut self, enable_logs: bool) -> Self {
351        self.logging_enabled = enable_logs;
352        self
353    }
354
355    /// Consumes the builder to construct the tracing layer.
356    pub fn build(self) -> Result<DatadogTraceLayer<S>, BuilderError> {
357        let Some(service) = self.service else {
358            return Err(BuilderError("service is required"));
359        };
360        if !self.default_tags.contains_key("env") {
361            return Err(BuilderError("env is required"));
362        };
363        if !self.default_tags.contains_key("version") {
364            return Err(BuilderError("version is required"));
365        };
366        let Some(agent_address) = self.agent_address else {
367            return Err(BuilderError("agent_address is required"));
368        };
369        let container_id = match self.container_id {
370            Some(s) => Some(
371                s.parse::<HeaderValue>()
372                    .map_err(|_| BuilderError("Failed to parse container ID into header"))?,
373            ),
374            _ => None,
375        };
376
377        let buffer = Arc::new(Mutex::new(Vec::new()));
378        let (shutdown, shutdown_rx) = mpsc::channel();
379
380        spawn(crate::export::exporter(
381            agent_address,
382            buffer.clone(),
383            container_id,
384            shutdown_rx,
385        ));
386
387        Ok(DatadogTraceLayer {
388            buffer,
389            service,
390            default_tags: self.default_tags,
391            logging_enabled: self.logging_enabled,
392            #[cfg(feature = "http")]
393            with_context: crate::http::WithContext(DatadogTraceLayer::<S>::get_context),
394            shutdown,
395            _registry: PhantomData,
396        })
397    }
398}
399
400/// Returns the current system time as nanoseconds since 1970.
401fn epoch_ns() -> i64 {
402    SystemTime::now()
403        .duration_since(UNIX_EPOCH)
404        .expect("SystemTime is before UNIX epoch")
405        .as_nanos() as i64
406}
407
408#[cfg(test)]
409mod tests {
410    use super::*;
411
412    #[test]
413    fn builder_builds_successfully() {
414        assert!(
415            DatadogTraceLayer::<tracing_subscriber::Registry>::builder()
416                .service("test-service")
417                .env("test")
418                .version("test-version")
419                .agent_address("localhost:8126")
420                .build()
421                .is_ok()
422        );
423    }
424
425    #[test]
426    fn service_is_required() {
427        let result = DatadogTraceLayer::<tracing_subscriber::Registry>::builder()
428            .env("test")
429            .version("test-version")
430            .agent_address("localhost:8126")
431            .build();
432        assert!(result.unwrap_err().to_string().contains("service"));
433    }
434
435    #[test]
436    fn env_is_required() {
437        let result = DatadogTraceLayer::<tracing_subscriber::Registry>::builder()
438            .service("test-service")
439            .version("test-version")
440            .agent_address("localhost:8126")
441            .build();
442        assert!(result.unwrap_err().to_string().contains("env"));
443    }
444
445    #[test]
446    fn version_is_required() {
447        let result = DatadogTraceLayer::<tracing_subscriber::Registry>::builder()
448            .service("test-service")
449            .env("test")
450            .agent_address("localhost:8126")
451            .build();
452        assert!(result.unwrap_err().to_string().contains("version"));
453    }
454
455    #[test]
456    fn agent_address_is_required() {
457        let result = DatadogTraceLayer::<tracing_subscriber::Registry>::builder()
458            .service("test-service")
459            .env("test")
460            .version("test-version")
461            .build();
462        assert!(result.unwrap_err().to_string().contains("agent_address"));
463    }
464
465    #[test]
466    fn default_default_tags_include_env_and_version() {
467        let layer: DatadogTraceLayer<tracing_subscriber::Registry> = DatadogTraceLayer::builder()
468            .service("test-service")
469            .env("test")
470            .version("test-version")
471            .agent_address("localhost:8126")
472            .build()
473            .unwrap();
474        let default_tags = &layer.default_tags;
475        assert_eq!(default_tags["env"], "test");
476        assert_eq!(default_tags["version"], "test-version");
477    }
478
479    #[test]
480    fn default_tags_can_be_added() {
481        let layer: DatadogTraceLayer<tracing_subscriber::Registry> = DatadogTraceLayer::builder()
482            .service("test-service")
483            .env("test")
484            .version("test-version")
485            .agent_address("localhost:8126")
486            .default_tag("static", "bar")
487            .default_tag(String::from("dynamic"), "qux")
488            .build()
489            .unwrap();
490        let default_tags = &layer.default_tags;
491        assert_eq!(default_tags["static"], "bar");
492        assert_eq!(default_tags["dynamic"], "qux");
493    }
494}