Skip to main content

Module performance_monitor

Module performance_monitor 

Source
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 below DEFAULT_RECENT_TIMES_WINDOW at 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 needs min_samples recent 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 per anomaly_emission_cooldown seconds.

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§

PatternReport
One aggregated pattern report (build_pattern_report).
PatternStats
The per-pattern counters (PatternStats).
PerformanceMonitor
The monitor (the reference PerformanceMonitor): the clamped knobs on the struct, the stats behind a mutex (the reference asyncio lock).

Enums§

PatternAnomaly
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_LENGTH in 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 over slow_pattern_threshold seconds.
detect_statistical_anomaly
The statistical arm (detect_statistical_anomaly): with at least min_samples recent times, a non-timeout execution whose z-score over the window’s sample stddev exceeds anomaly_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).