oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
//! Distributed Query Tracing Across Subgraphs
//!
//! This module provides comprehensive distributed tracing for federated GraphQL queries,
//! enabling end-to-end observability across multiple subgraphs with OpenTelemetry integration.
//!
//! ## Features
//!
//! - **OpenTelemetry Integration**: Full W3C trace context propagation
//! - **Span Hierarchy**: Automatic parent-child span relationships
//! - **Cross-Service Tracing**: Traces queries across multiple subgraphs
//! - **Performance Metrics**: Detailed timing and resource usage per subgraph
//! - **Error Tracking**: Captures and propagates errors across services
//! - **Sampling Strategies**: Configurable sampling for high-volume scenarios

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;
use uuid::Uuid;

/// Trace context for distributed tracing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceContext {
    /// Unique trace ID following W3C trace context format
    pub trace_id: String,
    /// Current span ID
    pub span_id: String,
    /// Parent span ID (if any)
    pub parent_span_id: Option<String>,
    /// Trace flags (sampled, debug, etc.)
    pub trace_flags: u8,
    /// Trace state for vendor-specific data
    pub trace_state: HashMap<String, String>,
}

impl TraceContext {
    /// Create a new root trace context
    pub fn new() -> Self {
        Self {
            trace_id: generate_trace_id(),
            span_id: generate_span_id(),
            parent_span_id: None,
            trace_flags: 0x01, // Sampled by default
            trace_state: HashMap::new(),
        }
    }

    /// Create a child span context
    pub fn create_child(&self) -> Self {
        Self {
            trace_id: self.trace_id.clone(),
            span_id: generate_span_id(),
            parent_span_id: Some(self.span_id.clone()),
            trace_flags: self.trace_flags,
            trace_state: self.trace_state.clone(),
        }
    }

    /// Check if this trace is sampled
    pub fn is_sampled(&self) -> bool {
        (self.trace_flags & 0x01) != 0
    }

    /// Convert to W3C traceparent header
    pub fn to_traceparent(&self) -> String {
        format!(
            "00-{}-{}-{:02x}",
            self.trace_id, self.span_id, self.trace_flags
        )
    }

    /// Parse W3C traceparent header
    pub fn from_traceparent(header: &str) -> Result<Self> {
        let parts: Vec<&str> = header.split('-').collect();
        if parts.len() != 4 {
            return Err(anyhow!("Invalid traceparent format"));
        }

        if parts[0] != "00" {
            return Err(anyhow!("Unsupported trace version"));
        }

        let trace_flags =
            u8::from_str_radix(parts[3], 16).map_err(|e| anyhow!("Invalid trace flags: {}", e))?;

        Ok(Self {
            trace_id: parts[1].to_string(),
            span_id: parts[2].to_string(),
            parent_span_id: None,
            trace_flags,
            trace_state: HashMap::new(),
        })
    }
}

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

/// Span status
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub enum SpanStatus {
    /// Span completed successfully
    Ok,
    /// Span encountered an error
    Error,
    /// Span is still in progress
    InProgress,
}

/// Span kind (following OpenTelemetry convention)
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub enum SpanKind {
    /// Internal operation
    Internal,
    /// Server handling a request
    Server,
    /// Client making a request
    Client,
    /// Producer sending a message
    Producer,
    /// Consumer receiving a message
    Consumer,
}

/// A single span in the trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Span {
    /// Span ID
    pub span_id: String,
    /// Parent span ID
    pub parent_span_id: Option<String>,
    /// Span name (e.g., "resolve_user", "subgraph_query")
    pub name: String,
    /// Span kind
    pub kind: SpanKind,
    /// Start time
    pub start_time: SystemTime,
    /// End time (None if span is still active)
    pub end_time: Option<SystemTime>,
    /// Span duration
    pub duration: Option<Duration>,
    /// Span status
    pub status: SpanStatus,
    /// Span attributes (key-value pairs)
    pub attributes: HashMap<String, String>,
    /// Events recorded during span
    pub events: Vec<SpanEvent>,
    /// Service name
    pub service_name: String,
    /// Subgraph name (for federated queries)
    pub subgraph_name: Option<String>,
}

impl Span {
    pub fn new(
        span_id: String,
        parent_span_id: Option<String>,
        name: String,
        kind: SpanKind,
        service_name: String,
    ) -> Self {
        Self {
            span_id,
            parent_span_id,
            name,
            kind,
            start_time: SystemTime::now(),
            end_time: None,
            duration: None,
            status: SpanStatus::InProgress,
            attributes: HashMap::new(),
            events: Vec::new(),
            service_name,
            subgraph_name: None,
        }
    }

    /// Set an attribute
    pub fn set_attribute(&mut self, key: String, value: String) {
        self.attributes.insert(key, value);
    }

    /// Add an event
    pub fn add_event(&mut self, name: String, attributes: HashMap<String, String>) {
        self.events.push(SpanEvent {
            timestamp: SystemTime::now(),
            name,
            attributes,
        });
    }

    /// End the span
    pub fn end(&mut self, status: SpanStatus) {
        let end_time = SystemTime::now();
        self.end_time = Some(end_time);
        self.status = status;

        if let Ok(duration) = end_time.duration_since(self.start_time) {
            self.duration = Some(duration);
        }
    }

    /// Check if span has ended
    pub fn is_finished(&self) -> bool {
        self.end_time.is_some()
    }
}

/// Event recorded during a span
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpanEvent {
    pub timestamp: SystemTime,
    pub name: String,
    pub attributes: HashMap<String, String>,
}

/// Complete trace with all spans
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trace {
    /// Trace ID
    pub trace_id: String,
    /// Root span
    pub root_span: Span,
    /// All spans in the trace
    pub spans: Vec<Span>,
    /// Trace start time
    pub start_time: SystemTime,
    /// Trace end time
    pub end_time: Option<SystemTime>,
    /// Total duration
    pub duration: Option<Duration>,
    /// Trace-level attributes
    pub attributes: HashMap<String, String>,
}

impl Trace {
    pub fn new(trace_id: String, root_span: Span) -> Self {
        let start_time = root_span.start_time;

        Self {
            trace_id,
            root_span,
            spans: Vec::new(),
            start_time,
            end_time: None,
            duration: None,
            attributes: HashMap::new(),
        }
    }

    /// Add a span to the trace
    pub fn add_span(&mut self, span: Span) {
        self.spans.push(span);
    }

    /// End the trace
    pub fn end(&mut self) {
        let end_time = SystemTime::now();
        self.end_time = Some(end_time);

        if let Ok(duration) = end_time.duration_since(self.start_time) {
            self.duration = Some(duration);
        }
    }

    /// Get all spans (including root)
    pub fn all_spans(&self) -> Vec<&Span> {
        let mut spans = vec![&self.root_span];
        spans.extend(self.spans.iter());
        spans
    }

    /// Get subgraph breakdown (time spent per subgraph)
    pub fn subgraph_breakdown(&self) -> HashMap<String, Duration> {
        let mut breakdown = HashMap::new();

        for span in self.all_spans() {
            if let (Some(subgraph), Some(duration)) = (&span.subgraph_name, span.duration) {
                *breakdown.entry(subgraph.clone()).or_insert(Duration::ZERO) += duration;
            }
        }

        breakdown
    }

    /// Calculate critical path (slowest sequential path)
    pub fn critical_path(&self) -> Vec<&Span> {
        // Simple implementation: return spans in chronological order
        let mut spans = self.all_spans();
        spans.sort_by_key(|s| s.start_time);
        spans
    }
}

/// Distributed tracing configuration
#[derive(Debug, Clone)]
pub struct TracingConfig {
    /// Enable distributed tracing
    pub enabled: bool,
    /// Sample rate (0.0 to 1.0)
    pub sample_rate: f64,
    /// Service name
    pub service_name: String,
    /// Export traces to backend
    pub export_enabled: bool,
    /// Export endpoint (e.g., OpenTelemetry collector)
    pub export_endpoint: Option<String>,
    /// Maximum spans per trace
    pub max_spans_per_trace: usize,
    /// Maximum trace duration before auto-completion
    pub max_trace_duration: Duration,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            sample_rate: 1.0, // Sample all traces by default
            service_name: "oxirs-gql".to_string(),
            export_enabled: false,
            export_endpoint: None,
            max_spans_per_trace: 1000,
            max_trace_duration: Duration::from_secs(60),
        }
    }
}

/// Distributed tracer for federated queries
pub struct DistributedTracer {
    config: TracingConfig,
    active_traces: Arc<RwLock<HashMap<String, Trace>>>,
    completed_traces: Arc<RwLock<Vec<Trace>>>,
    active_spans: Arc<RwLock<HashMap<String, Span>>>,
}

impl DistributedTracer {
    pub fn new(config: TracingConfig) -> Self {
        Self {
            config,
            active_traces: Arc::new(RwLock::new(HashMap::new())),
            completed_traces: Arc::new(RwLock::new(Vec::new())),
            active_spans: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Start a new trace
    pub async fn start_trace(&self, context: &TraceContext, name: String) -> Result<Span> {
        if !self.config.enabled || !self.should_sample() {
            // Return a no-op span if tracing is disabled or not sampled
            return Ok(Span::new(
                context.span_id.clone(),
                context.parent_span_id.clone(),
                name,
                SpanKind::Server,
                self.config.service_name.clone(),
            ));
        }

        let root_span = Span::new(
            context.span_id.clone(),
            context.parent_span_id.clone(),
            name,
            SpanKind::Server,
            self.config.service_name.clone(),
        );

        let trace = Trace::new(context.trace_id.clone(), root_span.clone());

        let mut traces = self.active_traces.write().await;
        traces.insert(context.trace_id.clone(), trace);

        Ok(root_span)
    }

    /// Start a child span
    pub async fn start_span(
        &self,
        context: &TraceContext,
        name: String,
        kind: SpanKind,
        subgraph_name: Option<String>,
    ) -> Result<Span> {
        if !self.config.enabled || !context.is_sampled() {
            // Return a no-op span
            let mut span = Span::new(
                context.span_id.clone(),
                context.parent_span_id.clone(),
                name,
                kind,
                self.config.service_name.clone(),
            );
            span.subgraph_name = subgraph_name;
            return Ok(span);
        }

        let mut span = Span::new(
            context.span_id.clone(),
            context.parent_span_id.clone(),
            name,
            kind,
            self.config.service_name.clone(),
        );
        span.subgraph_name = subgraph_name;

        // Store active span
        let mut spans = self.active_spans.write().await;
        spans.insert(context.span_id.clone(), span.clone());

        Ok(span)
    }

    /// End a span
    pub async fn end_span(&self, trace_id: &str, span_id: &str, status: SpanStatus) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        // Get and remove from active spans
        let span = {
            let mut spans = self.active_spans.write().await;
            spans.remove(span_id)
        };

        if let Some(mut span) = span {
            span.end(status);

            // Add to trace
            let mut traces = self.active_traces.write().await;
            if let Some(trace) = traces.get_mut(trace_id) {
                trace.add_span(span);
            }
        }

        Ok(())
    }

    /// End a trace
    pub async fn end_trace(&self, trace_id: &str) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        let trace = {
            let mut traces = self.active_traces.write().await;
            traces.remove(trace_id)
        };

        if let Some(mut trace) = trace {
            trace.end();

            // Export if configured
            if self.config.export_enabled {
                self.export_trace(&trace).await?;
            }

            // Store completed trace
            let mut completed = self.completed_traces.write().await;
            completed.push(trace);

            // Limit completed traces
            if completed.len() > 1000 {
                completed.drain(0..500);
            }
        }

        Ok(())
    }

    /// Get a trace
    pub async fn get_trace(&self, trace_id: &str) -> Option<Trace> {
        // Check active traces
        {
            let traces = self.active_traces.read().await;
            if let Some(trace) = traces.get(trace_id) {
                return Some(trace.clone());
            }
        }

        // Check completed traces
        {
            let completed = self.completed_traces.read().await;
            completed.iter().find(|t| t.trace_id == trace_id).cloned()
        }
    }

    /// Get all completed traces
    pub async fn get_completed_traces(&self) -> Vec<Trace> {
        let completed = self.completed_traces.read().await;
        completed.clone()
    }

    /// Should sample this trace
    fn should_sample(&self) -> bool {
        use scirs2_core::random::{rng, RngExt};
        let mut rng = rng();
        rng.random_range(0.0..1.0) < self.config.sample_rate
    }

    /// Export trace to backend
    async fn export_trace(&self, trace: &Trace) -> Result<()> {
        // Placeholder for OpenTelemetry export
        // In production, this would send to an OTLP endpoint
        if let Some(endpoint) = &self.config.export_endpoint {
            tracing::debug!(
                "Exporting trace {} to {} (not implemented)",
                trace.trace_id,
                endpoint
            );
        }
        Ok(())
    }

    /// Get tracing statistics
    pub async fn get_stats(&self) -> TracingStats {
        let active_traces = self.active_traces.read().await;
        let completed_traces = self.completed_traces.read().await;

        let avg_duration = if !completed_traces.is_empty() {
            let total: Duration = completed_traces.iter().filter_map(|t| t.duration).sum();
            total / completed_traces.len() as u32
        } else {
            Duration::ZERO
        };

        TracingStats {
            active_traces: active_traces.len(),
            completed_traces: completed_traces.len(),
            total_traces: active_traces.len() + completed_traces.len(),
            avg_trace_duration: avg_duration,
        }
    }
}

/// Tracing statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingStats {
    pub active_traces: usize,
    pub completed_traces: usize,
    pub total_traces: usize,
    pub avg_trace_duration: Duration,
}

/// Generate a random trace ID (32 hex characters)
fn generate_trace_id() -> String {
    format!("{:032x}", Uuid::new_v4().as_u128())
}

/// Generate a random span ID (16 hex characters)
fn generate_span_id() -> String {
    format!("{:016x}", Uuid::new_v4().as_u128() & 0xFFFFFFFFFFFFFFFF)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trace_context_creation() {
        let ctx = TraceContext::new();
        assert_eq!(ctx.trace_id.len(), 32);
        assert_eq!(ctx.span_id.len(), 16);
        assert!(ctx.parent_span_id.is_none());
        assert!(ctx.is_sampled());
    }

    #[test]
    fn test_trace_context_child() {
        let parent = TraceContext::new();
        let child = parent.create_child();

        assert_eq!(child.trace_id, parent.trace_id);
        assert_ne!(child.span_id, parent.span_id);
        assert_eq!(child.parent_span_id, Some(parent.span_id.clone()));
    }

    #[test]
    fn test_traceparent_format() {
        let ctx = TraceContext::new();
        let header = ctx.to_traceparent();

        assert!(header.starts_with("00-"));
        assert_eq!(header.matches('-').count(), 3);
    }

    #[test]
    fn test_traceparent_parse() {
        let header = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01";
        let ctx = TraceContext::from_traceparent(header).expect("should succeed");

        assert_eq!(ctx.trace_id, "0af7651916cd43dd8448eb211c80319c");
        assert_eq!(ctx.span_id, "b7ad6b7169203331");
        assert_eq!(ctx.trace_flags, 0x01);
        assert!(ctx.is_sampled());
    }

    #[test]
    fn test_span_lifecycle() {
        let mut span = Span::new(
            "span123".to_string(),
            None,
            "test_span".to_string(),
            SpanKind::Internal,
            "test_service".to_string(),
        );

        assert_eq!(span.status, SpanStatus::InProgress);
        assert!(!span.is_finished());

        span.set_attribute("key".to_string(), "value".to_string());
        assert_eq!(span.attributes.get("key"), Some(&"value".to_string()));

        span.add_event("event1".to_string(), HashMap::new());
        assert_eq!(span.events.len(), 1);

        span.end(SpanStatus::Ok);
        assert_eq!(span.status, SpanStatus::Ok);
        assert!(span.is_finished());
        assert!(span.duration.is_some());
    }

    #[test]
    fn test_trace_creation() {
        let root_span = Span::new(
            "root".to_string(),
            None,
            "root_span".to_string(),
            SpanKind::Server,
            "service".to_string(),
        );

        let trace = Trace::new("trace123".to_string(), root_span);

        assert_eq!(trace.trace_id, "trace123");
        assert_eq!(trace.spans.len(), 0);
        assert!(trace.duration.is_none());
    }

    #[test]
    fn test_trace_add_spans() {
        let root_span = Span::new(
            "root".to_string(),
            None,
            "root".to_string(),
            SpanKind::Server,
            "service".to_string(),
        );

        let mut trace = Trace::new("trace123".to_string(), root_span);

        let child_span = Span::new(
            "child1".to_string(),
            Some("root".to_string()),
            "child".to_string(),
            SpanKind::Client,
            "service".to_string(),
        );

        trace.add_span(child_span);
        assert_eq!(trace.spans.len(), 1);
        assert_eq!(trace.all_spans().len(), 2); // root + 1 child
    }

    #[tokio::test]
    async fn test_tracer_start_trace() {
        let config = TracingConfig::default();
        let tracer = DistributedTracer::new(config);

        let ctx = TraceContext::new();
        let span = tracer
            .start_trace(&ctx, "test_query".to_string())
            .await
            .expect("should succeed");

        assert_eq!(span.name, "test_query");
        assert_eq!(span.kind, SpanKind::Server);

        let trace = tracer.get_trace(&ctx.trace_id).await;
        assert!(trace.is_some());
    }

    #[tokio::test]
    async fn test_tracer_child_span() {
        let config = TracingConfig::default();
        let tracer = DistributedTracer::new(config);

        let ctx = TraceContext::new();
        tracer
            .start_trace(&ctx, "root".to_string())
            .await
            .expect("should succeed");

        let child_ctx = ctx.create_child();
        let child_span = tracer
            .start_span(
                &child_ctx,
                "child_query".to_string(),
                SpanKind::Client,
                Some("subgraph1".to_string()),
            )
            .await
            .expect("should succeed");

        assert_eq!(child_span.name, "child_query");
        assert_eq!(child_span.subgraph_name, Some("subgraph1".to_string()));
        assert_eq!(child_span.parent_span_id, Some(ctx.span_id.clone()));
    }

    #[tokio::test]
    async fn test_tracer_end_span() {
        let config = TracingConfig::default();
        let tracer = DistributedTracer::new(config);

        let ctx = TraceContext::new();
        tracer
            .start_trace(&ctx, "root".to_string())
            .await
            .expect("should succeed");

        let child_ctx = ctx.create_child();
        tracer
            .start_span(&child_ctx, "child".to_string(), SpanKind::Client, None)
            .await
            .expect("should succeed");

        tracer
            .end_span(&ctx.trace_id, &child_ctx.span_id, SpanStatus::Ok)
            .await
            .expect("should succeed");

        let trace = tracer
            .get_trace(&ctx.trace_id)
            .await
            .expect("should succeed");
        assert_eq!(trace.spans.len(), 1);
    }

    #[tokio::test]
    async fn test_tracer_end_trace() {
        let config = TracingConfig::default();
        let tracer = DistributedTracer::new(config);

        let ctx = TraceContext::new();
        tracer
            .start_trace(&ctx, "root".to_string())
            .await
            .expect("should succeed");

        tracer
            .end_trace(&ctx.trace_id)
            .await
            .expect("should succeed");

        // Should now be in completed traces
        let completed = tracer.get_completed_traces().await;
        assert_eq!(completed.len(), 1);
        assert_eq!(completed[0].trace_id, ctx.trace_id);
    }

    #[tokio::test]
    async fn test_tracer_disabled() {
        let config = TracingConfig {
            enabled: false,
            ..Default::default()
        };
        let tracer = DistributedTracer::new(config);

        let ctx = TraceContext::new();
        let span = tracer
            .start_trace(&ctx, "test".to_string())
            .await
            .expect("should succeed");

        // Should still return a span, but not track it
        assert_eq!(span.name, "test");

        let trace = tracer.get_trace(&ctx.trace_id).await;
        assert!(trace.is_none());
    }

    #[tokio::test]
    async fn test_get_stats() {
        let config = TracingConfig::default();
        let tracer = DistributedTracer::new(config);

        let ctx1 = TraceContext::new();
        tracer
            .start_trace(&ctx1, "trace1".to_string())
            .await
            .expect("should succeed");

        let ctx2 = TraceContext::new();
        tracer
            .start_trace(&ctx2, "trace2".to_string())
            .await
            .expect("should succeed");

        tracer
            .end_trace(&ctx1.trace_id)
            .await
            .expect("should succeed");

        let stats = tracer.get_stats().await;
        assert_eq!(stats.active_traces, 1);
        assert_eq!(stats.completed_traces, 1);
        assert_eq!(stats.total_traces, 2);
    }

    #[test]
    fn test_span_kind_variants() {
        assert_eq!(SpanKind::Internal as i32, SpanKind::Internal as i32);
        assert_ne!(SpanKind::Client as i32, SpanKind::Server as i32);
    }

    #[test]
    fn test_span_status_variants() {
        assert_eq!(SpanStatus::Ok, SpanStatus::Ok);
        assert_ne!(SpanStatus::Ok, SpanStatus::Error);
        assert_ne!(SpanStatus::Error, SpanStatus::InProgress);
    }
}