datadog-opentelemetry 0.2.0

A Datadog layer of compatibility for the opentelemetry SDK
Documentation
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use hashbrown::{hash_map, HashMap as BHashMap};
use std::{
    collections::HashMap,
    fmt::Debug,
    str::FromStr,
    sync::{Arc, RwLock},
};

use crate::{
    core::{
        configuration::{
            remote_config::{
                RemoteConfigClientError, RemoteConfigClientHandle, RemoteConfigClientWorker,
            },
            Config,
        },
        constants::SAMPLING_DECISION_MAKER_TAG_KEY,
        sampling::SamplingDecision,
        telemetry::init_telemetry,
        utils::WorkerError,
    },
    create_dd_resource, dd_debug, dd_error,
    span_exporter::DatadogExporter,
    spans_metrics::{TelemetryMetricsCollector, TelemetryMetricsCollectorHandle},
    text_map_propagator::DatadogExtractData,
};
use opentelemetry::{
    global::ObjectSafeSpan,
    trace::{SpanContext, TraceContextExt, TraceState},
    Key, KeyValue, SpanId, TraceFlags, TraceId,
};
use opentelemetry_sdk::trace::SpanData;
use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;

#[derive(Debug)]
struct Trace {
    local_root_span_id: [u8; 8],
    /// Root span will always be the first span in this vector if it is present
    finished_spans: Vec<SpanData>,
    open_span_count: usize,

    propagation_data: TracePropagationData,
}

#[derive(Debug, Clone)]
pub(crate) struct TracePropagationData {
    pub sampling_decision: SamplingDecision,
    pub origin: Option<String>,
    pub tags: Option<HashMap<String, String>>,
}

const EMPTY_PROPAGATION_DATA: TracePropagationData = TracePropagationData {
    origin: None,
    sampling_decision: SamplingDecision {
        priority: None,
        mechanism: None,
    },
    tags: None,
};

#[derive(Debug)]
struct InnerTraceRegistry {
    registry: BHashMap<[u8; 16], Trace>,
    metrics: TraceRegistryMetrics,
    config: Arc<Config>,
}

pub enum RegisterTracePropagationResult {
    Existing(SamplingDecision),
    New,
}

impl InnerTraceRegistry {
    fn register_local_root_trace_propagation_data(
        &mut self,
        trace_id: [u8; 16],
        propagation_data: TracePropagationData,
    ) -> RegisterTracePropagationResult {
        match self.registry.entry(trace_id) {
            hash_map::Entry::Occupied(mut occupied_entry) => {
                if occupied_entry
                    .get()
                    .propagation_data
                    .sampling_decision
                    .priority
                    .is_some()
                {
                    RegisterTracePropagationResult::Existing(
                        occupied_entry.get().propagation_data.sampling_decision,
                    )
                } else {
                    let trace = occupied_entry.get_mut();
                    trace.propagation_data = propagation_data;
                    RegisterTracePropagationResult::New
                }
            }
            hash_map::Entry::Vacant(vacant_entry) => {
                vacant_entry.insert(Trace {
                    local_root_span_id: [0; 8], /* This will be set when the first span is
                                                 * registered */
                    finished_spans: Vec::new(),
                    // We set the open span count to 1 to take into account the local root span
                    // then once we register it, we don't actually increment the open span count
                    // We have to do this because the tracing-otel bridge doesn't actually
                    // materialize spans until they are closed.
                    // Which means that if we don't consider the local root span as "opened" when we
                    // register it's propagation data, then child spans might be
                    // sent flushed prematurely
                    open_span_count: 1,
                    propagation_data,
                });
                self.metrics.trace_segments_created += 1;
                self.metrics.spans_created += 1;
                RegisterTracePropagationResult::New
            }
        }
    }

    /// Set the root span ID for a given trace ID.
    ///
    /// This should be paired, after a call to `register_local_root_trace_propagation_data`
    fn register_local_root_span(&mut self, trace_id: [u8; 16], root_span_id: [u8; 8]) {
        let trace = self.registry.entry(trace_id).or_insert_with(|| Trace {
            local_root_span_id: [0; 8], // This will be set when the first span is registered
            finished_spans: Vec::new(),
            open_span_count: 1,
            propagation_data: EMPTY_PROPAGATION_DATA,
        });
        if trace.local_root_span_id == [0; 8] {
            trace.local_root_span_id = root_span_id;
        } else {
            dd_debug!(
                "TraceRegistry.register_local_root_span: trace with trace_id={:?} already has a root span registered with root_span_id={:?}. Ignoring the new root_span_id={:?}",
                trace_id,
                trace.local_root_span_id,
                root_span_id
            );
        }
    }

    /// Register a new trace with the given trace ID and span ID.
    /// If the trace is already registered, increment the open span count.
    /// If the trace is not registered, create a new entry with the given trace ID
    fn register_span(
        &mut self,
        trace_id: [u8; 16],
        span_id: [u8; 8],
        propagation_data: TracePropagationData,
    ) {
        self.registry
            .entry(trace_id)
            .or_insert_with(|| {
                self.metrics.trace_segments_created += 1;
                Trace {
                    local_root_span_id: span_id,
                    finished_spans: Vec::new(),
                    open_span_count: 0,
                    propagation_data,
                }
            })
            .open_span_count += 1;
        self.metrics.spans_created += 1;
    }

    /// Finish a span with the given trace ID and span data.
    /// If the trace is finished (i.e., all spans are finished), return the full trace chunk.
    /// Otherwise, return None.
    ///
    /// This function tries to maintain the invariant that the first span of the trace chunk should
    /// be the local root span, since it makes processing latter easier.
    /// If the root span is not the first span, it will be swapped with the first span.
    ///
    /// # Bounding memory usage
    ///
    /// If partial flushing in not enabled traces with unfinished spans are kept forever in memory.
    /// This lead to unbounded memory usage, if new spans keep getting added to the trace.
    ///
    /// Otherwise we flush partial trace chunks when then contain more than the configured minimum
    /// count of spans
    fn finish_span(&mut self, trace_id: [u8; 16], span_data: SpanData) -> Option<Trace> {
        self.metrics.spans_finished += 1;
        if let hash_map::Entry::Occupied(mut slot) = self.registry.entry(trace_id) {
            let trace = slot.get_mut();
            let span = if !trace.finished_spans.is_empty()
                && span_data.span_context.span_id().to_bytes() == trace.local_root_span_id
            {
                std::mem::replace(&mut trace.finished_spans[0], span_data)
            } else {
                span_data
            };

            // Reserve enough space to store all currently open spans in the chunk,
            trace.finished_spans.reserve(trace.open_span_count);
            trace.finished_spans.push(span);

            trace.open_span_count = trace.open_span_count.saturating_sub(1);
            let partial_flush = self.config.trace_partial_flush_enabled()
                && trace.finished_spans.len() >= self.config.trace_partial_flush_min_spans();
            if partial_flush {
                self.metrics.trace_partial_flush_count += 1;
                let trace = Trace {
                    local_root_span_id: trace.local_root_span_id,
                    finished_spans: std::mem::take(&mut trace.finished_spans),
                    open_span_count: trace.open_span_count,
                    propagation_data: trace.propagation_data.clone(),
                };
                Some(trace)
            } else if trace.open_span_count == 0 {
                let trace = slot.remove();
                self.metrics.trace_segments_closed += 1;
                Some(trace)
            } else {
                None
            }
        } else {
            // if we somehow don't have the trace registered, we just flush the span...
            self.metrics.trace_segments_created += 1;
            self.metrics.trace_segments_closed += 1;

            dd_debug!(
                "TraceRegistry.finish_span: trace with trace_id={:?} has a finished span span_id={:?}, but hasn't been registered first. This is probably a bug.",
                u128::from_be_bytes(trace_id),
                u64::from_be_bytes(span_data.span_context.span_id().to_bytes())

            );
            Some(Trace {
                local_root_span_id: span_data.span_context.span_id().to_bytes(),
                finished_spans: vec![span_data],
                open_span_count: 0,
                propagation_data: EMPTY_PROPAGATION_DATA,
            })
        }
    }

    fn get_trace_propagation_data(&self, trace_id: [u8; 16]) -> &TracePropagationData {
        match self.registry.get(&trace_id) {
            Some(trace) => &trace.propagation_data,
            None => &EMPTY_PROPAGATION_DATA,
        }
    }

    fn get_metrics(&mut self) -> TraceRegistryMetrics {
        std::mem::take(&mut self.metrics)
    }
}

const TRACE_REGISTRY_SHARDS: usize = 64;

#[repr(align(128))]
#[derive(Debug, Clone)]
struct CachePadded<T>(T);

#[derive(Clone, Debug)]
/// A registry of traces that are currently running
///
/// This registry maintains the following information:
/// - The root span ID of the trace
/// - The finished spans of the trace
/// - The number of open spans in the trace
/// - The sampling decision of the trace
pub(crate) struct TraceRegistry {
    // Example:
    // inner: Arc<[CacheAligned<RwLock<InnerTraceRegistry>>; N]>;
    // to access a trace we do inner[hash(trace_id) % N].read()
    inner: Arc<[CachePadded<RwLock<InnerTraceRegistry>>; TRACE_REGISTRY_SHARDS]>,
    hasher: foldhash::fast::RandomState,
}

impl TraceRegistry {
    pub fn new(config: Arc<Config>) -> Self {
        Self {
            inner: Arc::new(std::array::from_fn(|_| {
                CachePadded(RwLock::new(InnerTraceRegistry {
                    registry: BHashMap::new(),
                    metrics: TraceRegistryMetrics::default(),
                    config: config.clone(),
                }))
            })),
            hasher: foldhash::fast::RandomState::default(),
        }
    }

    fn get_shard(&self, trace_id: [u8; 16]) -> &RwLock<InnerTraceRegistry> {
        use std::hash::BuildHasher;
        let hash = self.hasher.hash_one(u128::from_ne_bytes(trace_id));
        let shard = hash as usize % TRACE_REGISTRY_SHARDS;
        &self.inner[shard].0
    }

    /// Register the trace propagation data for a given trace ID
    /// This increases the open span count for the trace by 1, but does not set the root span ID.
    /// You will then need to call `register_local_root_span` to set the root span ID
    ///
    /// If the trace is already registered with a non None sampling decision,
    /// it will return the existing sampling decision instead
    pub fn register_local_root_trace_propagation_data(
        &self,
        trace_id: [u8; 16],
        propagation_data: TracePropagationData,
    ) -> RegisterTracePropagationResult {
        let mut inner = self
            .get_shard(trace_id)
            .write()
            .expect("Failed to acquire lock on trace registry");
        inner.register_local_root_trace_propagation_data(trace_id, propagation_data)
    }

    /// Set the root span ID for a given trace ID.
    /// This will also increment the open span count for the trace.
    /// If the trace is already registered, it will ignore the new root span ID and log a warning.
    pub fn register_local_root_span(&self, trace_id: [u8; 16], root_span_id: [u8; 8]) {
        let mut inner = self
            .get_shard(trace_id)
            .write()
            .expect("Failed to acquire lock on trace registry");
        inner.register_local_root_span(trace_id, root_span_id);
    }

    /// Register a new span with the given trace ID and span ID.
    pub fn register_span(
        &self,
        trace_id: [u8; 16],
        span_id: [u8; 8],
        propagation_data: TracePropagationData,
    ) {
        let mut inner = self
            .get_shard(trace_id)
            .write()
            .expect("Failed to acquire lock on trace registry");
        inner.register_span(trace_id, span_id, propagation_data);
    }

    /// Finish a span with the given trace ID and span data.
    /// If the trace is finished (i.e., all spans are finished), return the full trace chunk to
    /// flush
    fn finish_span(&self, trace_id: [u8; 16], span_data: SpanData) -> Option<Trace> {
        let mut inner = self
            .get_shard(trace_id)
            .write()
            .expect("Failed to acquire lock on trace registry");
        inner.finish_span(trace_id, span_data)
    }

    pub fn get_trace_propagation_data(&self, trace_id: [u8; 16]) -> TracePropagationData {
        let inner = self
            .get_shard(trace_id)
            .read()
            .expect("Failed to acquire lock on trace registry");

        inner.get_trace_propagation_data(trace_id).clone()
    }

    pub fn get_metrics(&self) -> TraceRegistryMetrics {
        let mut stats = TraceRegistryMetrics::default();
        for shard_idx in 0..TRACE_REGISTRY_SHARDS {
            let mut shard = self.inner[shard_idx].0.write().unwrap();
            let shard_stats = shard.get_metrics();
            stats.spans_created += shard_stats.spans_created;
            stats.spans_finished += shard_stats.spans_finished;
            stats.trace_segments_created += shard_stats.trace_segments_created;
            stats.trace_segments_closed += shard_stats.trace_segments_closed;
            stats.trace_partial_flush_count += shard_stats.trace_partial_flush_count;
        }
        stats
    }
}

#[derive(Default, Debug)]
pub struct TraceRegistryMetrics {
    pub spans_created: usize,
    pub spans_finished: usize,
    pub trace_segments_created: usize,
    pub trace_segments_closed: usize,
    pub trace_partial_flush_count: usize,
}

pub(crate) struct DatadogSpanProcessor {
    registry: TraceRegistry,
    span_exporter: DatadogExporter,
    resource: Arc<RwLock<Resource>>,
    config: Arc<Config>,
    rc_client_handle: Option<RemoteConfigClientHandle>,
    telemetry_metrics_handle: Option<TelemetryMetricsCollectorHandle>,
}

impl std::fmt::Debug for DatadogSpanProcessor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DatadogSpanProcessor").finish()
    }
}

impl DatadogSpanProcessor {
    #[allow(clippy::type_complexity)]
    pub(crate) fn new(
        config: Arc<Config>,
        registry: TraceRegistry,
        resource: Arc<RwLock<Resource>>,
        agent_response_handler: Option<Box<dyn for<'a> Fn(&'a str) + Send + Sync>>,
    ) -> Self {
        let rc_client_handle = if config.remote_config_enabled() && config.enabled() {
            RemoteConfigClientWorker::start(config.clone())
                .inspect_err(|e| {
                    dd_error!(
                        "RemoteConfigClientWorker.start: Failed to start remote config client: {}",
                        e
                    );
                })
                .ok()
        } else {
            None
        };
        let span_exporter = DatadogExporter::new(config.clone(), agent_response_handler);
        let telemetry_metrics_handle = config.telemetry_enabled().then(|| {
            TelemetryMetricsCollector::start(registry.clone(), span_exporter.queue_metrics())
        });

        Self {
            registry,
            span_exporter,
            resource,
            config,
            rc_client_handle,
            telemetry_metrics_handle,
        }
    }

    /// If SpanContext is remote, recover [`DatadogExtractData`] from parent context:
    /// - links generated during extraction are added to the root span as span links.
    /// - sampling decision, origin and tags are returned to be stored as Trace propagation data
    fn add_remote_links(
        &self,
        span: &mut opentelemetry_sdk::trace::Span,
        parent_ctx: &opentelemetry::Context,
    ) {
        if let Some(DatadogExtractData { links, .. }) = parent_ctx.get::<DatadogExtractData>() {
            links.iter().for_each(|link| {
                let link_ctx = SpanContext::new(
                    TraceId::from(link.trace_id as u128),
                    SpanId::from(link.span_id),
                    TraceFlags::new(link.flags.unwrap_or_default() as u8),
                    false, // TODO: dd SpanLink doesn't have the remote field...
                    link.tracestate
                        .as_ref()
                        .map(|ts| TraceState::from_str(ts).unwrap_or_default())
                        .unwrap_or_default(),
                );

                let attributes = match &link.attributes {
                    Some(attributes) => attributes
                        .iter()
                        .map(|(key, value)| KeyValue::new(key.clone(), value.clone()))
                        .collect(),
                    None => vec![],
                };

                span.add_link(link_ctx, attributes);
            });
        }
    }

    /// If [`Trace`] contains origin, tags or sampling_decision add them as attributes of the root
    /// span
    fn add_trace_propagation_data(&self, mut trace: Trace) -> Vec<SpanData> {
        let propagation_data = trace.propagation_data;
        let origin = propagation_data.origin.unwrap_or_default();

        for span in trace.finished_spans.iter_mut() {
            if span.span_context.span_id().to_bytes() == trace.local_root_span_id {
                if let Some(ref tags) = propagation_data.tags {
                    tags.iter().for_each(|(key, value)| {
                        span.attributes
                            .push(KeyValue::new(key.clone(), value.clone()))
                    });
                }
            }

            if !origin.is_empty() {
                span.attributes
                    .push(KeyValue::new("_dd.origin", origin.clone()));
            }

            // TODO: is this correct? What if _sampling_priority_v1 or _dd.p.dm were extracted?
            // they shouldn't be overridden
            if let Some(priority) = propagation_data.sampling_decision.priority {
                span.attributes.push(KeyValue::new(
                    "_sampling_priority_v1",
                    priority.into_i8() as i64,
                ));
            }

            if let Some(mechanism) = propagation_data.sampling_decision.mechanism {
                span.attributes.push(KeyValue::new(
                    SAMPLING_DECISION_MAKER_TAG_KEY,
                    mechanism.to_cow(),
                ));
            }
        }

        trace.finished_spans
    }
}

impl opentelemetry_sdk::trace::SpanProcessor for DatadogSpanProcessor {
    fn on_start(
        &self,
        span: &mut opentelemetry_sdk::trace::Span,
        parent_ctx: &opentelemetry::Context,
    ) {
        if !self.config.enabled() || !span.is_recording() || !span.span_context().is_valid() {
            return;
        }

        let trace_id = span.span_context().trace_id().to_bytes();
        let span_id = span.span_context().span_id().to_bytes();

        if parent_ctx.span().span_context().is_remote() {
            self.add_remote_links(span, parent_ctx);
            self.registry.register_local_root_span(trace_id, span_id);
        } else if !parent_ctx.has_active_span() {
            self.registry.register_local_root_span(trace_id, span_id);
        } else {
            self.registry
                .register_span(trace_id, span_id, EMPTY_PROPAGATION_DATA);
        }
    }

    fn on_end(&self, span: SpanData) {
        let trace_id = span.span_context.trace_id().to_bytes();

        let Some(mut trace) = self.registry.finish_span(trace_id, span) else {
            return;
        };

        if !self.config.enabled() {
            return;
        }

        if self.config.trace_partial_flush_enabled() {
            // TODO(paullgdc):
            // This is wrong, we should go over all span to find who has a different service name
            // than their parent yet this is complex to implement as there are cases
            // where we can't read the parent before finishing the child...
            // This tags only the local root as top level which is good enough in a lot of cases
            // though To make partial flushing enabled by default we should fix this
            // behaviour.
            let root_span = trace
                .finished_spans
                .iter_mut()
                .find(|s| s.span_context.span_id().to_bytes() == trace.local_root_span_id);
            if let Some(root_span) = root_span {
                root_span.attributes.push(KeyValue::new("_top_level", 1.0));
            }
        }

        // Add propagation data before exporting the trace
        let trace_chunk = self.add_trace_propagation_data(trace);
        if let Err(e) = self.span_exporter.export_chunk_no_wait(trace_chunk) {
            dd_error!(
                "DatadogSpanProcessor.on_end message='Failed to export trace chunk' error='{e}'",
            );
        }
    }

    fn force_flush(&self) -> opentelemetry_sdk::error::OTelSdkResult {
        self.span_exporter.force_flush()
    }

    fn shutdown_with_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> opentelemetry_sdk::error::OTelSdkResult {
        let deadline = std::time::Instant::now() + timeout;

        // Trigger all tasks shutdown
        self.span_exporter.trigger_shutdown();
        if let Some(rc_client_handle) = &self.rc_client_handle {
            rc_client_handle.trigger_shutdown();
        };
        if let Some(telemetry_metrics_handle) = &self.telemetry_metrics_handle {
            telemetry_metrics_handle.trigger_shutdown();
        }

        // Wait fot all tasks to finish, keeping in mind how much time is left
        // since the beginning of the call
        let left = deadline.saturating_duration_since(std::time::Instant::now());
        self.span_exporter
            .wait_for_shutdown(left)
            .map_err(|e| match e {
                opentelemetry_sdk::error::OTelSdkError::Timeout(_) => {
                    opentelemetry_sdk::error::OTelSdkError::Timeout(timeout)
                }
                _ => e,
            })?;

        if let Some(rc_client_handle) = &self.rc_client_handle {
            let left = deadline.saturating_duration_since(std::time::Instant::now());
            rc_client_handle
                .wait_for_shutdown(left)
                .map_err(|e| match e {
                    RemoteConfigClientError::HandleMutexPoisoned
                    | RemoteConfigClientError::WorkerPanicked(_)
                    | RemoteConfigClientError::InvalidAgentUri => {
                        opentelemetry_sdk::error::OTelSdkError::InternalFailure(format!(
                            "RemoteConfigClient.shutdown_with_timeout: {}",
                            e
                        ))
                    }
                    RemoteConfigClientError::ShutdownTimedOut => {
                        opentelemetry_sdk::error::OTelSdkError::Timeout(timeout)
                    }
                })?;
        }

        if let Some(telemetry_metrics_handle) = &self.telemetry_metrics_handle {
            let left = deadline.saturating_duration_since(std::time::Instant::now());
            telemetry_metrics_handle
                .wait_for_shutdown(left)
                .map_err(|e| match e {
                    WorkerError::ShutdownTimedOut => {
                        opentelemetry_sdk::error::OTelSdkError::Timeout(timeout)
                    }
                    WorkerError::HandleMutexPoisoned | WorkerError::WorkerPanicked(_) => {
                        opentelemetry_sdk::error::OTelSdkError::InternalFailure(format!(
                            "TelemetryMetricsCollector.shutdown_with_timeout: {}",
                            e
                        ))
                    }
                })?;
        }
        Ok(())
    }

    fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
        let dd_resource = create_dd_resource(resource.clone(), &self.config);
        if let Err(e) = self.span_exporter.set_resource(dd_resource.clone()) {
            dd_error!(
                "DatadogSpanProcessor.set_resource message='Failed to set resource' error='{e}'",
            );
        }
        // set the shared resource in the DatadogSpanProcessor
        *self.resource.write().unwrap() = dd_resource.clone();

        // update config's service name and init telemetry once service name has been resolved
        let service_name = dd_resource
            .get(&Key::from_static_str(SERVICE_NAME))
            .map(|service_name| service_name.as_str().to_string());
        self.config.update_service_name(service_name);

        init_telemetry(&self.config);
    }
}

#[cfg(test)]
mod tests {
    use std::{
        borrow::Cow,
        collections::HashMap,
        hint::black_box,
        sync::{Arc, RwLock},
        thread,
        time::Duration,
    };

    use crate::core::{
        configuration::Config,
        sampling::{mechanism, priority, SamplingDecision},
    };
    use opentelemetry::{
        trace::{SpanContext, TraceFlags},
        SpanId, TraceId, {Key, KeyValue, Value},
    };
    use opentelemetry_sdk::{
        trace::{SpanData, SpanEvents, SpanLinks, SpanProcessor},
        Resource,
    };

    use crate::span_processor::{
        DatadogSpanProcessor, TracePropagationData, TraceRegistry, EMPTY_PROPAGATION_DATA,
    };

    #[test]
    fn test_set_resource_from_empty_dd_config() {
        let config = Config::builder().build();

        let registry = TraceRegistry::new(Arc::new(config.clone()));
        let resource = Arc::new(RwLock::new(Resource::builder_empty().build()));

        let mut processor =
            DatadogSpanProcessor::new(Arc::new(config), registry, resource.clone(), None);

        let otel_resource = Resource::builder()
            // .with_service_name("otel-service")
            .with_attribute(KeyValue::new("key1", "value1"))
            .build();

        processor.set_resource(&otel_resource);

        let dd_resource = resource.read().unwrap();
        assert_eq!(
            dd_resource.get(&Key::from_static_str("service.name")),
            Some(Value::String("unnamed-rust-service".into()))
        );
        assert_eq!(
            dd_resource.get(&Key::from_static_str("key1")),
            Some(Value::String("value1".into()))
        );
    }

    #[test]
    fn test_set_resource_from_dd_config() {
        let mut builder = Config::builder();
        builder.set_service("test-service".to_string());
        let config = builder.build();

        let registry = TraceRegistry::new(Arc::new(config.clone()));
        let resource = Arc::new(RwLock::new(Resource::builder_empty().build()));

        let mut processor =
            DatadogSpanProcessor::new(Arc::new(config), registry, resource.clone(), None);

        let attributes = [KeyValue::new("key_schema", "value_schema")];

        let otel_resource = Resource::builder_empty()
            //.with_service_name("otel-service")
            .with_attribute(KeyValue::new("key1", "value1"))
            .with_schema_url(attributes, "schema_url")
            .build();

        processor.set_resource(&otel_resource);

        let dd_resource = resource.read().unwrap();
        assert_eq!(
            dd_resource.get(&Key::from_static_str("service.name")),
            Some(Value::String("test-service".into()))
        );
        assert_eq!(
            dd_resource.get(&Key::from_static_str("key1")),
            Some(Value::String("value1".into()))
        );
        assert_eq!(
            dd_resource.get(&Key::from_static_str("key_schema")),
            Some(Value::String("value_schema".into()))
        );

        assert_eq!(dd_resource.schema_url(), Some("schema_url"));
    }

    #[test]
    fn test_set_resource_empty_builder_from_dd_config() {
        let mut builder = Config::builder();
        builder.set_service("test-service".to_string());
        let config = builder.build();

        let registry = TraceRegistry::new(Arc::new(config.clone()));
        let resource = Arc::new(RwLock::new(Resource::builder_empty().build()));

        let mut processor =
            DatadogSpanProcessor::new(Arc::new(config), registry, resource.clone(), None);

        let otel_resource = Resource::builder_empty()
            .with_attribute(KeyValue::new("key1", "value1"))
            .build();

        processor.set_resource(&otel_resource);

        let dd_resource = resource.read().unwrap();
        assert_eq!(
            dd_resource.get(&Key::from_static_str("service.name")),
            Some(Value::String("test-service".into()))
        );
        assert_eq!(
            dd_resource.get(&Key::from_static_str("key1")),
            Some(Value::String("value1".into()))
        );
    }

    #[test]
    fn test_dd_config_non_default_service() {
        let mut builder = Config::builder();
        builder.set_service("test-service".to_string());
        let config = builder.build();

        let registry = TraceRegistry::new(Arc::new(config.clone()));
        let resource = Arc::new(RwLock::new(Resource::builder_empty().build()));

        let mut processor =
            DatadogSpanProcessor::new(Arc::new(config), registry, resource.clone(), None);

        let otel_resource = Resource::builder()
            .with_service_name("otel-service")
            .build();

        processor.set_resource(&otel_resource);

        let dd_resource = resource.read().unwrap();
        assert_eq!(
            dd_resource.get(&Key::from_static_str("service.name")),
            Some(Value::String("test-service".into()))
        );
    }

    #[test]
    fn test_dd_config_default_service() {
        let config = Config::builder().build();

        let registry = TraceRegistry::new(Arc::new(config.clone()));
        let resource = Arc::new(RwLock::new(Resource::builder_empty().build()));

        let mut processor =
            DatadogSpanProcessor::new(Arc::new(config), registry, resource.clone(), None);

        let otel_resource = Resource::builder()
            .with_service_name("otel-service")
            .build();

        processor.set_resource(&otel_resource);

        let dd_resource = resource.read().unwrap();
        assert_eq!(
            dd_resource.get(&Key::from_static_str("service.name")),
            Some(Value::String("otel-service".into()))
        );
    }

    fn bench_trace_registry(c: &mut criterion::Criterion) {
        const ITERATIONS: u32 = 10000;
        const NUM_TRACES: usize = ITERATIONS as usize / 20;
        let mut group = c.benchmark_group("trace_registry_concurrent_access_threads");
        group
            .warm_up_time(Duration::from_millis(100))
            .measurement_time(Duration::from_millis(1000));

        for concurrency in [1, 2, 4, 8, 16, 32] {
            group
                .throughput(criterion::Throughput::Elements(
                    ITERATIONS as u64 * concurrency,
                ))
                .bench_function(
                    criterion::BenchmarkId::from_parameter(concurrency),
                    move |g| {
                        let trace_ids: Vec<_> = (0..concurrency)
                            .map(|thread| {
                                std::array::from_fn::<_, NUM_TRACES, _>(|i| {
                                    ((thread << 16 | i as u64) as u128).to_be_bytes()
                                })
                            })
                            .collect();
                        g.iter_batched_ref(
                            {
                                let trace_ids = trace_ids.clone();
                                move || {
                                    let tr: TraceRegistry =
                                        TraceRegistry::new(Arc::new(Config::builder().build()));
                                    for trace_id in trace_ids.iter().flatten() {
                                        tr.register_local_root_trace_propagation_data(
                                            *trace_id,
                                            TracePropagationData {
                                                sampling_decision: SamplingDecision {
                                                    priority: Some(priority::AUTO_KEEP),
                                                    mechanism: Some(mechanism::DEFAULT),
                                                },
                                                origin: Some("rum".to_string()),
                                                tags: Some(HashMap::from_iter([(
                                                    "dd.p.tid".to_string(),
                                                    "foobar".to_string(),
                                                )])),
                                            },
                                        );
                                    }
                                    tr
                                }
                            },
                            move |tr| {
                                let tr = &*tr;
                                let trace_ids = &trace_ids;
                                thread::scope(move |s| {
                                    for trace_id in trace_ids {
                                        s.spawn(move || {
                                            for _ in 0..(ITERATIONS as usize / NUM_TRACES) {
                                                for trace_id in trace_id {
                                                    black_box(tr.get_trace_propagation_data(
                                                        black_box(*trace_id),
                                                    ));
                                                }
                                            }
                                        });
                                    }
                                })
                            },
                            criterion::BatchSize::LargeInput,
                        );
                    },
                );
        }
    }

    #[test]
    fn bench() {
        // Run with
        // `cargo test --profile bench -- --nocapture bench -- <benchmark_filter>
        // Collect cli arguments

        // Interpret sequence of args `[ "...bench", "--", "[filter]" ]` as a trigger and extract
        // `filter`
        let filter = std::env::args()
            .collect::<Vec<_>>()
            .windows(3)
            .filter(|p| p.len() >= 2 && p[0].ends_with("bench") && p[1] == "--")
            .map(|s| s.get(2).unwrap_or(&"".to_string()).clone())
            .next();

        let filter = match filter {
            None => return,
            Some(f) => f,
        };

        let mut criterion = criterion::Criterion::default()
            .with_output_color(true)
            .with_filter(&filter);
        bench_trace_registry(&mut criterion);

        criterion.final_summary();
    }

    fn create_test_span_data(trace_id: [u8; 16], span_id: [u8; 8]) -> SpanData {
        SpanData {
            span_context: SpanContext::new(
                TraceId::from_bytes(trace_id),
                SpanId::from_bytes(span_id),
                TraceFlags::default(),
                false,
                Default::default(),
            ),
            parent_span_id: SpanId::INVALID,
            span_kind: opentelemetry::trace::SpanKind::Internal,
            name: Cow::Borrowed("test_span"),
            start_time: std::time::SystemTime::now(),
            end_time: std::time::SystemTime::now(),
            attributes: Vec::new(),
            dropped_attributes_count: 0,
            events: SpanEvents::default(),
            links: SpanLinks::default(),
            status: opentelemetry::trace::Status::Unset,
            instrumentation_scope: Default::default(),
            parent_span_is_remote: false,
        }
    }

    #[test]
    fn test_stats_single_span_trace() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [1u8; 16];
        let span_id = [1u8; 8];

        // Register and finish a single span
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, span_id);

        let span_data = create_test_span_data(trace_id, span_id);
        registry.finish_span(trace_id, span_data);

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 1);
        assert_eq!(stats.spans_finished, 1);
        assert_eq!(stats.trace_segments_created, 1);
        assert_eq!(stats.trace_segments_closed, 1);
    }

    #[test]
    fn test_stats_multiple_spans_single_trace() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [2u8; 16];
        let root_span_id = [1u8; 8];
        let child1_span_id = [2u8; 8];
        let child2_span_id = [3u8; 8];

        // Register root span
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);

        // Register child spans
        registry.register_span(trace_id, child1_span_id, EMPTY_PROPAGATION_DATA);
        registry.register_span(trace_id, child2_span_id, EMPTY_PROPAGATION_DATA);

        // Finish all spans
        let root_span = create_test_span_data(trace_id, root_span_id);
        let child1_span = create_test_span_data(trace_id, child1_span_id);
        let child2_span = create_test_span_data(trace_id, child2_span_id);

        assert!(
            registry.finish_span(trace_id, child1_span).is_none(),
            "Should not flush incomplete trace"
        );
        assert!(
            registry.finish_span(trace_id, child2_span).is_none(),
            "Should not flush incomplete trace"
        );
        assert!(
            registry.finish_span(trace_id, root_span).is_some(),
            "Should flush complete trace"
        );

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 3);
        assert_eq!(stats.spans_finished, 3);
        assert_eq!(stats.trace_segments_created, 1);
        assert_eq!(stats.trace_segments_closed, 1);
    }

    #[test]
    fn test_stats_multiple_independent_traces() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));

        // Create 3 independent traces
        for i in 1..=3 {
            let trace_id = [i; 16];
            let span_id = [i; 8];

            registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
            registry.register_local_root_span(trace_id, span_id);

            let span_data = create_test_span_data(trace_id, span_id);
            registry.finish_span(trace_id, span_data);
        }

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 3, "Expected 3 spans created");
        assert_eq!(stats.spans_finished, 3, "Expected 3 spans finished");
        assert_eq!(
            stats.trace_segments_created, 3,
            "Expected 3 trace segments created"
        );
        assert_eq!(
            stats.trace_segments_closed, 3,
            "Expected 3 trace segments closed"
        );
    }

    #[test]
    fn test_stats_unfinished_trace() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [4u8; 16];
        let root_span_id = [1u8; 8];
        let child_span_id = [2u8; 8];

        // Register root and child spans
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);
        registry.register_span(trace_id, child_span_id, EMPTY_PROPAGATION_DATA);

        // Only finish the root span
        let root_span = create_test_span_data(trace_id, root_span_id);
        assert!(
            registry.finish_span(trace_id, root_span).is_none(),
            "Should not flush incomplete trace"
        );

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 2);
        assert_eq!(stats.spans_finished, 1);
        assert_eq!(stats.trace_segments_created, 1);
        assert_eq!(stats.trace_segments_closed, 0);
    }

    #[test]
    fn test_stats_orphaned_span() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [5u8; 16];
        let span_id = [1u8; 8];

        // Finish a span without registering it first
        let span_data = create_test_span_data(trace_id, span_id);
        assert!(
            registry.finish_span(trace_id, span_data).is_some(),
            "Should flush orphaned span immediately"
        );

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 0);
        assert_eq!(stats.spans_finished, 1);
        assert_eq!(stats.trace_segments_created, 1);
        assert_eq!(stats.trace_segments_closed, 1);
    }

    #[test]
    fn test_stats_reset_after_get() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [6u8; 16];
        let span_id = [1u8; 8];

        // Create and finish a trace
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, span_id);
        let span_data = create_test_span_data(trace_id, span_id);
        registry.finish_span(trace_id, span_data);

        // Get stats (should reset them)
        let stats1 = registry.get_metrics();
        assert_eq!(stats1.spans_created, 1);

        // Get stats again (should be zero)
        let stats2 = registry.get_metrics();
        assert_eq!(stats2.spans_created, 0);
        assert_eq!(stats2.spans_finished, 0);
        assert_eq!(stats2.trace_segments_created, 0);
        assert_eq!(stats2.trace_segments_closed, 0);
    }

    #[test]
    fn test_stats_across_multiple_shards() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));

        // Create traces that will likely hit different shards
        let num_traces = 100;
        for i in 0..num_traces {
            let trace_id = (i as u128).to_be_bytes();
            let span_id = [i as u8; 8];

            registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
            registry.register_local_root_span(trace_id, span_id);

            let span_data = create_test_span_data(trace_id, span_id);
            registry.finish_span(trace_id, span_data);
        }

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, num_traces);
        assert_eq!(stats.spans_finished, num_traces);
        assert_eq!(stats.trace_segments_created, num_traces);
        assert_eq!(stats.trace_segments_closed, num_traces);
    }

    #[test]
    fn test_stats_complex_trace_hierarchy() {
        let registry = TraceRegistry::new(Arc::new(Config::builder().build()));
        let trace_id = [7u8; 16];
        let root_span_id = [1u8; 8];

        // Register root
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);

        // Register 5 child spans
        for i in 2..=6 {
            let child_span_id = [i; 8];
            registry.register_span(trace_id, child_span_id, EMPTY_PROPAGATION_DATA);
        }

        // Finish all spans
        for i in 1..=6 {
            let span_id = [i; 8];
            let span_data = create_test_span_data(trace_id, span_id);
            let result = registry.finish_span(trace_id, span_data);
            if i == 6 {
                assert!(result.is_some(), "Should flush after last span");
            } else {
                assert!(
                    result.is_none(),
                    "Should not flush before all spans complete"
                );
            }
        }

        let stats = registry.get_metrics();
        assert_eq!(stats.spans_created, 6);
        assert_eq!(stats.spans_finished, 6);
        assert_eq!(stats.trace_segments_created, 1)
    }

    #[test]
    fn test_partial_flush_disabled_by_default() {
        let config = Config::builder().build();
        let registry = TraceRegistry::new(Arc::new(config));
        let trace_id = [8u8; 16];
        let root_span_id = [1u8; 8];

        // Register root span
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);

        // Register and finish more than default min_spans
        for i in 2..=400 {
            let child_span_id = [0, 0, 0, 0, 0, 0, (i / 256) as u8, i as u8];
            registry.register_span(trace_id, child_span_id, EMPTY_PROPAGATION_DATA);
            let span_data = create_test_span_data(trace_id, child_span_id);

            // With partial flushing disabled, no trace should be flushed until all spans are done
            let result = registry.finish_span(trace_id, span_data);
            assert!(
                result.is_none(),
                "Should not flush until all spans are done (disabled by default)"
            );
        }

        // Finish root span - now it should flush
        let root_span = create_test_span_data(trace_id, root_span_id);
        let result = registry.finish_span(trace_id, root_span);
        assert!(result.is_some(), "Should flush after all spans are done");
        let trace = result.unwrap();
        assert_eq!(trace.finished_spans.len(), 400);
        let metrics = registry.get_metrics();
        assert_eq!(metrics.trace_partial_flush_count, 0);
        assert_eq!(metrics.trace_segments_closed, 1);
    }

    #[test]
    fn test_partial_flush_enabled_min_spans() {
        let config = Config::builder()
            .set_trace_partial_flush_enabled(true)
            .set_trace_partial_flush_min_spans(10)
            .build();
        let registry = TraceRegistry::new(Arc::new(config));
        let trace_id = [9u8; 16];
        let root_span_id = [1u8; 8];

        // Register root span
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);

        // Register 15 child spans
        for i in 2..=16 {
            let child_span_id = [i; 8];
            registry.register_span(trace_id, child_span_id, EMPTY_PROPAGATION_DATA);
        }

        // Finish 9 spans (below threshold)
        for i in 2..=10 {
            let span_data = create_test_span_data(trace_id, [i; 8]);
            let result = registry.finish_span(trace_id, span_data);
            assert!(
                result.is_none(),
                "Should not flush until min_spans threshold is reached"
            );
        }

        // Finish the 10th span (reaches threshold)
        let span_data = create_test_span_data(trace_id, [11; 8]);
        let result = registry.finish_span(trace_id, span_data);
        assert!(
            result.is_some(),
            "Should flush when min_spans threshold is reached"
        );
        let trace = result.unwrap();
        let metrics = registry.get_metrics();
        assert_eq!(metrics.trace_partial_flush_count, 1);
        assert_eq!(metrics.trace_segments_closed, 0);

        assert_eq!(trace.finished_spans.len(), 10);
        assert_eq!(
            trace.open_span_count, 6,
            "Should have 6 open spans remaining (root + 5 children)"
        );
    }

    #[test]
    fn test_partial_flush_multiple_flushes() {
        let config = Config::builder()
            .set_trace_partial_flush_enabled(true)
            .set_trace_partial_flush_min_spans(5)
            .build();
        let registry = TraceRegistry::new(Arc::new(config));
        let trace_id = [10u8; 16];
        let root_span_id = [1u8; 8];

        // Register root span
        registry.register_local_root_trace_propagation_data(trace_id, EMPTY_PROPAGATION_DATA);
        registry.register_local_root_span(trace_id, root_span_id);

        // Register 20 child spans
        for i in 2..=21 {
            let child_span_id = [i; 8];
            registry.register_span(trace_id, child_span_id, EMPTY_PROPAGATION_DATA);
        }

        let mut total_flushed = 0;
        let mut flush_count = 0;

        // Finish all child spans
        for i in 2..=21 {
            let span_data = create_test_span_data(trace_id, [i; 8]);
            if let Some(trace) = registry.finish_span(trace_id, span_data) {
                total_flushed += trace.finished_spans.len();
                flush_count += 1;
            }
        }

        // Should have multiple partial flushes
        assert_eq!(flush_count, 4, "Should have 4 partial flushes");
        assert_eq!(total_flushed, 20, "Should have flushed all 20 child spans");
        let metrics = registry.get_metrics();
        assert_eq!(metrics.trace_partial_flush_count, 4);
        assert_eq!(metrics.trace_segments_closed, 0);

        // Finish root span - final flush
        let root_span = create_test_span_data(trace_id, root_span_id);
        let result = registry.finish_span(trace_id, root_span);
        assert!(result.is_some(), "Should flush root span");
        let trace = result.unwrap();
        assert_eq!(trace.finished_spans.len(), 1);
        assert_eq!(trace.open_span_count, 0);
        let metrics = registry.get_metrics();
        assert_eq!(
            metrics.trace_partial_flush_count, 0,
            "Last flush is a complete one"
        );
        assert_eq!(metrics.trace_segments_closed, 1);
    }
}