rustapi-extras 0.1.470

Production-ready middleware collection for RustAPI. Includes JWT auth, CORS, Rate Limiting, SQLx integration, and OpenTelemetry observability.
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
//! W3C Trace Context propagation utilities
//!
//! This module implements trace context propagation according to the
//! W3C Trace Context specification for distributed tracing.

use rustapi_core::Request;
use std::fmt;

/// W3C Trace Context header name for traceparent
pub const TRACEPARENT_HEADER: &str = "traceparent";

/// W3C Trace Context header name for tracestate
pub const TRACESTATE_HEADER: &str = "tracestate";

/// Correlation ID header name
pub const CORRELATION_ID_HEADER: &str = "x-correlation-id";

/// Request ID header name
pub const REQUEST_ID_HEADER: &str = "x-request-id";

/// Trace context information
#[derive(Clone, Debug, Default)]
pub struct TraceContext {
    /// Trace ID (128-bit, hex encoded)
    pub trace_id: String,
    /// Span ID (64-bit, hex encoded)
    pub span_id: String,
    /// Parent span ID (64-bit, hex encoded) - if this is a child span
    pub parent_span_id: Option<String>,
    /// Trace flags (8 bits)
    pub trace_flags: u8,
    /// Trace state (vendor-specific data)
    pub trace_state: Option<String>,
    /// Correlation ID for request tracking
    pub correlation_id: Option<String>,
}

impl TraceContext {
    /// Create a new trace context with generated IDs
    pub fn new() -> Self {
        Self {
            trace_id: Self::generate_trace_id(),
            span_id: Self::generate_span_id(),
            parent_span_id: None,
            trace_flags: 0x01, // Sampled flag
            trace_state: None,
            correlation_id: Some(Self::generate_correlation_id()),
        }
    }

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

    /// Generate a new trace ID (128-bit, 32 hex chars)
    pub fn generate_trace_id() -> String {
        use std::time::{SystemTime, UNIX_EPOCH};
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let random: u64 = rand_simple();
        format!("{:016x}{:016x}", now as u64, random)
    }

    /// Generate a new span ID (64-bit, 16 hex chars)
    pub fn generate_span_id() -> String {
        let random: u64 = rand_simple();
        format!("{:016x}", random)
    }

    /// Generate a correlation ID
    pub fn generate_correlation_id() -> String {
        let random: u64 = rand_simple();
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();
        format!("{:x}-{:x}", timestamp, random)
    }

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

    /// Set sampled flag
    pub fn set_sampled(&mut self, sampled: bool) {
        if sampled {
            self.trace_flags |= 0x01;
        } else {
            self.trace_flags &= !0x01;
        }
    }

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

    /// Parse from W3C traceparent header value
    pub fn from_traceparent(value: &str) -> Option<Self> {
        let parts: Vec<&str> = value.split('-').collect();
        if parts.len() != 4 {
            return None;
        }

        let version = parts[0];
        if version != "00" {
            return None; // Only version 00 is supported
        }

        let trace_id = parts[1];
        let span_id = parts[2];
        let flags = parts[3];

        // Validate lengths
        if trace_id.len() != 32 || span_id.len() != 16 || flags.len() != 2 {
            return None;
        }

        // Parse flags
        let trace_flags = u8::from_str_radix(flags, 16).ok()?;

        Some(Self {
            trace_id: trace_id.to_string(),
            span_id: span_id.to_string(),
            parent_span_id: None,
            trace_flags,
            trace_state: None,
            correlation_id: None,
        })
    }
}

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

/// Extract trace context from incoming request headers
pub fn extract_trace_context(request: &Request) -> TraceContext {
    let headers = request.headers();

    // Try to extract traceparent header
    let mut context = headers
        .get(TRACEPARENT_HEADER)
        .and_then(|v| v.to_str().ok())
        .and_then(TraceContext::from_traceparent)
        .unwrap_or_default();

    // Extract tracestate if present
    if let Some(state) = headers.get(TRACESTATE_HEADER).and_then(|v| v.to_str().ok()) {
        context.trace_state = Some(state.to_string());
    }

    // Extract correlation ID from various headers
    context.correlation_id = headers
        .get(CORRELATION_ID_HEADER)
        .or_else(|| headers.get(REQUEST_ID_HEADER))
        .or_else(|| headers.get("x-amzn-trace-id"))
        .and_then(|v| v.to_str().ok())
        .map(String::from)
        .or_else(|| Some(TraceContext::generate_correlation_id()));

    context
}

/// Inject trace context into outgoing request headers
pub fn inject_trace_context(headers: &mut http::HeaderMap, context: &TraceContext) {
    use http::header::HeaderValue;

    // Inject traceparent
    if let Ok(value) = HeaderValue::from_str(&context.to_traceparent()) {
        headers.insert(TRACEPARENT_HEADER, value);
    }

    // Inject tracestate if present
    if let Some(ref state) = context.trace_state {
        if let Ok(value) = HeaderValue::from_str(state) {
            headers.insert(TRACESTATE_HEADER, value);
        }
    }

    // Inject correlation ID
    if let Some(ref correlation_id) = context.correlation_id {
        if let Ok(value) = HeaderValue::from_str(correlation_id) {
            headers.insert(CORRELATION_ID_HEADER, value);
        }
    }
}

/// Propagate trace context to response headers
pub fn propagate_trace_context(response_headers: &mut http::HeaderMap, context: &TraceContext) {
    use http::header::HeaderValue;

    // Include trace ID in response for debugging
    if let Ok(value) = HeaderValue::from_str(&context.trace_id) {
        response_headers.insert("x-trace-id", value);
    }

    // Include correlation ID in response
    if let Some(ref correlation_id) = context.correlation_id {
        if let Ok(value) = HeaderValue::from_str(correlation_id) {
            response_headers.insert(CORRELATION_ID_HEADER, value);
        }
    }
}

/// Simple random number generator (using XorShift)
fn rand_simple() -> u64 {
    use std::cell::Cell;
    use std::time::{SystemTime, UNIX_EPOCH};

    thread_local! {
        static STATE: Cell<u64> = Cell::new(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos() as u64
        );
    }

    STATE.with(|state| {
        let mut x = state.get();
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        state.set(x);
        x
    })
}

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

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

    #[test]
    fn test_trace_context_child() {
        let parent = TraceContext::new();
        let child = parent.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));
        assert_eq!(child.correlation_id, parent.correlation_id);
    }

    #[test]
    fn test_traceparent_round_trip() {
        let ctx = TraceContext::new();
        let traceparent = ctx.to_traceparent();
        let parsed = TraceContext::from_traceparent(&traceparent).unwrap();

        assert_eq!(parsed.trace_id, ctx.trace_id);
        assert_eq!(parsed.span_id, ctx.span_id);
        assert_eq!(parsed.trace_flags, ctx.trace_flags);
    }

    #[test]
    fn test_traceparent_parsing() {
        let traceparent = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01";
        let ctx = TraceContext::from_traceparent(traceparent).unwrap();

        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_invalid_traceparent() {
        // Invalid version
        assert!(TraceContext::from_traceparent("01-abc-def-00").is_none());
        // Wrong number of parts
        assert!(TraceContext::from_traceparent("00-abc-def").is_none());
        // Invalid lengths
        assert!(TraceContext::from_traceparent("00-abc-def-00").is_none());
    }

    #[test]
    fn test_sampled_flag() {
        let mut ctx = TraceContext::new();
        assert!(ctx.is_sampled());

        ctx.set_sampled(false);
        assert!(!ctx.is_sampled());

        ctx.set_sampled(true);
        assert!(ctx.is_sampled());
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    /// **Feature: v1-features-roadmap, Property 13: Trace context propagation**
    /// **Validates: Requirements 7.3**
    ///
    /// For any distributed trace:
    /// - Child spans SHALL inherit parent trace ID
    /// - Child spans SHALL have unique span IDs
    /// - Correlation ID SHALL propagate through entire request chain
    /// - Traceparent format SHALL conform to W3C specification
    /// - Trace context SHALL survive serialization round-trip

    /// Strategy for generating trace IDs (32 hex chars)
    fn trace_id_strategy() -> impl Strategy<Value = String> {
        prop::string::string_regex("[0-9a-f]{32}").unwrap()
    }

    /// Strategy for generating span IDs (16 hex chars)
    fn span_id_strategy() -> impl Strategy<Value = String> {
        prop::string::string_regex("[0-9a-f]{16}").unwrap()
    }

    /// Strategy for generating trace flags
    fn trace_flags_strategy() -> impl Strategy<Value = u8> {
        0u8..=255
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property 13: Generated trace IDs are unique
        #[test]
        fn prop_trace_ids_unique(_seed in 0u32..100) {
            let ctx1 = TraceContext::new();
            let ctx2 = TraceContext::new();

            // Each generation should produce unique IDs
            prop_assert_ne!(ctx1.trace_id, ctx2.trace_id);
            prop_assert_ne!(ctx1.span_id, ctx2.span_id);
        }

        /// Property 13: Generated IDs have correct format
        #[test]
        fn prop_generated_ids_format(_seed in 0u32..100) {
            let ctx = TraceContext::new();

            // Trace ID: 32 hex chars
            prop_assert_eq!(ctx.trace_id.len(), 32);
            prop_assert!(ctx.trace_id.chars().all(|c| c.is_ascii_hexdigit()));

            // Span ID: 16 hex chars
            prop_assert_eq!(ctx.span_id.len(), 16);
            prop_assert!(ctx.span_id.chars().all(|c| c.is_ascii_hexdigit()));
        }

        /// Property 13: Child spans inherit parent trace ID
        #[test]
        fn prop_child_inherits_trace_id(_seed in 0u32..100) {
            let parent = TraceContext::new();
            let child = parent.child();

            // Child MUST have same trace_id as parent
            prop_assert_eq!(child.trace_id, parent.trace_id);

            // Child MUST have different span_id
            prop_assert_ne!(child.span_id, parent.span_id.clone());

            // Child's parent_span_id MUST be parent's span_id
            prop_assert_eq!(child.parent_span_id, Some(parent.span_id.clone()));
        }

        /// Property 13: Multi-level trace propagation preserves trace ID
        #[test]
        fn prop_multilevel_trace_propagation(_seed in 0u32..100) {
            let root = TraceContext::new();
            let child1 = root.child();
            let child2 = child1.child();
            let child3 = child2.child();

            // All spans in the chain MUST have same trace_id
            prop_assert_eq!(child1.trace_id, root.trace_id.clone());
            prop_assert_eq!(child2.trace_id, root.trace_id.clone());
            prop_assert_eq!(child3.trace_id, root.trace_id.clone());

            // Each span MUST have unique span_id
            let span_ids = vec![&root.span_id, &child1.span_id, &child2.span_id, &child3.span_id];
            for i in 0..span_ids.len() {
                for j in (i+1)..span_ids.len() {
                    prop_assert_ne!(span_ids[i], span_ids[j]);
                }
            }

            // Parent relationships MUST be correct
            prop_assert_eq!(child1.parent_span_id, Some(root.span_id.clone()));
            prop_assert_eq!(child2.parent_span_id, Some(child1.span_id.clone()));
            prop_assert_eq!(child3.parent_span_id, Some(child2.span_id.clone()));
        }

        /// Property 13: Correlation ID propagates through chain
        #[test]
        fn prop_correlation_id_propagation(_seed in 0u32..100) {
            let root = TraceContext::new();
            let correlation_id = root.correlation_id.clone();

            let child1 = root.child();
            let child2 = child1.child();

            // Correlation ID MUST propagate through entire chain
            prop_assert_eq!(child1.correlation_id, correlation_id.clone());
            prop_assert_eq!(child2.correlation_id, correlation_id.clone());
        }

        /// Property 13: Traceparent format conforms to W3C spec
        #[test]
        fn prop_traceparent_format(
            trace_id in trace_id_strategy(),
            span_id in span_id_strategy(),
            flags in trace_flags_strategy(),
        ) {
            let ctx = TraceContext {
                trace_id: trace_id.clone(),
                span_id: span_id.clone(),
                parent_span_id: None,
                trace_flags: flags,
                trace_state: None,
                correlation_id: None,
            };

            let traceparent = ctx.to_traceparent();

            // Format: version-trace_id-span_id-flags
            let parts: Vec<&str> = traceparent.split('-').collect();
            prop_assert_eq!(parts.len(), 4);

            // Version must be "00"
            prop_assert_eq!(parts[0], "00");

            // Trace ID must match (32 hex chars)
            prop_assert_eq!(parts[1], trace_id);
            prop_assert_eq!(parts[1].len(), 32);

            // Span ID must match (16 hex chars)
            prop_assert_eq!(parts[2], span_id);
            prop_assert_eq!(parts[2].len(), 16);

            // Flags must be 2 hex chars
            prop_assert_eq!(parts[3].len(), 2);
            prop_assert_eq!(parts[3], format!("{:02x}", flags));
        }

        /// Property 13: Traceparent round-trip preserves data
        #[test]
        fn prop_traceparent_roundtrip(
            trace_id in trace_id_strategy(),
            span_id in span_id_strategy(),
            flags in trace_flags_strategy(),
        ) {
            let original = TraceContext {
                trace_id: trace_id.clone(),
                span_id: span_id.clone(),
                parent_span_id: None,
                trace_flags: flags,
                trace_state: None,
                correlation_id: None,
            };

            // Serialize to traceparent
            let traceparent = original.to_traceparent();

            // Deserialize back
            let parsed = TraceContext::from_traceparent(&traceparent).unwrap();

            // All fields must match
            prop_assert_eq!(parsed.trace_id, original.trace_id);
            prop_assert_eq!(parsed.span_id, original.span_id);
            prop_assert_eq!(parsed.trace_flags, original.trace_flags);
        }

        /// Property 13: Sampled flag is correctly encoded/decoded
        #[test]
        fn prop_sampled_flag_encoding(sampled in proptest::bool::ANY) {
            let mut ctx = TraceContext::new();
            ctx.set_sampled(sampled);

            // Sampled flag should be reflected in is_sampled()
            prop_assert_eq!(ctx.is_sampled(), sampled);

            // Sampled flag should survive serialization
            let traceparent = ctx.to_traceparent();
            let parsed = TraceContext::from_traceparent(&traceparent).unwrap();
            prop_assert_eq!(parsed.is_sampled(), sampled);
        }

        /// Property 13: Invalid traceparent strings are rejected
        #[test]
        fn prop_invalid_traceparent_rejected(
            invalid_version in "0[1-9]|[1-9][0-9]",
            trace_id in "[0-9a-f]{10,50}",
            span_id in "[0-9a-f]{8,20}",
            flags in "[0-9a-f]{1,4}",
        ) {
            // Wrong version
            let invalid1 = format!("{}-{}-{}-{}", invalid_version, trace_id, span_id, flags);
            prop_assert!(TraceContext::from_traceparent(&invalid1).is_none());

            // Missing parts
            let invalid2 = format!("00-{}-{}", trace_id, span_id);
            prop_assert!(TraceContext::from_traceparent(&invalid2).is_none());
        }

        /// Property 13: Trace state propagation
        #[test]
        fn prop_trace_state_propagation(state in "[a-z0-9=,]{5,50}") {
            let mut ctx = TraceContext::new();
            ctx.trace_state = Some(state.clone());

            let child = ctx.child();

            // Trace state MUST propagate to child
            prop_assert_eq!(child.trace_state, Some(state));
        }

        /// Property 13: Correlation ID format is valid
        #[test]
        fn prop_correlation_id_format(_seed in 0u32..100) {
            let ctx = TraceContext::new();

            prop_assert!(ctx.correlation_id.is_some());
            let corr_id = ctx.correlation_id.unwrap();

            // Should be non-empty
            prop_assert!(!corr_id.is_empty());

            // Should contain hex characters and hyphen
            prop_assert!(corr_id.contains('-'));

            // Parts should be hex
            let parts: Vec<&str> = corr_id.split('-').collect();
            prop_assert_eq!(parts.len(), 2);
            prop_assert!(parts[0].chars().all(|c| c.is_ascii_hexdigit()));
            prop_assert!(parts[1].chars().all(|c| c.is_ascii_hexdigit()));
        }

        /// Property 13: Header injection and extraction preserves context
        #[test]
        fn prop_header_injection_extraction(
            trace_id in trace_id_strategy(),
            span_id in span_id_strategy(),
            flags in trace_flags_strategy(),
        ) {
            let original = TraceContext {
                trace_id: trace_id.clone(),
                span_id: span_id.clone(),
                parent_span_id: None,
                trace_flags: flags,
                trace_state: None,
                correlation_id: Some("test-corr-id".to_string()),
            };

            // Inject into headers
            let mut headers = http::HeaderMap::new();
            inject_trace_context(&mut headers, &original);

            // Headers should contain traceparent
            prop_assert!(headers.contains_key(TRACEPARENT_HEADER));

            // Extract traceparent back
            let traceparent_value = headers.get(TRACEPARENT_HEADER).unwrap().to_str().unwrap();
            let extracted = TraceContext::from_traceparent(traceparent_value).unwrap();

            // Verify trace context is preserved
            prop_assert_eq!(extracted.trace_id, original.trace_id);
            prop_assert_eq!(extracted.span_id, original.span_id);
            prop_assert_eq!(extracted.trace_flags, original.trace_flags);
        }
    }
}