use super::*;
#[must_use]
pub(crate) fn host_observability_platform() -> (ObservabilityPlatform, &'static str, &'static str) {
match OS {
OBS_OS_KEY_MACOS => (ObservabilityPlatform::Macos, OBS_OS_KEY_MACOS, OBS_OS_MACOS),
OBS_OS_KEY_WINDOWS => (
ObservabilityPlatform::Windows,
OBS_OS_KEY_WINDOWS,
OBS_OS_WINDOWS,
),
_ => (ObservabilityPlatform::Linux, OBS_OS_KEY_LINUX, OBS_OS_LINUX),
}
}
#[must_use]
fn platform_ext_id(platform_key: &str, suffix: &str) -> String {
format!("platform.{platform_key}.{suffix}")
}
#[must_use]
pub(crate) fn probe_destroyed_owner_live_objects() -> u64 {
reset_reactive_runtime();
let probe_sig: Signal<u32> = Signal::new(1);
let (scope, _memo): (Scope, Memo<u32>) = Scope::run(|_: Scope| {
let derived: Memo<u32> = Memo::new(move || probe_sig.get().wrapping_mul(3));
let _: u32 = derived.get();
derived
});
scope.dispose();
if probe_sig.has_subscribers() { 1 } else { 0 }
}
#[must_use]
pub fn emit_stress_observability_report(
report: &StressMatrixReport,
commit: &str,
profile: &str,
) -> ObservabilityReportV1 {
let (platform, platform_key, os_name): (ObservabilityPlatform, &'static str, &'static str) =
host_observability_platform();
let resolved_commit: String = if commit.trim().is_empty() {
OBSERVABILITY_DEFAULT_COMMIT.to_string()
} else {
commit.trim().to_string()
};
let resolved_profile: String = if profile.trim().is_empty() {
OBSERVABILITY_DEFAULT_PROFILE.to_string()
} else {
profile.trim().to_string()
};
let total_cycles: u64 = report
.get_entries()
.iter()
.map(|entry: &StressMatrixEntry| entry.get_latency().get_iterations() as u64)
.sum();
let primary_latency: LatencyDistribution = report
.partial_damage_ab()
.map(PartialDamageAbSummary::get_candidate_partial_damage)
.or_else(|| {
report
.get_entries()
.first()
.map(StressMatrixEntry::get_latency)
})
.unwrap_or_default();
let primary_iters: u64 = primary_latency.get_iterations().max(1) as u64;
let frame_p95_ms: f64 = primary_latency.get_p95_us() / 1_000.0;
let frame_p99_ms: f64 = primary_latency.get_p99_us() / 1_000.0;
let frame_p99_9_ms: f64 = primary_latency.get_p99_9_us() / 1_000.0;
let dirty_ratio: f64 = primary_latency.get_mean_dirty_area_ratio().clamp(0.0, 1.0);
let full_redraws: u64 = primary_latency.get_full_redraw_count() as u64;
let partial_redraws: u64 = primary_iters.saturating_sub(full_redraws);
let mut layout_invalidations: u64 = 0;
let mut layout_passes: u64 = 0;
let mut layout_changed_passes: u64 = 0;
let mut layout_changed_nodes: u64 = 0;
let mut draw_commands_total: u64 = 0;
let mut pixels_shaded_total: u64 = 0;
let mut steady_frames: u64 = 0;
let mut steady_allocs: u64 = 0;
let mut steady_frees: u64 = 0;
let mut steady_bytes: u64 = 0;
let mut peak_live_bytes: u64 = 0;
let mut over_240_steady: u64 = 0;
let mut over_360_steady: u64 = 0;
let mut over_480_steady: u64 = 0;
for entry in report.get_entries().iter() {
let lat: LatencyDistribution = entry.get_latency();
layout_invalidations = layout_invalidations.saturating_add(lat.get_layout_invalidations());
layout_passes = layout_passes.saturating_add(lat.get_layout_passes());
layout_changed_passes =
layout_changed_passes.saturating_add(lat.get_layout_changed_passes());
layout_changed_nodes =
layout_changed_nodes.saturating_add(lat.get_layout_changed_nodes_total());
draw_commands_total = draw_commands_total.saturating_add(lat.get_draw_commands_total());
pixels_shaded_total = pixels_shaded_total.saturating_add(lat.get_pixels_shaded_total());
peak_live_bytes = peak_live_bytes.max(lat.get_peak_live_bytes());
if entry.get_name().as_str() != STRESS_COLD_START_TTFF_NAME {
steady_frames = steady_frames.saturating_add(lat.get_iterations() as u64);
steady_allocs = steady_allocs.saturating_add(lat.get_alloc_count_total());
steady_frees = steady_frees.saturating_add(lat.get_free_count_total());
steady_bytes = steady_bytes.saturating_add(lat.get_bytes_allocated_total());
over_240_steady = over_240_steady.saturating_add(lat.get_over_4_17ms_count() as u64);
over_360_steady = over_360_steady.saturating_add(lat.get_over_2_78ms_count() as u64);
over_480_steady = over_480_steady.saturating_add(lat.get_over_2_08ms_count() as u64);
}
}
let layout_changed_passes_clamped: u64 = layout_changed_passes.min(layout_passes);
let steady_denom: f64 = steady_frames.max(1) as f64;
let missed_240hz_pct: f64 =
(((over_240_steady as f64) / steady_denom) * 100.0).clamp(0.0, 100.0);
let missed_360hz_pct: f64 =
(((over_360_steady as f64) / steady_denom) * 100.0).clamp(0.0, 100.0);
let missed_480hz_pct: f64 =
(((over_480_steady as f64) / steady_denom) * 100.0).clamp(0.0, 100.0);
let ttff_owned: ColdStartTtffReport = match report.cold_start_ttff() {
Some(existing) => existing.clone(),
None => run_cold_start_ttff_report(1),
};
let ttff_med: ColdStartTtffStages = ttff_owned.get_median();
let ttff_samples: u64 = ttff_owned.get_iterations() as u64;
let destroyed_live: u64 = probe_destroyed_owner_live_objects();
let alloc_tracked: bool = allocation_tracking_active();
let ev_refs: Vec<String> = vec![OBS_EVIDENCE_ID_STRESS.to_string()];
let mut findings: Vec<ObservabilityFinding> = Vec::new();
let mut failed: bool = false;
if let Some(ab) = report.partial_damage_ab()
&& !ab.get_bit_exact_parity()
{
failed = true;
findings.push(ObservabilityFinding::new(
ObservabilityFindingClass::BudgetViolation,
Some(OBS_OWNER_PARTIAL_DAMAGE.to_string()),
Some(OBS_FINDING_PARITY_MISMATCH.to_string()),
ev_refs.clone(),
));
}
if frame_p95_ms > OBS_THRESHOLD_FRAME_P95_MS || destroyed_live > 0 {
failed = true;
}
let mut metrics: Vec<ObservabilityMetric> = vec![
ObservabilityMetric::measured_common(
OBS_METRIC_FRAME_INTERVAL,
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::P95,
frame_p95_ms,
)
.with_samples(primary_iters)
.with_threshold(ObservabilityThreshold::new(
ObservabilityThresholdRule::Max,
Some(OBS_THRESHOLD_FRAME_P95_MS),
OBS_UNIT_MS.to_string(),
OBSERVABILITY_BUDGET_VERSION_V1.to_string(),
))
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_FRAME_P99_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::P99,
frame_p99_ms,
)
.with_samples(primary_iters)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_FRAME_P99_9_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Value,
frame_p99_9_ms,
)
.with_samples(primary_iters)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_FRAME_COUNT,
OBS_SCOPE_FRAME,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
total_cycles as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_FRAME_MISSED_VSYNC,
OBS_SCOPE_FRAME,
ObservabilityUnit::Percent,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Value,
missed_240hz_pct,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_MISSED_240HZ_PCT),
OBS_SCOPE_FRAME,
ObservabilityUnit::Percent,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Value,
missed_240hz_pct,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_MISSED_360HZ_PCT),
OBS_SCOPE_FRAME,
ObservabilityUnit::Percent,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Value,
missed_360hz_pct,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_MISSED_480HZ_PCT),
OBS_SCOPE_FRAME,
ObservabilityUnit::Percent,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Value,
missed_480hz_pct,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_STARTUP_READY_MS,
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_ready_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_STARTUP_FIRST_PRESENT_MS,
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_total_ttff_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_INIT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_app_renderer_init_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_FONT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_font_ready_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_MOUNT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_initial_mount_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_LAYOUT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_first_layout_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_PAINT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_first_paint_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_TTFF_PRESENT_MS),
OBS_SCOPE_FRAME,
ObservabilityUnit::Ms,
OBS_WINDOW_COLD_START,
ObservabilityStatistic::P50,
ttff_med.get_first_present_ms(),
)
.with_samples(ttff_samples)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_LAYOUT_INVALIDATIONS,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
layout_invalidations as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_LAYOUT_PASSES,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
layout_passes as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_LAYOUT_CHANGED_PASSES,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
layout_changed_passes_clamped as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_LAYOUT_CHANGED_NODES,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
layout_changed_nodes as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_DIRTY_AREA_RATIO,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Ratio,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Mean,
dirty_ratio,
)
.with_samples(primary_iters)
.with_threshold(ObservabilityThreshold::new(
ObservabilityThresholdRule::Max,
Some(OBS_THRESHOLD_DIRTY_RATIO_MAX),
OBS_UNIT_RATIO.to_string(),
OBSERVABILITY_BUDGET_VERSION_V1.to_string(),
))
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_PARTIAL_REDRAW_COUNT,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
partial_redraws as f64,
)
.with_samples(primary_iters)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_FULL_REDRAW_COUNT,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
full_redraws as f64,
)
.with_samples(primary_iters)
.with_threshold(ObservabilityThreshold::new(
ObservabilityThresholdRule::Max,
Some(OBS_THRESHOLD_FULL_REDRAW_MAX),
OBS_UNIT_COUNT.to_string(),
OBSERVABILITY_BUDGET_VERSION_V1.to_string(),
))
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_DRAW_COMMANDS,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
draw_commands_total as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_common(
OBS_METRIC_PIXELS_SHADED,
OBS_SCOPE_LAYOUT,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
pixels_shaded_total as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
ObservabilityMetric::measured_leak(
OBS_METRIC_DESTROYED_OWNERS,
OBS_SCOPE_LIFECYCLE,
ObservabilityUnit::Count,
OBS_WINDOW_POST_DRAIN,
0.0,
destroyed_live as f64,
0.0,
)
.with_samples(1)
.with_threshold(ObservabilityThreshold::new(
ObservabilityThresholdRule::MaxDestroyedOwnerCount,
Some(OBS_THRESHOLD_DESTROYED_OWNERS_MAX),
OBS_UNIT_COUNT.to_string(),
OBSERVABILITY_BUDGET_VERSION_V1.to_string(),
))
.with_evidence_refs(ev_refs.clone()),
];
let native_heap_coverage: ObservabilityCoverage = if alloc_tracked {
let heap_probe: NativeHeapLifecycleProbe =
probe_native_heap_lifecycle(OBS_HEAP_PROBE_CYCLES);
let peak_bytes_all: u64 = peak_live_bytes.max(heap_probe.get_peak_live_bytes());
let mean_allocs_per_frame: f64 = (steady_allocs as f64) / steady_denom;
let mean_bytes_per_frame: f64 = (steady_bytes as f64) / steady_denom;
metrics.push(
ObservabilityMetric::measured_leak(
OBS_METRIC_HEAP_LIVE_BYTES,
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Bytes,
OBS_WINDOW_POST_DRAIN,
heap_probe.get_baseline_live_bytes() as f64,
heap_probe.get_after_drain_live_bytes() as f64,
heap_probe.get_live_bytes_slope_per_cycle(),
)
.with_samples(heap_probe.get_cycles())
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_leak(
OBS_METRIC_HEAP_LIVE_ALLOCS,
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Count,
OBS_WINDOW_POST_DRAIN,
heap_probe.get_baseline_live_allocations() as f64,
heap_probe.get_after_drain_live_allocations() as f64,
heap_probe.get_live_allocations_slope_per_cycle(),
)
.with_samples(heap_probe.get_cycles())
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
OBS_METRIC_HEAP_ALLOC_COUNT,
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
steady_allocs as f64,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
OBS_METRIC_HEAP_FREE_COUNT,
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
steady_frees as f64,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
OBS_METRIC_MEMORY_PEAK_BYTES,
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Bytes,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Max,
peak_bytes_all as f64,
)
.with_samples(total_cycles)
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_HEAP_BYTES_ALLOCATED),
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Bytes,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Total,
steady_bytes as f64,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_STEADY_ALLOCS_PER_FRAME),
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Count,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Mean,
mean_allocs_per_frame,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs.clone()),
);
metrics.push(
ObservabilityMetric::measured_common(
&platform_ext_id(platform_key, OBS_EXT_STEADY_BYTES_PER_FRAME),
OBS_SCOPE_NATIVE_HEAP,
ObservabilityUnit::Bytes,
OBS_WINDOW_STRESS_ITERS,
ObservabilityStatistic::Mean,
mean_bytes_per_frame,
)
.with_samples(steady_frames)
.with_evidence_refs(ev_refs),
);
ObservabilityCoverage::new(
OBS_SCOPE_NATIVE_HEAP.to_string(),
true,
ObservabilityCoverageStatus::Measured,
None,
)
} else {
ObservabilityCoverage::new(
OBS_SCOPE_NATIVE_HEAP.to_string(),
false,
ObservabilityCoverageStatus::Missing,
Some(OBS_REASON_ALLOC_TRACKER_DISABLED.to_string()),
)
};
let tool_scope: &'static str = if alloc_tracked {
OBS_TOOL_SCOPE_WITH_HEAP
} else {
OBS_TOOL_SCOPE
};
let status: ObservabilityStatus = if failed {
ObservabilityStatus::Fail
} else {
ObservabilityStatus::Pass
};
ObservabilityReportV1 {
schema_version: OBSERVABILITY_SCHEMA_VERSION_V1.to_string(),
target: ObservabilityTarget::new(
platform,
os_name.to_string(),
OBS_DEVICE_HEADLESS_HOST.to_string(),
OBS_BACKEND_SOFTWARE.to_string(),
OBS_MODE_HEADLESS_STRESS.to_string(),
Some(ObservabilityExecutionKind::Headless),
),
build: ObservabilityBuild::new(
resolved_commit,
resolved_profile,
Some(OBS_TOOL_VERSION.to_string()),
),
tools: vec![ObservabilityTool::new(
OBS_TOOL_NAME.to_string(),
OBS_TOOL_VERSION.to_string(),
tool_scope.to_string(),
)],
workload: ObservabilityWorkload::new(
OBS_FIXTURE_STRESS_MATRIX.to_string(),
total_cycles,
OBS_CHECKPOINT_POST_DRAIN.to_string(),
Some(0),
Some(0),
),
status,
coverage: vec![
ObservabilityCoverage::new(
OBS_SCOPE_FRAME.to_string(),
true,
ObservabilityCoverageStatus::Measured,
None,
),
ObservabilityCoverage::new(
OBS_SCOPE_LAYOUT.to_string(),
true,
ObservabilityCoverageStatus::Measured,
None,
),
ObservabilityCoverage::new(
OBS_SCOPE_LIFECYCLE.to_string(),
true,
ObservabilityCoverageStatus::Measured,
None,
),
native_heap_coverage,
ObservabilityCoverage::new(
OBS_SCOPE_GPU.to_string(),
false,
ObservabilityCoverageStatus::NotApplicable,
Some(OBS_REASON_NO_GPU_HEADLESS.to_string()),
),
],
metrics,
findings,
evidence: vec![ObservabilityEvidence::new(
OBS_EVIDENCE_ID_STRESS.to_string(),
OBS_EVIDENCE_ARTIFACT_STRESS.to_string(),
None,
true,
Some(OBS_EVIDENCE_DESC_STRESS.to_string()),
)],
}
}
#[must_use]
pub fn observability_report_to_json(report: &ObservabilityReportV1) -> String {
to_string_pretty(report).unwrap_or_default()
}
pub fn validate_observability_report(report: &ObservabilityReportV1) -> Result<(), Vec<String>> {
let mut errors: Vec<String> = Vec::new();
if report.get_schema_version().as_str() != OBSERVABILITY_SCHEMA_VERSION_V1 {
errors.push(OBS_ERR_SCHEMA_VERSION.to_string());
}
if report.get_tools().is_empty() {
errors.push(OBS_ERR_EMPTY_TOOLS.to_string());
}
if report.get_coverage().is_empty() {
errors.push(OBS_ERR_EMPTY_COVERAGE.to_string());
}
let evidence_ids: HashSet<&str> = report
.get_evidence()
.iter()
.map(|ev: &ObservabilityEvidence| ev.get_id().as_str())
.collect();
let mut coverage_status: HashMap<&str, ObservabilityCoverageStatus> = HashMap::new();
let mut missing_required: bool = false;
for cov in report.get_coverage().iter() {
let _: Option<ObservabilityCoverageStatus> =
coverage_status.insert(cov.get_scope().as_str(), cov.get_status());
if cov.get_required() && cov.get_status() != ObservabilityCoverageStatus::Measured {
missing_required = true;
}
}
let mut measured_ids: HashSet<&str> = HashSet::new();
let mut layout_passes: Option<f64> = None;
let mut layout_changed: Option<f64> = None;
for metric in report.get_metrics().iter() {
let scope_state: Option<ObservabilityCoverageStatus> =
coverage_status.get(metric.get_scope().as_str()).copied();
let scope_unmeasured: bool = matches!(
scope_state,
Some(
ObservabilityCoverageStatus::Missing
| ObservabilityCoverageStatus::Unsupported
| ObservabilityCoverageStatus::NotApplicable
)
);
if (metric.get_status() != ObservabilityMetricStatus::Measured || scope_unmeasured)
&& metric.has_any_numeric_value()
{
errors.push(format!("{}: {}", OBS_ERR_FAKE_ZERO, metric.get_id()));
}
for ev_ref in metric.get_evidence_refs().iter() {
if !evidence_ids.contains(ev_ref.as_str()) {
errors.push(format!("{}: {ev_ref}", OBS_ERR_BROKEN_EVIDENCE));
}
}
if metric.get_status() == ObservabilityMetricStatus::Measured && !scope_unmeasured {
let mid: &str = metric.get_id().as_str();
let _: bool = measured_ids.insert(mid);
let is_leak_metric: bool = mid == OBS_METRIC_DESTROYED_OWNERS
|| mid == OBS_METRIC_HEAP_LIVE_BYTES
|| mid == OBS_METRIC_HEAP_LIVE_ALLOCS;
if is_leak_metric {
if metric.baseline_value().is_none() || metric.after_drain_value().is_none() {
errors.push(format!("{}: {mid}", OBS_ERR_LEAK_MISSING_BOUNDS));
}
} else if metric.scalar_value().is_none() {
errors.push(format!("{}: {mid}", OBS_ERR_COMMON_MISSING_VALUE));
}
if mid == OBS_METRIC_LAYOUT_PASSES {
layout_passes = metric.scalar_value();
} else if mid == OBS_METRIC_LAYOUT_CHANGED_PASSES {
layout_changed = metric.scalar_value();
}
}
}
if let (Some(passes), Some(changed)) = (layout_passes, layout_changed)
&& changed > passes
{
errors.push(OBS_ERR_LAYOUT_INVARIANT.to_string());
}
let mut has_blocking_finding: bool = false;
for finding in report.get_findings().iter() {
for ev_ref in finding.get_evidence_refs().iter() {
if !evidence_ids.contains(ev_ref.as_str()) {
errors.push(format!("{}: {ev_ref}", OBS_ERR_BROKEN_EVIDENCE));
}
}
if matches!(
finding.get_classification(),
ObservabilityFindingClass::UnexpectedLeak
| ObservabilityFindingClass::BudgetViolation
| ObservabilityFindingClass::UnattributedGrowth
) {
has_blocking_finding = true;
}
}
if report.get_status() == ObservabilityStatus::Pass {
if missing_required {
errors.push(OBS_ERR_PASS_MISSING_COVERAGE.to_string());
}
if has_blocking_finding {
errors.push(OBS_ERR_PASS_BLOCKING_FINDING.to_string());
}
if coverage_status.get(OBS_SCOPE_FRAME) == Some(&ObservabilityCoverageStatus::Measured)
&& !measured_ids.contains(OBS_METRIC_FRAME_INTERVAL)
{
errors.push(format!(
"{}: {OBS_METRIC_FRAME_INTERVAL}",
OBS_ERR_PASS_MISSING_REQUIRED_METRIC
));
}
if coverage_status.get(OBS_SCOPE_LAYOUT) == Some(&ObservabilityCoverageStatus::Measured) {
for req in [
OBS_METRIC_LAYOUT_INVALIDATIONS,
OBS_METRIC_LAYOUT_PASSES,
OBS_METRIC_LAYOUT_CHANGED_PASSES,
OBS_METRIC_LAYOUT_CHANGED_NODES,
] {
if !measured_ids.contains(req) {
errors.push(format!("{}: {req}", OBS_ERR_PASS_MISSING_REQUIRED_METRIC));
}
}
}
if coverage_status.get(OBS_SCOPE_LIFECYCLE) == Some(&ObservabilityCoverageStatus::Measured)
&& !measured_ids.contains(OBS_METRIC_DESTROYED_OWNERS)
{
errors.push(format!(
"{}: {OBS_METRIC_DESTROYED_OWNERS}",
OBS_ERR_PASS_MISSING_REQUIRED_METRIC
));
}
if coverage_status.get(OBS_SCOPE_NATIVE_HEAP)
== Some(&ObservabilityCoverageStatus::Measured)
&& !measured_ids.contains(OBS_METRIC_HEAP_LIVE_BYTES)
{
errors.push(format!(
"{}: {OBS_METRIC_HEAP_LIVE_BYTES}",
OBS_ERR_PASS_MISSING_REQUIRED_METRIC
));
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}