fastrace 0.7.18

A high-performance timeline tracing library for Rust
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
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

use std::cell::Cell;
use std::fmt;
use std::rc::Rc;
use std::str::FromStr;

use crate::Span;
use crate::local::local_span_stack::LOCAL_SPAN_STACK;

thread_local! {
    static LOCAL_ID_GENERATOR: Cell<(u32, u32)> = Cell::new((rand::random(), 0))
}

/// An identifier for a trace, which groups a set of related spans together.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct TraceId(pub u128);

impl TraceId {
    /// Create a random `TraceId`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let trace_id = TraceId::random();
    /// ```
    pub fn random() -> Self {
        TraceId(rand::random())
    }
}

impl fmt::Display for TraceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:032x}", self.0)
    }
}

impl FromStr for TraceId {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        u128::from_str_radix(s, 16).map(TraceId)
    }
}

impl serde::Serialize for TraceId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&format!("{:032x}", self.0))
    }
}

impl<'de> serde::Deserialize<'de> for TraceId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        u128::from_str_radix(&s, 16)
            .map(TraceId)
            .map_err(serde::de::Error::custom)
    }
}

/// An identifier for a span within a trace.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SpanId(pub u64);

impl SpanId {
    /// Create a random `SpanId`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span_id = SpanId::random();
    /// ```
    pub fn random() -> Self {
        SpanId(rand::random())
    }

    #[inline]
    #[doc(hidden)]
    /// Create a non-zero `SpanId`
    pub fn next_id() -> SpanId {
        LOCAL_ID_GENERATOR
            .try_with(|g| {
                let (prefix, mut suffix) = g.get();

                suffix = suffix.wrapping_add(1);

                g.set((prefix, suffix));

                SpanId(((prefix as u64) << 32) | (suffix as u64))
            })
            .unwrap_or_else(|_| SpanId(rand::random()))
    }
}

impl fmt::Display for SpanId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:016x}", self.0)
    }
}

impl FromStr for SpanId {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        u64::from_str_radix(s, 16).map(SpanId)
    }
}

impl serde::Serialize for SpanId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&format!("{:016x}", self.0))
    }
}

impl<'de> serde::Deserialize<'de> for SpanId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        u64::from_str_radix(&s, 16)
            .map(SpanId)
            .map_err(serde::de::Error::custom)
    }
}

/// A struct representing the context of a span, including its [`TraceId`] and [`SpanId`].
///
/// [`TraceId`]: crate::collector::TraceId
/// [`SpanId`]: crate::collector::SpanId
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SpanContext {
    pub trace_id: TraceId,
    pub span_id: SpanId,
    pub sampled: bool,
}

impl SpanContext {
    /// Creates a new `SpanContext` with the given [`TraceId`] and [`SpanId`].
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span_context = SpanContext::new(TraceId(12), SpanId::default());
    /// ```
    ///
    /// [`TraceId`]: crate::collector::TraceId
    /// [`SpanId`]: crate::collector::SpanId
    pub fn new(trace_id: TraceId, span_id: SpanId) -> Self {
        Self {
            trace_id,
            span_id,
            sampled: true,
        }
    }

    /// Create a new `SpanContext` with a random trace id.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let root = Span::root("root", SpanContext::random());
    /// ```
    pub fn random() -> Self {
        Self {
            trace_id: TraceId::random(),
            span_id: SpanId(0),
            sampled: true,
        }
    }

    /// Sets the `sampled` flag of the `SpanContext`.
    ///
    /// When the `sampled` flag is `false`, the spans will not be collected, but the parent-child
    /// relationship will still be maintained and the `SpanContext` can still be propagated.
    ///
    /// The default value is `true`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span_context = SpanContext::new(TraceId(12), SpanId(34)).sampled(false);
    /// ```
    pub fn sampled(mut self, sampled: bool) -> Self {
        self.sampled = sampled;
        self
    }

    /// Creates a `SpanContext` from the given [`Span`]. If the `Span` is a noop span,
    /// this function will return `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span = Span::root("root", SpanContext::random());
    /// let span_context = SpanContext::from_span(&span);
    /// ```
    ///
    /// [`Span`]: crate::Span
    pub fn from_span(span: &Span) -> Option<Self> {
        #[cfg(not(feature = "enable"))]
        {
            None
        }

        #[cfg(feature = "enable")]
        {
            let inner = span.inner.as_ref()?;
            let collect_token = inner.issue_collect_token();

            Some(Self {
                trace_id: collect_token.trace_id,
                span_id: collect_token.parent_id,
                sampled: collect_token.is_sampled,
            })
        }
    }

    /// Creates a `SpanContext` from the current local parent span. If there is no
    /// local parent span, this function will return `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span = Span::root("root", SpanContext::random());
    /// let _guard = span.set_local_parent();
    ///
    /// let span_context = SpanContext::current_local_parent();
    /// ```
    pub fn current_local_parent() -> Option<Self> {
        #[cfg(not(feature = "enable"))]
        {
            None
        }

        #[cfg(feature = "enable")]
        {
            let stack = LOCAL_SPAN_STACK.try_with(Rc::clone).ok()?;

            let mut stack = stack.borrow_mut();
            let collect_token = stack.current_collect_token()?;

            Some(Self {
                trace_id: collect_token.trace_id,
                span_id: collect_token.parent_id,
                sampled: collect_token.is_sampled,
            })
        }
    }

    /// Decodes the `SpanContext` from a [W3C Trace Context](https://www.w3.org/TR/trace-context/)
    /// `traceparent` header string.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span_context = SpanContext::decode_w3c_traceparent(
    ///     "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
    /// )
    /// .unwrap();
    ///
    /// assert_eq!(
    ///     span_context.trace_id,
    ///     TraceId(0x0af7651916cd43dd8448eb211c80319c)
    /// );
    /// assert_eq!(span_context.span_id, SpanId(0xb7ad6b7169203331));
    /// ```
    pub fn decode_w3c_traceparent(traceparent: &str) -> Option<Self> {
        let mut parts = traceparent.split('-');

        match (
            parts.next(),
            parts.next(),
            parts.next(),
            parts.next(),
            parts.next(),
        ) {
            (Some("00"), Some(trace_id), Some(span_id), Some(sampled), None) => {
                let trace_id = u128::from_str_radix(trace_id, 16).ok()?;
                let span_id = u64::from_str_radix(span_id, 16).ok()?;
                let sampled = u8::from_str_radix(sampled, 16).ok()? & 1 == 1;
                if trace_id == 0 || span_id == 0 {
                    return None;
                }
                Some(Self::new(TraceId(trace_id), SpanId(span_id)).sampled(sampled))
            }
            _ => None,
        }
    }

    /// Encodes the `SpanContext` into a [W3C Trace Context](https://www.w3.org/TR/trace-context/)
    /// `traceparent` header string.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::prelude::*;
    ///
    /// let span_context = SpanContext::new(TraceId(12), SpanId(34));
    /// let traceparent = span_context.encode_w3c_traceparent();
    ///
    /// assert_eq!(
    ///     traceparent,
    ///     "00-0000000000000000000000000000000c-0000000000000022-01"
    /// );
    /// ```
    pub fn encode_w3c_traceparent(&self) -> String {
        format!(
            "00-{:032x}-{:016x}-{:02x}",
            self.trace_id.0, self.span_id.0, self.sampled as u8,
        )
    }

    /// Encodes the `SpanContext` as a [W3C Trace Context](https://www.w3.org/TR/trace-context/)
    /// `traceparent` header string with a sampled flag.
    #[deprecated(since = "0.7.0", note = "Please use `SpanContext::sampled()` instead")]
    pub fn encode_w3c_traceparent_with_sampled(&self, sampled: bool) -> String {
        self.sampled(sampled).encode_w3c_traceparent()
    }
}

impl Default for SpanContext {
    fn default() -> Self {
        Self::random()
    }
}

impl serde::Serialize for SpanContext {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.encode_w3c_traceparent().serialize(serializer)
    }
}

impl<'de> serde::Deserialize<'de> for SpanContext {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        SpanContext::decode_w3c_traceparent(&s)
            .ok_or_else(|| serde::de::Error::custom("invalid w3c traceparent"))
    }
}

/// A complete [W3C Trace Context](https://www.w3.org/TR/trace-context/) representation
/// carrying both the `traceparent` and `tracestate` headers.
///
/// [`SpanContext`] is `Copy` and only stores trace-id, span-id, and the sampled flag
/// (the `traceparent` portion). `W3CTraceContext` extends it with an optional
/// `tracestate` string so that vendor-specific key-value pairs survive propagation
/// across process boundaries.
///
/// **Important:** `W3CTraceContext` is a **boundary/header wrapper** for encoding and
/// decoding W3C headers at RPC injection/extraction points. The `tracestate` field is
/// **not** carried through fastrace's internal span machinery — converting to a
/// [`SpanContext`] (e.g. via [`Span::root`] or field access) discards the tracestate.
/// **If you extract the `span_context`, create spans, and later need to inject outgoing
/// headers via `SpanContext::from_span` or `SpanContext::current_local_parent()`, the
/// original `tracestate` will be lost.** Store the `W3CTraceContext` or `tracestate`
/// value separately if you need to preserve it for outbound propagation.
///
/// # Examples
///
/// ```
/// use fastrace::collector::W3CTraceContext;
/// use fastrace::prelude::*;
///
/// // Decode from incoming HTTP headers.
/// let ctx = W3CTraceContext::decode(
///     "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
///     Some("rw=frontend,congo=t61rcWkgMzE"),
/// )
/// .unwrap();
///
/// assert_eq!(
///     ctx.span_context.trace_id,
///     TraceId(0x0af7651916cd43dd8448eb211c80319c)
/// );
/// assert_eq!(ctx.tracestate(), Some("rw=frontend,congo=t61rcWkgMzE"));
///
/// // Encode for outgoing HTTP headers.
/// let traceparent = ctx.encode_traceparent();
/// let tracestate = ctx.encode_tracestate();
/// assert_eq!(
///     traceparent,
///     "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
/// );
/// assert_eq!(tracestate, Some("rw=frontend,congo=t61rcWkgMzE"));
///
/// // Start a root span (note: tracestate is not retained in the span).
/// let root = Span::root("server", ctx.span_context);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct W3CTraceContext {
    /// The core span context (trace-id, span-id, sampled flag).
    pub span_context: SpanContext,
    tracestate: Option<String>,
}

impl W3CTraceContext {
    /// Creates a new `W3CTraceContext` from a [`SpanContext`] with no `tracestate`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::collector::W3CTraceContext;
    /// use fastrace::prelude::*;
    ///
    /// let ctx = W3CTraceContext::new(SpanContext::new(TraceId(12), SpanId(34)));
    /// assert_eq!(ctx.tracestate(), None);
    /// ```
    pub fn new(span_context: SpanContext) -> Self {
        Self {
            span_context,
            tracestate: None,
        }
    }

    /// Attaches a `tracestate` string. Replaces any existing value.
    ///
    /// An empty string is normalized to `None` (no tracestate), consistent
    /// with [`W3CTraceContext::decode`].
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::collector::W3CTraceContext;
    /// use fastrace::prelude::*;
    ///
    /// let ctx = W3CTraceContext::new(SpanContext::random()).with_tracestate("rw=frontend");
    /// assert_eq!(ctx.tracestate(), Some("rw=frontend"));
    ///
    /// // Empty string is normalized to None.
    /// let ctx = ctx.with_tracestate("");
    /// assert_eq!(ctx.tracestate(), None);
    /// ```
    pub fn with_tracestate(mut self, tracestate: impl Into<String>) -> Self {
        let ts = tracestate.into();
        self.tracestate = if ts.is_empty() { None } else { Some(ts) };
        self
    }

    /// Returns the `tracestate` value, if any.
    pub fn tracestate(&self) -> Option<&str> {
        self.tracestate.as_deref()
    }

    /// Encodes the `traceparent` header value.
    ///
    /// This delegates to [`SpanContext::encode_w3c_traceparent`].
    pub fn encode_traceparent(&self) -> String {
        self.span_context.encode_w3c_traceparent()
    }

    /// Returns the `tracestate` header value, if present.
    ///
    /// Returns `None` when no tracestate was set, meaning the header should be
    /// omitted from the outgoing request.
    pub fn encode_tracestate(&self) -> Option<&str> {
        self.tracestate.as_deref()
    }

    /// Decodes a `W3CTraceContext` from `traceparent` and optional `tracestate`
    /// header values.
    ///
    /// Returns `None` if the `traceparent` string is malformed.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::collector::W3CTraceContext;
    /// use fastrace::prelude::*;
    ///
    /// let ctx = W3CTraceContext::decode(
    ///     "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
    ///     Some("rw=frontend"),
    /// )
    /// .unwrap();
    ///
    /// assert_eq!(ctx.tracestate(), Some("rw=frontend"));
    ///
    /// // Without tracestate.
    /// let ctx2 = W3CTraceContext::decode(
    ///     "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
    ///     None,
    /// )
    /// .unwrap();
    /// assert_eq!(ctx2.tracestate(), None);
    /// ```
    pub fn decode(traceparent: &str, tracestate: Option<&str>) -> Option<Self> {
        let span_context = SpanContext::decode_w3c_traceparent(traceparent)?;
        Some(Self {
            span_context,
            tracestate: tracestate.filter(|s| !s.is_empty()).map(|s| s.to_string()),
        })
    }

    /// Encodes both `traceparent` and `tracestate` into a list of header
    /// key-value pairs suitable for HTTP propagation.
    ///
    /// The returned vector always contains the `traceparent` entry. The
    /// `tracestate` entry is included only when a tracestate value is present.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::collector::W3CTraceContext;
    /// use fastrace::prelude::*;
    ///
    /// let ctx = W3CTraceContext::new(SpanContext::new(TraceId(1), SpanId(2)))
    ///     .with_tracestate("rw=frontend");
    /// let headers = ctx.encode_headers();
    /// assert_eq!(headers.len(), 2);
    /// assert_eq!(headers[0].0, "traceparent");
    /// assert_eq!(headers[1].0, "tracestate");
    /// assert_eq!(headers[1].1, "rw=frontend");
    /// ```
    pub fn encode_headers(&self) -> Vec<(String, String)> {
        let mut headers = vec![("traceparent".to_string(), self.encode_traceparent())];
        if let Some(ts) = &self.tracestate {
            headers.push(("tracestate".to_string(), ts.clone()));
        }
        headers
    }

    /// Decodes a `W3CTraceContext` from an iterator of header key-value pairs.
    ///
    /// The lookup is case-insensitive for the header names, per the W3C spec.
    /// **If multiple `tracestate` headers are present, they are joined with commas
    /// in the order encountered, per [W3C Trace Context §3.3.1.1](https://www.w3.org/TR/trace-context/#tracestate-header).**
    /// Empty or whitespace-only `tracestate` values are ignored.
    /// Returns `None` if no valid `traceparent` header is found.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastrace::collector::W3CTraceContext;
    ///
    /// let headers = vec![
    ///     (
    ///         "traceparent",
    ///         "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
    ///     ),
    ///     ("tracestate", "rw=frontend,congo=t61rcWkgMzE"),
    /// ];
    ///
    /// let ctx = W3CTraceContext::decode_headers(headers).unwrap();
    /// assert_eq!(ctx.tracestate(), Some("rw=frontend,congo=t61rcWkgMzE"));
    /// ```
    pub fn decode_headers<'a>(
        headers: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> Option<Self> {
        let mut traceparent = None;
        let mut tracestate_parts: Vec<&str> = Vec::new();

        for (key, value) in headers {
            if key.eq_ignore_ascii_case("traceparent") {
                traceparent = Some(value);
            } else if key.eq_ignore_ascii_case("tracestate") {
                let trimmed = value.trim();
                if !trimmed.is_empty() {
                    tracestate_parts.push(trimmed);
                }
            }
        }

        let tracestate = if tracestate_parts.is_empty() {
            None
        } else {
            Some(tracestate_parts.join(","))
        };

        Self::decode(traceparent?, tracestate.as_deref())
    }
}

impl From<SpanContext> for W3CTraceContext {
    fn from(span_context: SpanContext) -> Self {
        Self::new(span_context)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;

    #[test]
    #[allow(clippy::needless_collect)]
    fn unique_id() {
        let handles = std::iter::repeat_with(|| {
            std::thread::spawn(|| {
                std::iter::repeat_with(SpanId::next_id)
                    .take(1000)
                    .collect::<Vec<_>>()
            })
        })
        .take(32)
        .collect::<Vec<_>>();

        let k = handles
            .into_iter()
            .flat_map(|h| h.join().unwrap())
            .collect::<HashSet<_>>();

        assert_eq!(k.len(), 32 * 1000);
    }

    #[test]
    fn w3c_trace_context_decode_with_tracestate() {
        let ctx = W3CTraceContext::decode(
            "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            Some("rw=frontend,congo=t61rcWkgMzE"),
        )
        .unwrap();

        assert_eq!(
            ctx.span_context.trace_id,
            TraceId(0x0af7651916cd43dd8448eb211c80319c)
        );
        assert_eq!(ctx.span_context.span_id, SpanId(0xb7ad6b7169203331));
        assert!(ctx.span_context.sampled);
        assert_eq!(ctx.tracestate(), Some("rw=frontend,congo=t61rcWkgMzE"));
    }

    #[test]
    fn w3c_trace_context_decode_without_tracestate() {
        let ctx = W3CTraceContext::decode(
            "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            None,
        )
        .unwrap();

        assert_eq!(ctx.tracestate(), None);
    }

    #[test]
    fn w3c_trace_context_decode_empty_tracestate() {
        let ctx = W3CTraceContext::decode(
            "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            Some(""),
        )
        .unwrap();

        assert_eq!(ctx.tracestate(), None);
    }

    #[test]
    fn w3c_trace_context_decode_invalid_traceparent() {
        assert!(W3CTraceContext::decode("invalid", Some("rw=frontend")).is_none());
    }

    #[test]
    fn w3c_trace_context_encode_roundtrip() {
        let original = W3CTraceContext::decode(
            "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            Some("rw=frontend"),
        )
        .unwrap();

        let traceparent = original.encode_traceparent();
        let tracestate = original.encode_tracestate();

        let decoded = W3CTraceContext::decode(&traceparent, tracestate).unwrap();

        assert_eq!(original, decoded);
    }

    #[test]
    fn w3c_trace_context_encode_roundtrip_no_tracestate() {
        let original = W3CTraceContext::decode(
            "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00",
            None,
        )
        .unwrap();

        let traceparent = original.encode_traceparent();
        let tracestate = original.encode_tracestate();

        let decoded = W3CTraceContext::decode(&traceparent, tracestate).unwrap();

        assert_eq!(original, decoded);
        assert!(!decoded.span_context.sampled);
    }

    #[test]
    fn w3c_trace_context_with_tracestate() {
        let ctx = W3CTraceContext::new(SpanContext::new(TraceId(1), SpanId(2)));
        assert_eq!(ctx.tracestate(), None);

        let ctx = ctx.with_tracestate("rw=frontend");
        assert_eq!(ctx.tracestate(), Some("rw=frontend"));

        // Replace existing.
        let ctx = ctx.with_tracestate("rw=backend");
        assert_eq!(ctx.tracestate(), Some("rw=backend"));

        // Empty string normalizes to None.
        let ctx = ctx.with_tracestate("");
        assert_eq!(ctx.tracestate(), None);
    }

    #[test]
    fn w3c_trace_context_encode_headers() {
        let ctx = W3CTraceContext::new(SpanContext::new(TraceId(1), SpanId(2)))
            .with_tracestate("rw=frontend");

        let headers = ctx.encode_headers();
        assert_eq!(headers.len(), 2);
        assert_eq!(headers[0].0, "traceparent");
        assert_eq!(
            headers[0].1,
            "00-00000000000000000000000000000001-0000000000000002-01"
        );
        assert_eq!(headers[1].0, "tracestate");
        assert_eq!(headers[1].1, "rw=frontend");
    }

    #[test]
    fn w3c_trace_context_encode_headers_no_tracestate() {
        let ctx = W3CTraceContext::new(SpanContext::new(TraceId(1), SpanId(2)));
        let headers = ctx.encode_headers();
        assert_eq!(headers.len(), 1);
        assert_eq!(headers[0].0, "traceparent");
    }

    #[test]
    fn w3c_trace_context_decode_headers() {
        let headers = vec![
            (
                "traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            ),
            ("tracestate", "rw=frontend,congo=t61rcWkgMzE"),
        ];

        let ctx = W3CTraceContext::decode_headers(headers).unwrap();
        assert_eq!(
            ctx.span_context.trace_id,
            TraceId(0x0af7651916cd43dd8448eb211c80319c)
        );
        assert_eq!(ctx.tracestate(), Some("rw=frontend,congo=t61rcWkgMzE"));
    }

    #[test]
    fn w3c_trace_context_decode_headers_case_insensitive() {
        let headers = vec![
            (
                "Traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            ),
            ("TraceState", "rw=frontend"),
        ];

        let ctx = W3CTraceContext::decode_headers(headers).unwrap();
        assert_eq!(ctx.tracestate(), Some("rw=frontend"));
    }

    #[test]
    fn w3c_trace_context_decode_headers_no_traceparent() {
        let headers = vec![("tracestate", "rw=frontend")];
        assert!(W3CTraceContext::decode_headers(headers).is_none());
    }

    #[test]
    fn w3c_trace_context_decode_headers_repeated_tracestate() {
        // Per W3C spec, multiple tracestate headers should be joined with commas.
        let headers = vec![
            (
                "traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            ),
            ("tracestate", "rw=frontend"),
            ("tracestate", "congo=t61rcWkgMzE"),
        ];

        let ctx = W3CTraceContext::decode_headers(headers).unwrap();
        assert_eq!(ctx.tracestate(), Some("rw=frontend,congo=t61rcWkgMzE"));
    }

    #[test]
    fn w3c_trace_context_decode_headers_repeated_empty_mixed() {
        let headers = vec![
            (
                "traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            ),
            ("tracestate", ""),
            ("tracestate", "rw=frontend"),
            ("tracestate", "  "),
        ];

        let ctx = W3CTraceContext::decode_headers(headers).unwrap();
        assert_eq!(ctx.tracestate(), Some("rw=frontend"));
    }

    #[test]
    fn w3c_trace_context_decode_headers_all_empty_tracestate() {
        let headers = vec![
            (
                "traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
            ),
            ("tracestate", ""),
            ("tracestate", "  "),
        ];

        let ctx = W3CTraceContext::decode_headers(headers).unwrap();
        assert_eq!(ctx.tracestate(), None);
    }

    #[test]
    fn w3c_trace_context_header_roundtrip() {
        let original = W3CTraceContext::new(SpanContext::new(TraceId(42), SpanId(99)))
            .with_tracestate("vendor=value,other=data");

        let headers = original.encode_headers();
        let header_refs: Vec<(&str, &str)> = headers
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
            .collect();

        let decoded = W3CTraceContext::decode_headers(header_refs).unwrap();
        assert_eq!(original, decoded);
    }

    #[test]
    fn w3c_trace_context_from_span_context() {
        let span_ctx = SpanContext::new(TraceId(1), SpanId(2));
        let w3c_ctx: W3CTraceContext = span_ctx.into();
        assert_eq!(w3c_ctx.span_context, span_ctx);
        assert_eq!(w3c_ctx.tracestate(), None);
    }
}