#[cfg(test)]
mod tests {
use crate::observability::w3c_trace_context::{W3CTraceContext, TraceFlags, TraceId, SpanId};
#[test]
fn otlp_parent_sampling_propagation_audit() {
eprintln!("\n🔍 OTLP PARENT SAMPLING PROPAGATION AUDIT");
eprintln!("===========================================");
{
let parent = W3CTraceContext::new_root(); let child = parent.create_child();
eprintln!("\n📋 Test 1: Sampled parent → sampled child");
eprintln!(" Parent sampled: {}", parent.flags.is_sampled());
eprintln!(" Child sampled: {}", child.flags.is_sampled());
assert!(
parent.flags.is_sampled(),
"Root span should be sampled by default"
);
assert!(
child.flags.is_sampled(),
"Child should inherit SAMPLED decision from parent"
);
eprintln!(" ✅ CORRECT: Child inherits SAMPLED decision");
}
{
let mut parent = W3CTraceContext::new_root();
parent.flags = TraceFlags::NONE; let child = parent.create_child();
eprintln!("\n📋 Test 2: Non-sampled parent → non-sampled child");
eprintln!(" Parent sampled: {}", parent.flags.is_sampled());
eprintln!(" Child sampled: {}", child.flags.is_sampled());
assert!(
!parent.flags.is_sampled(),
"Parent should NOT be sampled when explicitly set to NONE"
);
assert!(
!child.flags.is_sampled(),
"Child should inherit NON-SAMPLED decision from parent"
);
eprintln!(" ✅ CORRECT: Child inherits NON-SAMPLED decision");
}
{
let mut parent = W3CTraceContext::new_root();
parent.flags = TraceFlags::NONE; let child = parent.create_child();
let grandchild = child.create_child();
eprintln!("\n📋 Test 3: Multi-level inheritance");
eprintln!(" Grandparent sampled: {}", parent.flags.is_sampled());
eprintln!(" Parent sampled: {}", child.flags.is_sampled());
eprintln!(" Grandchild sampled: {}", grandchild.flags.is_sampled());
assert!(
!parent.flags.is_sampled() && !child.flags.is_sampled() && !grandchild.flags.is_sampled(),
"All generations should inherit NON-SAMPLED decision from root"
);
eprintln!(" ✅ CORRECT: Sampling decision propagates through multiple generations");
}
{
let mut parent = W3CTraceContext::new_root();
parent.flags = TraceFlags::NONE;
let child = parent.create_child();
eprintln!("\n📋 Test 4: Trace ID consistency with sampling");
eprintln!(" Parent trace_id: {}", parent.trace_id.to_hex());
eprintln!(" Child trace_id: {}", child.trace_id.to_hex());
assert_eq!(
parent.trace_id.to_hex(),
child.trace_id.to_hex(),
"Child should maintain same trace_id as parent"
);
eprintln!(" ✅ CORRECT: Trace ID preserved with sampling decision");
}
eprintln!("\n🎯 AUDIT CONCLUSION");
eprintln!("==================");
eprintln!("✅ SOUND: Parent sampling decision properly propagates to children");
eprintln!("✅ Prevents trace fragmentation per W3C trace-context specification");
eprintln!("✅ Non-sampled parents produce non-sampled children (no independent sampling)");
eprintln!("✅ Multi-generation inheritance works correctly");
eprintln!("✅ Trace ID consistency maintained with sampling decisions");
eprintln!("");
eprintln!("📌 BEHAVIOR: Current implementation correctly implements option (a):");
eprintln!(" Parent decision propagates (correct: prevents trace fragmentation)");
eprintln!(" NOT option (b): Independent sampling (incorrect: causes fragmentation)");
}
#[test]
fn w3c_trace_context_sampling_compliance() {
eprintln!("\n🌐 W3C TRACE CONTEXT SAMPLING COMPLIANCE AUDIT");
eprintln!("==============================================");
{
let sampled_flags = TraceFlags::SAMPLED;
assert!(sampled_flags.is_sampled());
assert_eq!(sampled_flags.bits(), 0x01);
let none_flags = TraceFlags::NONE;
assert!(!none_flags.is_sampled());
assert_eq!(none_flags.bits(), 0x00);
eprintln!("✅ TraceFlags implementation matches W3C specification");
}
eprintln!("\n📋 COMPLIANCE VERIFIED:");
eprintln!(" ✅ TraceFlags::SAMPLED = 0x01 (per W3C spec)");
eprintln!(" ✅ TraceFlags::NONE = 0x00 (not sampled)");
eprintln!(" ✅ is_sampled() correctly interprets flags");
eprintln!(" ✅ W3CTraceContext::create_child() preserves parent flags");
}
#[test]
#[should_panic(expected = "DEFECTIVE: Independent sampling breaks W3C compliance")]
fn demonstrate_defective_independent_sampling() {
eprintln!("\n❌ DEMONSTRATING DEFECTIVE INDEPENDENT SAMPLING");
eprintln!("==============================================");
struct DefectiveSpan {
sampled: bool,
}
impl DefectiveSpan {
fn new_independent_sampled(_name: &str) -> Self {
Self { sampled: true } }
}
let parent_sampled = false;
let child = DefectiveSpan::new_independent_sampled("defective_child");
eprintln!("Parent sampled: {}", parent_sampled);
eprintln!("Child sampled: {}", child.sampled);
if parent_sampled != child.sampled {
eprintln!("💥 TRACE FRAGMENTATION: Parent and child have different sampling decisions!");
eprintln!("This breaks distributed tracing and violates W3C trace-context spec!");
panic!("DEFECTIVE: Independent sampling breaks W3C compliance");
}
}
}