#![cfg(all(test, feature = "metrics"))]
use crate::observability::otel::{CardinalityOverflow, MetricsConfig};
#[test]
fn audit_default_cardinality_limits_prevent_dos() {
println!("🔍 AUDIT: Default cardinality limits DoS protection");
let config = MetricsConfig::default();
println!("📊 Default cardinality configuration:");
println!(" max_cardinality: {}", config.max_cardinality);
println!(" max_metrics: {}", config.max_metrics);
println!(" overflow_strategy: {:?}", config.overflow_strategy);
assert_eq!(
config.max_cardinality, 1000,
"DEFAULT CARDINALITY VIOLATION: max_cardinality should be 1000 for bounded memory"
);
assert_eq!(
config.max_metrics, 4096,
"METRIC NAME CAP MISSING: max_metrics should limit distinct metric names"
);
assert_eq!(
config.overflow_strategy,
CardinalityOverflow::Drop,
"UNSAFE OVERFLOW STRATEGY: default should Drop (bounded) not Warn (unbounded)"
);
println!("✅ CARDINALITY LIMITS: Default configuration prevents metric explosion DoS");
println!(
" ✓ Bounded attribute combinations: {} per metric",
config.max_cardinality
);
println!(" ✓ Bounded metric names: {}", config.max_metrics);
println!(" ✓ Safe overflow strategy: Drop (not unbounded growth)");
}
#[test]
fn audit_unsafe_overflow_strategies_not_default() {
println!("🔍 AUDIT: Unsafe overflow strategies require explicit opt-in");
let warn_config = MetricsConfig::default().with_overflow_strategy(CardinalityOverflow::Warn);
assert_eq!(warn_config.overflow_strategy, CardinalityOverflow::Warn);
let aggregate_config =
MetricsConfig::default().with_overflow_strategy(CardinalityOverflow::Aggregate);
assert_eq!(
aggregate_config.overflow_strategy,
CardinalityOverflow::Aggregate
);
let default_config = MetricsConfig::default();
assert_eq!(
default_config.overflow_strategy,
CardinalityOverflow::Drop,
"SECURITY VIOLATION: Default overflow strategy must be Drop (bounded memory)"
);
println!("✅ OVERFLOW STRATEGY SAFETY: Unsafe options available but require explicit opt-in");
println!(" ✓ Warn strategy: Available (unbounded - for debugging only)");
println!(" ✓ Aggregate strategy: Available (bounded fallback)");
println!(" ✓ Drop strategy: DEFAULT (bounded - memory safe)");
}
#[test]
fn audit_cardinality_configuration_tunability() {
println!("🔍 AUDIT: Cardinality limits are tunable with safe defaults");
let custom_config = MetricsConfig::new()
.with_max_cardinality(500)
.with_max_metrics(2048)
.with_overflow_strategy(CardinalityOverflow::Aggregate);
assert_eq!(custom_config.max_cardinality, 500);
assert_eq!(custom_config.max_metrics, 2048);
assert_eq!(
custom_config.overflow_strategy,
CardinalityOverflow::Aggregate
);
let minimal_config = MetricsConfig::default().with_max_cardinality(1);
assert_eq!(minimal_config.max_cardinality, 1);
let high_config = MetricsConfig::default().with_max_cardinality(10000);
assert_eq!(high_config.max_cardinality, 10000);
println!("✅ CONFIGURATION TUNABILITY: Cardinality limits are adjustable");
println!(
" ✓ Custom cardinality: {} (operator choice)",
custom_config.max_cardinality
);
println!(
" ✓ Custom metric cap: {} (operator choice)",
custom_config.max_metrics
);
println!(
" ✓ Custom strategy: {:?} (operator choice)",
custom_config.overflow_strategy
);
}
#[test]
fn audit_otlp_best_practice_compliance() {
println!("🔍 AUDIT: OTLP cardinality best practice compliance");
let config = MetricsConfig::default();
assert!(
config.max_cardinality > 0 && config.max_cardinality <= 10000,
"OTLP VIOLATION: cardinality must be bounded and reasonable (1-10k)"
);
assert!(
matches!(
config.overflow_strategy,
CardinalityOverflow::Drop | CardinalityOverflow::Aggregate
),
"OTLP VIOLATION: overflow strategy must prevent unbounded growth"
);
assert!(
config.max_metrics > 0,
"OTLP VIOLATION: metric name count must be bounded"
);
let tuned = MetricsConfig::default().with_max_cardinality(2000);
assert_eq!(tuned.max_cardinality, 2000, "Configuration must be tunable");
println!("✅ OTLP BEST PRACTICE COMPLIANCE: Full compliance verified");
println!(
" ✓ Bounded memory: max {} attributes per metric",
config.max_cardinality
);
println!(
" ✓ Bounded metrics: max {} distinct metric names",
config.max_metrics
);
println!(
" ✓ Safe overflow: {:?} strategy (prevents DoS)",
config.overflow_strategy
);
println!(" ✓ Configurable: Operators can tune limits for environment");
}