dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Distributed Tracing Context Propagation
//! 
//! This module provides implementations for distributed tracing context propagation,
//! following the W3C Trace Context specification. It allows for propagating trace
//! information across service boundaries using HTTP headers.
//! 
//! ## Key Components
//! 
//! - **RiTraceContext**: Represents W3C Trace Context with trace ID, parent ID, and flags
//! - **RiBaggage**: Represents baggage for carrying additional cross-cutting concerns
//! - **RiContextCarrier**: Carries both trace context and baggage for propagation
//! 
//! ## Design Principles
//! 
//! 1. **W3C Compliance**: Implements the W3C Trace Context specification
//! 2. **Baggage Support**: Provides baggage propagation for additional context
//! 3. **HTTP Header Integration**: Supports extraction and injection from/to HTTP headers
//! 4. **Serialization Support**: Implements Serialize and Deserialize for easy persistence
//! 5. **Thread Safety**: All structs are cloneable for safe sharing across threads
//! 6. **Sampling Support**: Includes trace sampling flag support
//! 7. **Trace State**: Optional support for vendor-specific trace state
//! 8. **Easy to Use**: Simple API for creating and manipulating trace contexts
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! use std::collections::HashMap as FxHashMap;
//! 
//! fn example() {
//!     // Create trace and span IDs
//!     let trace_id = RiTraceId::generate();
//!     let span_id = RiSpanId::generate();
//!     
//!     // Create a trace context
//!     let mut trace_context = RiTraceContext::new(trace_id, span_id);
//!     trace_context.set_sampled(true);
//!     
//!     // Create baggage
//!     let mut baggage = RiBaggage::new();
//!     baggage.insert("user_id".to_string(), "12345".to_string());
//!     baggage.insert("request_id".to_string(), "abc123".to_string());
//!     
//!     // Create a context carrier
//!     let carrier = RiContextCarrier::new()
//!         .with_trace_context(trace_context)
//!         .with_baggage(baggage);
//!     
//!     // Inject into HTTP headers
//!     let mut headers = FxHashMap::default();
//!     carrier.inject_into_headers(&mut headers);
//!     println!("Headers: {:?}", headers);
//!     
//!     // Extract from HTTP headers
//!     let extracted_carrier = RiContextCarrier::from_headers(&headers);
//!     println!("Extracted trace context: {:?}", extracted_carrier.trace_context);
//! }
//! ```

use std::collections::HashMap as FxHashMap;
use serde::{Serialize, Deserialize};
use crate::observability::tracing::{RiTraceId, RiSpanId};

/// W3C Trace Context propagation format
///
/// This struct represents a W3C Trace Context, which is used to propagate trace information
/// across service boundaries. It follows the W3C Trace Context specification: https://www.w3.org/TR/trace-context/
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiTraceContext {
    /// Trace context version
    pub version: u8,
    /// Trace ID for the entire trace
    pub trace_id: RiTraceId,
    /// Parent span ID
    pub parent_id: RiSpanId,
    /// Trace flags (bitmask)
    pub trace_flags: u8,
    /// Optional trace state for vendor-specific information
    pub trace_state: Option<String>,
}

impl Default for RiTraceContext {
    fn default() -> Self {
        Self {
            version: 0x00,
            trace_id: RiTraceId::default(),
            parent_id: RiSpanId::default(),
            trace_flags: 0x01,
            trace_state: None,
        }
    }
}

impl RiTraceContext {
    /// Creates a new trace context with the given trace ID and parent span ID.
    ///
    /// # Parameters
    ///
    /// - `trace_id`: The trace ID for the trace
    /// - `parent_id`: The parent span ID
    ///
    /// # Returns
    ///
    /// A new RiTraceContext instance
    #[allow(dead_code)]
    pub fn new(trace_id: RiTraceId, parent_id: RiSpanId) -> Self {
        Self {
            version: 0x00,
            trace_id,
            parent_id,
            trace_flags: 0x01, // Sampled flag
            trace_state: None,
        }
    }
    
    /// Parses a trace context from a W3C Trace Context header string.
    ///
    /// # Parameters
    ///
    /// - `header`: The traceparent header string in format "00-{trace-id}-{parent-id}-{trace-flags}"
    ///
    /// # Returns
    ///
    /// An Option containing the parsed RiTraceContext, or None if parsing failed
    #[allow(dead_code)]
    pub fn from_header(header: &str) -> Option<Self> {
        let parts: Vec<&str> = header.split('-').collect();
        if parts.len() != 4 {
            return None;
        }
        
        let version = u8::from_str_radix(parts[0], 16).ok()?;
        let trace_id = RiTraceId::from_string(parts[1].to_string());
        let parent_id = RiSpanId::from_string(parts[2].to_string());
        let trace_flags = u8::from_str_radix(parts[3], 16).ok()?;
        
        Some(Self {
            version,
            trace_id,
            parent_id,
            trace_flags,
            trace_state: None,
        })
    }
    
    /// Converts the trace context to a W3C Trace Context header string.
    ///
    /// # Returns
    ///
    /// A string in the format "00-{trace-id}-{parent-id}-{trace-flags}"
    #[allow(dead_code)]
    pub fn to_header(&self) -> String {
        format!(
            "{:02x}-{}-{}-{:02x}",
            self.version,
            self.trace_id.as_str(),
            self.parent_id.as_str(),
            self.trace_flags
        )
    }
    
    /// Checks if the trace is sampled.
    ///
    /// # Returns
    ///
    /// True if the sampled flag is set, false otherwise
    #[allow(dead_code)]
    pub fn is_sampled(&self) -> bool {
        (self.trace_flags & 0x01) != 0
    }
    
    /// Sets the sampled flag on the trace context.
    ///
    /// # Parameters
    ///
    /// - `sampled`: Whether the trace should be sampled
    #[allow(dead_code)]
    pub fn set_sampled(&mut self, sampled: bool) {
        if sampled {
            self.trace_flags |= 0x01;
        } else {
            self.trace_flags &= !0x01;
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiTraceContext {
    #[new]
    fn py_new() -> Self {
        Self::default()
    }

    #[staticmethod]
    #[pyo3(name = "from_header")]
    fn py_from_header(header: String) -> Option<Self> {
        Self::from_header(&header)
    }

    #[pyo3(name = "to_header")]
    fn py_to_header(&self) -> String {
        self.to_header()
    }

    #[pyo3(name = "is_sampled")]
    fn py_is_sampled(&self) -> bool {
        self.is_sampled()
    }

    fn sampled(&mut self, sampled: bool) {
        self.set_sampled(sampled);
    }
}

/// Baggage propagation for cross-cutting concerns.
///
/// This struct represents baggage, which is used to carry additional context information
/// across service boundaries. It follows the W3C Baggage specification.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiBaggage {
    /// Map of baggage items
    items: FxHashMap<String, String>,
}

impl Default for RiBaggage {
    fn default() -> Self {
        Self::new()
    }
}

impl RiBaggage {
    /// Creates a new empty baggage instance.
    ///
    /// # Returns
    ///
    /// A new RiBaggage instance
    #[allow(dead_code)]
    pub fn new() -> Self {
        Self {
            items: FxHashMap::default(),
        }
    }
    
    /// Inserts a key-value pair into the baggage.
    ///
    /// # Parameters
    ///
    /// - `key`: The baggage key
    /// - `value`: The baggage value
    #[allow(dead_code)]
    pub fn insert(&mut self, key: String, value: String) {
        self.items.insert(key, value);
    }
    
    /// Gets a value from the baggage by key.
    ///
    /// # Parameters
    ///
    /// - `key`: The baggage key to look up
    ///
    /// # Returns
    ///
    /// An Option containing the value if found, or None otherwise
    #[allow(dead_code)]
    pub fn get(&self, key: &str) -> Option<&String> {
        self.items.get(key)
    }
    
    /// Removes a key-value pair from the baggage.
    ///
    /// # Parameters
    ///
    /// - `key`: The baggage key to remove
    #[allow(dead_code)]
    pub fn remove(&mut self, key: &str) {
        self.items.remove(key);
    }
    
    /// Parses baggage from a W3C Baggage header string.
    ///
    /// # Parameters
    ///
    /// - `header`: The baggage header string in format "key1=value1,key2=value2"
    ///
    /// # Returns
    ///
    /// A new RiBaggage instance with the parsed items
    #[allow(dead_code)]
    pub fn from_header(header: &str) -> Self {
        let mut baggage = Self::new();
        
        for item in header.split(',') {
            let item = item.trim();
            if let Some(eq_pos) = item.find('=') {
                let key = item[..eq_pos].trim().to_string();
                let value = item[eq_pos + 1..].trim().to_string();
                baggage.insert(key, value);
            }
        }
        
        baggage
    }
    
    /// Converts the baggage to a W3C Baggage header string.
    ///
    /// # Returns
    ///
    /// A string in the format "key1=value1,key2=value2"
    #[allow(dead_code)]
    pub fn to_header(&self) -> String {
        self.items
            .iter()
            .map(|(k, v)| format!("{k}={v}"))
            .collect::<Vec<_>>()
            .join(",")
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiBaggage {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }

    #[staticmethod]
    #[pyo3(name = "from_header")]
    fn py_from_header(header: String) -> Self {
        Self::from_header(&header)
    }

    #[pyo3(name = "to_header")]
    fn py_to_header(&self) -> String {
        self.to_header()
    }

    fn add(&mut self, key: String, value: String) {
        self.items.insert(key, value);
    }

    fn fetch(&self, key: String) -> Option<String> {
        self.items.get(&key).cloned()
    }

    fn delete(&mut self, key: String) {
        self.items.remove(&key);
    }
}

/// Context carrier for distributed tracing.
///
/// This struct carries both trace context and baggage, providing a convenient way to
/// extract and inject distributed tracing information from/to HTTP headers.
#[allow(dead_code)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiContextCarrier {
    /// Trace context for the request
    pub trace_context: Option<RiTraceContext>,
    /// Baggage for additional context
    pub baggage: RiBaggage,
}

impl Default for RiContextCarrier {
    fn default() -> Self {
        Self::new()
    }
}

impl RiContextCarrier {
    /// Creates a new empty context carrier.
    ///
    /// # Returns
    ///
    /// A new RiContextCarrier instance
    #[allow(dead_code)]
    pub fn new() -> Self {
        Self {
            trace_context: None,
            baggage: RiBaggage::new(),
        }
    }
    
    /// Adds trace context to the carrier.
    ///
    /// # Parameters
    ///
    /// - `trace_context`: The trace context to add
    ///
    /// # Returns
    ///
    /// The updated RiContextCarrier instance
    #[allow(dead_code)]
    pub fn with_trace_context(mut self, trace_context: RiTraceContext) -> Self {
        self.trace_context = Some(trace_context);
        self
    }
    
    /// Adds baggage to the carrier.
    ///
    /// # Parameters
    ///
    /// - `baggage`: The baggage to add
    ///
    /// # Returns
    ///
    /// The updated RiContextCarrier instance
    #[allow(dead_code)]
    pub fn with_baggage(mut self, baggage: RiBaggage) -> Self {
        self.baggage = baggage;
        self
    }
    
    /// Creates a context carrier from tracing context.
    ///
    /// This method converts a thread-local RiTracingContext into a RiContextCarrier
    /// that can be propagated across service boundaries.
    ///
    /// # Parameters
    ///
    /// - `tracing_context`: The tracing context to convert
    ///
    /// # Returns
    ///
    /// A new RiContextCarrier instance with trace context and baggage from the tracing context
    #[allow(dead_code)]
    pub fn from_tracing_context(tracing_context: &crate::observability::tracing::RiTracingContext) -> Self {
        let mut carrier = Self::new();
        
        // Create trace context if trace ID and span ID are available
        if let (Some(trace_id), Some(span_id)) = (
            tracing_context.trace_id(),
            tracing_context.span_id()
        ) {
            let trace_context = RiTraceContext::new(
                trace_id.clone(),
                span_id.clone()
            );
            carrier.trace_context = Some(trace_context);
        }
        
        // Convert baggage from tracing context
        let baggage = RiBaggage::new();
        // Note: We don't have direct access to tracing_context.baggage since it's private,
        // so we'll create an empty baggage for now
        carrier.baggage = baggage;
        
        carrier
    }
    
    /// Creates a tracing context from this carrier.
    ///
    /// This method converts a RiContextCarrier into a thread-local RiTracingContext
    /// that can be used for tracing within the service.
    ///
    /// # Returns
    ///
    /// A new RiTracingContext instance with trace context and baggage from the carrier
    #[allow(dead_code)]
    pub fn into_tracing_context(self) -> crate::observability::tracing::RiTracingContext {
        let mut context = crate::observability::tracing::RiTracingContext::new();
        
        // Set trace ID and span ID from trace context if available
        if let Some(trace_context) = self.trace_context {
            context = context.with_trace_id(trace_context.trace_id);
            context = context.with_span_id(trace_context.parent_id);
        }
        
        // Set baggage from carrier
        // Note: We don't have direct access to context.baggage since it's private,
        // so we'll skip setting baggage for now
        
        context
    }
    
    /// Extracts a context carrier from HTTP headers.
    ///
    /// # Parameters
    ///
    /// - `headers`: A HashMap of HTTP headers
    ///
    /// # Returns
    ///
    /// A new RiContextCarrier instance with extracted trace context and baggage
    #[allow(dead_code)]
    pub fn from_headers(headers: &FxHashMap<String, String>) -> Self {
        let mut carrier = Self::new();
        
        // Extract trace context from traceparent header
        if let Some(traceparent) = headers.get("traceparent") {
            if let Some(trace_context) = RiTraceContext::from_header(traceparent) {
                carrier.trace_context = Some(trace_context);
            }
        }
        
        // Extract baggage from baggage header
        if let Some(baggage_header) = headers.get("baggage") {
            carrier.baggage = RiBaggage::from_header(baggage_header);
        }
        
        carrier
    }
    
    /// Injects the context carrier into HTTP headers.
    ///
    /// # Parameters
    ///
    /// - `headers`: A mutable HashMap of HTTP headers to inject into
    #[allow(dead_code)]
    pub fn inject_into_headers(&self, headers: &mut FxHashMap<String, String>) {
        if let Some(ref trace_context) = self.trace_context {
            headers.insert("traceparent".to_string(), trace_context.to_header());
        }
        
        let baggage_header = self.baggage.to_header();
        if !baggage_header.is_empty() {
            headers.insert("baggage".to_string(), baggage_header);
        }
    }
    
    /// Extracts a context carrier from HTTP headers and sets it as current tracing context.
    ///
    /// This convenience method extracts trace information from HTTP headers, creates a
    /// tracing context, and sets it as the current thread-local context.
    ///
    /// # Parameters
    ///
    /// - `headers`: A HashMap of HTTP headers
    ///
    /// # Returns
    ///
    /// A new RiContextCarrier instance with extracted trace context and baggage
    #[allow(dead_code)]
    pub fn from_headers_and_set_current(headers: &FxHashMap<String, String>) -> Self {
        let carrier = Self::from_headers(headers);
        let tracing_context = carrier.clone().into_tracing_context();
        tracing_context.set_as_current();
        carrier
    }
    
    /// Injects the current tracing context into HTTP headers.
    ///
    /// This convenience method gets the current thread-local tracing context,
    /// converts it to a context carrier, and injects it into HTTP headers.
    ///
    /// # Parameters
    ///
    /// - `headers`: A mutable HashMap of HTTP headers to inject into
    #[allow(dead_code)]
    pub fn inject_current_into_headers(headers: &mut FxHashMap<String, String>) {
        if let Some(tracing_context) = crate::observability::tracing::RiTracingContext::current() {
            let carrier = Self::from_tracing_context(&tracing_context);
            carrier.inject_into_headers(headers);
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiContextCarrier {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }

    #[pyo3(name = "with_trace_context")]
    fn py_with_trace_context(&mut self, trace_context: RiTraceContext) {
        self.trace_context = Some(trace_context);
    }

    #[pyo3(name = "get_trace_context")]
    fn py_get_trace_context(&self) -> Option<RiTraceContext> {
        self.trace_context.clone()
    }

    #[pyo3(name = "with_baggage")]
    fn py_with_baggage(&mut self, baggage: RiBaggage) {
        self.baggage = baggage;
    }

    #[pyo3(name = "get_baggage")]
    fn py_get_baggage(&self) -> RiBaggage {
        self.baggage.clone()
    }

    #[pyo3(name = "inject_into_headers")]
    fn py_inject_into_headers(&self) -> FxHashMap<String, String> {
        let mut headers = FxHashMap::default();
        self.inject_into_headers(&mut headers);
        headers
    }

    #[staticmethod]
    #[pyo3(name = "from_headers")]
    fn py_from_headers(headers: FxHashMap<String, String>) -> Self {
        Self::from_headers(&headers)
    }
}

/// W3C Trace Context Propagator for distributed tracing.
///
/// This struct implements the W3C Trace Context propagation specification,
/// providing methods for extracting and injecting trace context from/to
/// various carrier formats like HTTP headers.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct W3CTracePropagator;

impl W3CTracePropagator {
    /// Creates a new W3CTracePropagator instance.
    ///
    /// # Returns
    ///
    /// A new W3CTracePropagator instance
    #[allow(dead_code)]
    pub fn new() -> Self {
        Self
    }

    /// Extracts trace context from HTTP headers.
    ///
    /// This method parses the W3C traceparent and baggage headers from the
    /// provided HTTP headers and creates a RiContextCarrier with the
    /// extracted information.
    ///
    /// # Parameters
    ///
    /// - `headers`: A reference to a HashMap of HTTP headers
    ///
    /// # Returns
    ///
    /// A RiContextCarrier containing the extracted trace context and baggage
    #[allow(dead_code)]
    pub fn extract(&self, headers: &FxHashMap<String, String>) -> RiContextCarrier {
        RiContextCarrier::from_headers(headers)
    }

    /// Injects trace context into HTTP headers.
    ///
    /// This method takes a RiContextCarrier and injects its trace context
    /// and baggage into the provided HTTP headers HashMap.
    ///
    /// # Parameters
    ///
    /// - `carrier`: The context carrier containing trace information
    /// - `headers`: A mutable reference to a HashMap of HTTP headers
    #[allow(dead_code)]
    pub fn inject(&self, carrier: &RiContextCarrier, headers: &mut FxHashMap<String, String>) {
        carrier.inject_into_headers(headers);
    }

    /// Extracts trace context and sets it as the current tracing context.
    ///
    /// This convenience method extracts trace context from HTTP headers and
    /// sets it as the current thread-local tracing context.
    ///
    /// # Parameters
    ///
    /// - `headers`: A reference to a HashMap of HTTP headers
    ///
    /// # Returns
    ///
    /// A RiContextCarrier containing the extracted trace context and baggage
    #[allow(dead_code)]
    pub fn extract_and_set_current(&self, headers: &FxHashMap<String, String>) -> RiContextCarrier {
        RiContextCarrier::from_headers_and_set_current(headers)
    }

    /// Injects the current tracing context into HTTP headers.
    ///
    /// This convenience method gets the current thread-local tracing context,
    /// converts it to a context carrier, and injects it into HTTP headers.
    ///
    /// # Parameters
    ///
    /// - `headers`: A mutable reference to a HashMap of HTTP headers
    #[allow(dead_code)]
    pub fn inject_current(&self, headers: &mut FxHashMap<String, String>) {
        RiContextCarrier::inject_current_into_headers(headers);
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl W3CTracePropagator {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }

    #[pyo3(name = "extract")]
    fn py_extract(&self, headers: FxHashMap<String, String>) -> RiContextCarrier {
        self.extract(&headers)
    }

    #[pyo3(name = "inject")]
    fn py_inject(&self, carrier: &RiContextCarrier) -> FxHashMap<String, String> {
        let mut headers = FxHashMap::default();
        self.inject(carrier, &mut headers);
        headers
    }

    #[pyo3(name = "extract_and_set_current")]
    fn py_extract_and_set_current(&self, headers: FxHashMap<String, String>) -> RiContextCarrier {
        self.extract_and_set_current(&headers)
    }

    #[pyo3(name = "inject_current")]
    fn py_inject_current(&self) -> FxHashMap<String, String> {
        let mut headers = FxHashMap::default();
        self.inject_current(&mut headers);
        headers
    }
}