asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
//! OTLP-Trace resource attributes inheritance audit test.
//!
//! **AUDIT SCOPE**: Verifies that spans created in regions with resource attributes
//! properly inherit those attributes in OTLP trace exports per OTLP-Trace SDK specification.
//!
//! **OTLP-TRACE SDK REQUIREMENT**:
//! - Resource attributes MUST be included in ResourceSpans.resource field
//! - Regional context attributes should flow to exported spans (regional attribution)
//! - Global resource attributes should be merged with regional attributes
//! - NOT: only global resource sent (loses regional context)
//!
//! **CRITICAL**: Loss of regional attribution breaks distributed tracing attribution
//! and makes it impossible to correlate spans with their originating context.

#![cfg(test)]

use std::collections::BTreeMap;

/// **AUDIT TEST**: Verify OTLP resource attributes inheritance from regions.
///
/// **SCENARIO**: Span created in region with custom resource attributes.
/// **REQUIREMENT**: Regional resource attributes MUST flow to OTLP export ResourceSpans.
/// **ASSESSMENT**: DEFECT - regional resource attributes not inherited in export.
#[test]
fn audit_otlp_resource_attributes_regional_inheritance() {
    println!("🔍 AUDIT: OTLP resource attributes regional inheritance");

    // Demonstrate the expected OTLP export structure
    println!("📋 OTLP-Trace specification requirements:");
    println!("   • TracesData.resource_spans[].resource.attributes: Resource attributes");
    println!("   • TracesData.resource_spans[].scope_spans[].spans[]: Individual spans");
    println!("   • Resource attributes describe service/process/region context");
    println!("   • Span attributes describe specific operation details");

    // Test scenario: Region with custom resource attributes
    let global_resource_attributes = create_global_resource_attributes();
    let regional_resource_attributes = create_regional_resource_attributes();

    println!("📊 Test scenario setup:");
    println!("   Global resource attributes:");
    for (key, value) in &global_resource_attributes {
        println!("      {}: {}", key, value);
    }
    println!("   Regional resource attributes:");
    for (key, value) in &regional_resource_attributes {
        println!("      {}: {}", key, value);
    }

    // Current implementation analysis
    println!("📋 Current OTLP export implementation:");
    println!("   • OtlpSpan struct: includes span-level attributes only");
    println!("   • SpanBatch: collection of OtlpSpan objects");
    println!("   • Resource attributes: NOT FOUND in span export structure");

    println!("🚨 DEFECT ANALYSIS:");
    println!("   ✗ MISSING: Regional resource attributes not captured during span creation");
    println!("   ✗ MISSING: ResourceSpans.resource field not populated with regional context");
    println!("   ✗ MISSING: Merge logic for global + regional resource attributes");
    println!("   ✗ IMPACT: Spans lose regional attribution in OTLP export");

    // Verify the defect exists
    assert!(
        !current_implementation_includes_regional_resource_attributes(),
        "OTLP export must include regional resource attributes for proper attribution"
    );

    println!("🚨 OTLP RESOURCE ATTRIBUTES INHERITANCE: DEFECT - regional attributes not exported");
}

/// **AUDIT TEST**: Verify OTLP export structure compliance with specification.
///
/// **SCENARIO**: OTLP export should follow TracesData → ResourceSpans → ScopeSpans → Spans hierarchy.
/// **REQUIREMENT**: Resource attributes must be at ResourceSpans level, not span level.
/// **ASSESSMENT**: DEFECT - export structure doesn't include ResourceSpans.resource.
#[test]
fn audit_otlp_export_structure_compliance() {
    println!("🔍 AUDIT: OTLP export structure compliance");

    println!("📋 OTLP-Trace specification structure:");
    println!("   TracesData {{");
    println!("     resource_spans: [");
    println!("       ResourceSpans {{");
    println!("         resource: Resource {{");
    println!("           attributes: [KeyValue] // ← RESOURCE ATTRIBUTES HERE");
    println!("         }},");
    println!("         scope_spans: [");
    println!("           ScopeSpans {{");
    println!("             spans: [");
    println!("               Span {{");
    println!("                 attributes: [KeyValue] // ← SPAN ATTRIBUTES HERE");
    println!("               }}");
    println!("             ]");
    println!("           }}");
    println!("         ]");
    println!("       }}");
    println!("     ]");
    println!("   }}");

    println!("📊 Current implementation structure:");
    println!("   SpanBatch {{");
    println!("     batch_id: u64,");
    println!("     spans: Vec<OtlpSpan>, // ← Only span-level data");
    println!("     created_at: Instant,");
    println!("   }}");
    println!("   ");
    println!("   OtlpSpan {{");
    println!("     span_id: String,");
    println!("     name: String,");
    println!("     attributes: Vec<(String, String)>, // ← Only span attributes");
    println!("     // NO RESOURCE ATTRIBUTES");
    println!("   }}");

    println!("🚨 STRUCTURAL DEFECTS:");
    println!("   ✗ MISSING: ResourceSpans wrapper with resource field");
    println!("   ✗ MISSING: Resource.attributes for service/region context");
    println!(
        "   ✗ MISSING: Proper OTLP export hierarchy (TracesData → ResourceSpans → ScopeSpans)"
    );
    println!("   ✗ IMPACT: Non-compliant with OTLP-Trace specification");

    assert!(
        !current_otlp_export_structure_includes_resource_spans(),
        "OTLP export structure must include ResourceSpans with resource attributes"
    );
}

/// **AUDIT TEST**: Demonstrate regional attribution use case.
///
/// **SCENARIO**: Multiple regions with different resource attributes export spans.
/// **REQUIREMENT**: Each ResourceSpans should have distinct regional resource attributes.
/// **ASSESSMENT**: USE CASE - why regional attribution matters for debugging.
#[test]
fn audit_regional_attribution_use_case() {
    println!("🔍 AUDIT: Regional attribution use case demonstration");

    println!("📋 Multi-region distributed trace scenario:");
    println!("   Region A (us-east-1):");
    println!("     service.region: us-east-1");
    println!("     service.datacenter: aws-use1");
    println!("     service.instance: web-01");
    println!("   ");
    println!("   Region B (eu-west-1):");
    println!("     service.region: eu-west-1");
    println!("     service.datacenter: aws-euw1");
    println!("     service.instance: web-02");

    println!("📊 Expected OTLP export with regional attribution:");
    println!("   TracesData {{");
    println!("     resource_spans: [");
    println!("       ResourceSpans {{");
    println!("         resource: {{ service.region: \"us-east-1\", ... }},");
    println!("         scope_spans: [{{ spans: [span_a1, span_a2] }}]");
    println!("       }},");
    println!("       ResourceSpans {{");
    println!("         resource: {{ service.region: \"eu-west-1\", ... }},");
    println!("         scope_spans: [{{ spans: [span_b1, span_b2] }}]");
    println!("       }}");
    println!("     ]");
    println!("   }}");

    println!("📊 Current implementation (loses regional context):");
    println!("   SpanBatch {{ spans: [span_a1, span_a2, span_b1, span_b2] }}");
    println!("   // ALL spans in single batch, no regional resource differentiation");

    println!("🚨 ATTRIBUTION IMPACT:");
    println!("   ✗ LOST: Cannot identify which spans came from which region");
    println!("   ✗ LOST: Cannot correlate performance issues with regional infrastructure");
    println!("   ✗ LOST: Cannot apply region-specific sampling or filtering");
    println!("   ✗ IMPACT: Distributed tracing attribution is broken");

    // This test documents the use case - it always passes
    assert!(true, "Regional attribution use case documented");
}

/// **AUDIT TEST**: Document the fix strategy for resource attributes inheritance.
///
/// **SCENARIO**: Provide implementation guidance for proper regional resource attribution.
/// **REQUIREMENT**: Clear steps to implement compliant OTLP resource attribute inheritance.
/// **ASSESSMENT**: PLANNING - document implementation approach.
#[test]
fn audit_resource_attributes_inheritance_fix_strategy() {
    println!("🔍 AUDIT: Resource attributes inheritance fix strategy");

    println!("📋 IMPLEMENTATION PLAN: Proper OTLP resource attribute inheritance");

    println!("📊 Phase 1: Extend span creation to capture regional context");
    println!("   1. Modify span creation to capture DiagnosticContext.region_id");
    println!("   2. Add region_resource_attributes lookup during span creation");
    println!("   3. Store resource attributes alongside span data for export");

    println!("📊 Phase 2: Restructure OTLP export format");
    println!("   1. Replace SpanBatch with proper OTLP TracesData structure:");
    println!("      TracesData {{ resource_spans: Vec<ResourceSpans> }}");
    println!("   2. Group spans by resource attributes (regional attribution)");
    println!("   3. Create ResourceSpans with proper resource.attributes field");

    println!("📊 Phase 3: Implement resource attribute merging");
    println!("   1. Merge global service attributes with regional attributes");
    println!("   2. Handle attribute precedence (regional overrides global)");
    println!("   3. Ensure resource attributes are immutable per ResourceSpans");

    println!("📊 Phase 4: Update exporter to emit proper OTLP format");
    println!("   1. Modify LoadSheddingTraceExporter to handle ResourceSpans");
    println!("   2. Update export logic to preserve resource grouping");
    println!("   3. Ensure OTLP wire format compliance");

    println!("📋 VERIFICATION REQUIREMENTS:");
    println!("   1. Spans from different regions appear in separate ResourceSpans");
    println!("   2. Resource.attributes include regional context");
    println!("   3. OTLP export follows TracesData → ResourceSpans → ScopeSpans hierarchy");
    println!("   4. Regional attribution preserved through export pipeline");

    println!("✅ IMPLEMENTATION STRATEGY DOCUMENTED");

    // This test always passes - it's documentation
    assert!(
        true,
        "Resource attributes inheritance fix strategy documented"
    );
}

// Helper functions to document current implementation state

fn create_global_resource_attributes() -> BTreeMap<String, String> {
    let mut attrs = BTreeMap::new();
    attrs.insert("service.name".to_string(), "asupersync".to_string());
    attrs.insert("service.version".to_string(), "0.1.0".to_string());
    attrs.insert("process.pid".to_string(), "12345".to_string());
    attrs
}

fn create_regional_resource_attributes() -> BTreeMap<String, String> {
    let mut attrs = BTreeMap::new();
    attrs.insert("service.region".to_string(), "us-east-1".to_string());
    attrs.insert("service.datacenter".to_string(), "aws-use1".to_string());
    attrs.insert("service.instance".to_string(), "web-01".to_string());
    attrs.insert("region.id".to_string(), "R123456789".to_string());
    attrs
}

fn current_implementation_includes_regional_resource_attributes() -> bool {
    // Current OtlpSpan struct:
    // - span_id: String
    // - name: String
    // - start_time_unix_nano: u64
    // - end_time_unix_nano: u64
    // - attributes: Vec<(String, String)>  // Only span-level attributes
    // - trace_flags: Option<u8>
    //
    // Current SpanBatch struct:
    // - batch_id: u64
    // - spans: Vec<OtlpSpan>
    // - created_at: Instant
    //
    // MISSING:
    // - No resource attributes field
    // - No ResourceSpans structure
    // - No regional context capture
    false
}

fn current_otlp_export_structure_includes_resource_spans() -> bool {
    // Current export uses SpanBatch which doesn't follow OTLP specification:
    // - No TracesData wrapper
    // - No ResourceSpans grouping
    // - No Resource.attributes field
    // - Non-compliant with OTLP-Trace specification
    false
}