Expand description
The pattern performance monitor: per-pattern execution stats, the anomaly-detection trio, and the sanitized callbacks.
This is the Rust family’s port of the reference detection engine’s
monitor (guard_core/detection_engine/monitor.py,
monitor_anomalies.py, monitor_types.py), sync over a mutex where
the reference uses an asyncio lock:
- Constants (
DEFAULT_RECENT_TIMES_WINDOW, the knob clamps): the reference constructor clamps every knob into its range and caps the recent-times window belowDEFAULT_RECENT_TIMES_WINDOWat the sample floor. - The anomaly trio (
detect_timeout_anomaly,detect_slow_execution_anomaly,detect_statistical_anomaly): a timeout wins over a slow flag, the statistical arm needsmin_samplesrecent times and fires when the z-score over the sample stddev exceeds the threshold. - Sanitized callbacks (
sanitize_anomaly_data): callbacks receive the anomaly with the pattern redacted (truncated at 50 chars plus an 8-char hash) - a raising callback never breaks the recorder, the reference swallows the error (and reports it to the agent). - The emission cooldown ([
PerformanceMonitor::reserve_anomaly_ emission]): per pattern, one anomaly emission peranomaly_emission_cooldownseconds.
The mutex guards here are the reference’s asyncio.Lock sections; the
significant_drop_tightening lint is allowed module-wide because every
guard already scopes exactly its critical section.
§Example
use guard_core_engine::performance_monitor::PerformanceMonitor;
let monitor = PerformanceMonitor::new(3.0, 0.1, 1000, 1000, 60.0, 30);
monitor.record_metric("slow_pattern", 0.5, 1024, false, false, 0.0, None);
let report = monitor.pattern_report("slow_pattern").expect("tracked");
assert_eq!(report.total_executions, 1);
// The timeout anomaly wins over the slow-execution flag.
let timeout = monitor.record_metric("slow_pattern", 0.5, 1024, true, true, 1.0, None);
assert_eq!(timeout.len(), 1);
assert_eq!(timeout[0].kind(), "timeout");Structs§
- Pattern
Report - One aggregated pattern report (
build_pattern_report). - Pattern
Stats - The per-pattern counters (
PatternStats). - Performance
Monitor - The monitor (the reference
PerformanceMonitor): the clamped knobs on the struct, the stats behind a mutex (the reference asyncio lock).
Enums§
- Pattern
Anomaly - The anomaly kinds, with the payload the reference attaches.
Constants§
- DEFAULT_
RECENT_ TIMES_ WINDOW - The recent-times window (
DEFAULT_RECENT_TIMES_WINDOW): at most this many execution times feed each pattern’s average and stddev. - MAX_
PATTERN_ LENGTH - The pattern truncation point (
MAX_PATTERN_LENGTHin the reference recorder): longer patterns are cut and suffixed.
Functions§
- detect_
slow_ execution_ anomaly - The slow-execution arm (
detect_slow_execution_anomaly): non-timeout executions overslow_pattern_thresholdseconds. - detect_
statistical_ anomaly - The statistical arm (
detect_statistical_anomaly): with at leastmin_samplesrecent times, a non-timeout execution whose z-score over the window’s sample stddev exceedsanomaly_threshold. - detect_
timeout_ anomaly - The timeout arm (
detect_timeout_anomaly). - sanitize_
anomaly_ data - The callback payload sanitizer (
sanitize_anomaly_data). - truncate_
pattern - Truncate a pattern for tracking (
MAX_PATTERN_LENGTH+ the"...[truncated]"suffix).