1+ //! An event formatter to emit events in a way that Datadog can correlate them with traces.
2+ //!
3+ //! Datadog's trace ID and span ID format is different from the OpenTelemetry standard.
4+ //! Using this formatter, the trace ID is converted to the correct format.
5+ //! It also adds the trace ID to the `dd.trace_id` field and the span ID to the
6+ //! `dd.span_id` field, which is where Datadog looks for these by default
7+ //! (although the path to the trace ID can be overridden in Datadog).
18use std:: io;
29
310use chrono:: Utc ;
@@ -11,9 +18,9 @@ use tracing_subscriber::fmt::format::Writer;
1118use tracing_subscriber:: fmt:: { FmtContext , FormatEvent , FormatFields } ;
1219use tracing_subscriber:: registry:: { LookupSpan , SpanRef } ;
1320
14- pub struct TraceInfo {
15- pub trace_id : String ,
16- pub span_id : String ,
21+ struct TraceInfo {
22+ trace_id : String ,
23+ span_id : String ,
1724}
1825
1926fn convert_trace_id ( id : TraceId ) -> String {
@@ -24,14 +31,14 @@ fn convert_trace_id(id: TraceId) -> String {
2431}
2532
2633fn convert_span_id ( id : SpanId ) -> String {
27- let dd_id = u64:: from_be_bytes ( id. to_bytes ( ) . try_into ( ) . unwrap ( ) ) ;
34+ let dd_id = u64:: from_be_bytes ( id. to_bytes ( ) ) ;
2835
2936 dd_id. to_string ( )
3037}
3138
3239fn lookup_trace_info < S > ( span_ref : & SpanRef < S > ) -> Option < TraceInfo >
33- where
34- S : Subscriber + for < ' a > LookupSpan < ' a > ,
40+ where
41+ S : Subscriber + for < ' a > LookupSpan < ' a > ,
3542{
3643 span_ref. extensions ( ) . get :: < OtelData > ( ) . map ( |o| TraceInfo {
3744 trace_id : convert_trace_id ( o. parent_cx . span ( ) . span_context ( ) . trace_id ( ) ) ,
@@ -40,21 +47,21 @@ fn lookup_trace_info<S>(span_ref: &SpanRef<S>) -> Option<TraceInfo>
4047}
4148
4249// mostly stolen from here: https://github.com/tokio-rs/tracing/issues/1531
43- pub struct TraceIdFormat ;
50+ pub struct DatadogFormatter ;
4451
45- impl < S , N > FormatEvent < S , N > for TraceIdFormat
46- where
47- S : Subscriber + for < ' lookup > LookupSpan < ' lookup > ,
48- N : for < ' writer > FormatFields < ' writer > + ' static ,
52+ impl < S , N > FormatEvent < S , N > for DatadogFormatter
53+ where
54+ S : Subscriber + for < ' lookup > LookupSpan < ' lookup > ,
55+ N : for < ' writer > FormatFields < ' writer > + ' static ,
4956{
5057 fn format_event (
5158 & self ,
5259 ctx : & FmtContext < ' _ , S , N > ,
5360 mut writer : Writer < ' _ > ,
5461 event : & Event < ' _ > ,
5562 ) -> std:: fmt:: Result
56- where
57- S : Subscriber + for < ' a > LookupSpan < ' a > ,
63+ where
64+ S : Subscriber + for < ' a > LookupSpan < ' a > ,
5865 {
5966 let meta = event. metadata ( ) ;
6067
@@ -81,12 +88,12 @@ impl<S, N> FormatEvent<S, N> for TraceIdFormat
8188 }
8289}
8390
84- pub struct WriteAdaptor < ' a > {
91+ struct WriteAdaptor < ' a > {
8592 fmt_write : & ' a mut dyn std:: fmt:: Write ,
8693}
8794
8895impl < ' a > WriteAdaptor < ' a > {
89- pub fn new ( fmt_write : & ' a mut dyn std:: fmt:: Write ) -> Self {
96+ fn new ( fmt_write : & ' a mut dyn std:: fmt:: Write ) -> Self {
9097 Self { fmt_write }
9198 }
9299}
0 commit comments