rusty_feeder/provider/ext/
alerts.rs

1//! Alerting and anomaly detection for the HFT provider system
2//!
3//! This module contains configuration and types for real-time alerting,
4//! anomaly detection algorithms, and monitoring condition management.
5
6use rusty_model::instruments::InstrumentId;
7
8use super::health::IssueSeverity;
9
10/// Configuration for creating monitoring alerts
11///
12/// Defines the conditions, thresholds, and behavior for alerts that
13/// trigger when specific monitoring conditions are met.
14#[derive(Debug, Clone)]
15pub struct AlertConfig {
16    /// Human-readable name for this alert
17    pub name: String,
18    /// Condition that triggers this alert
19    pub condition: AlertCondition,
20    /// Threshold value for the condition
21    pub threshold: f64,
22    /// Duration in milliseconds the condition must persist
23    pub duration_ms: u64,
24    /// Severity level of this alert
25    pub severity: IssueSeverity,
26    /// Whether this alert is currently enabled
27    pub enabled: bool,
28}
29
30/// Types of conditions that can trigger alerts
31///
32/// Defines various monitoring conditions that can be configured
33/// to generate alerts when thresholds are exceeded.
34#[derive(Debug, Clone)]
35pub enum AlertCondition {
36    /// Alert when latency exceeds threshold for an instrument
37    LatencyThreshold {
38        /// Optional instrument ID, None means system-wide
39        instrument_id: Option<InstrumentId>,
40    },
41    /// Alert when error rate exceeds threshold for a component
42    ErrorRateThreshold {
43        /// Name of the component to monitor
44        component: String,
45    },
46    /// Alert when message drop rate exceeds threshold
47    DropRateThreshold {
48        /// Optional instrument ID, None means system-wide
49        instrument_id: Option<InstrumentId>,
50    },
51    /// Alert when overall health score drops below threshold
52    HealthScoreThreshold,
53    /// Alert based on a custom metric value
54    CustomMetric {
55        /// Name of the custom metric to monitor
56        metric_name: String,
57    },
58    /// Alert when connection to an exchange is lost
59    ConnectionLoss {
60        /// Name of the exchange
61        exchange: String,
62    },
63    /// Alert when anomaly is detected for an instrument
64    AnomalyDetected {
65        /// The instrument where anomaly was detected
66        instrument_id: InstrumentId,
67    },
68}
69
70/// Handle for managing active alerts
71///
72/// Opaque handle returned when enabling an alert, used to
73/// disable or query the alert status later.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
75pub struct AlertHandle(pub u64);
76
77/// Configuration for anomaly detection system
78///
79/// Specifies which instruments to monitor, sensitivity levels,
80/// and algorithms to use for detecting unusual patterns.
81#[derive(Debug, Clone)]
82pub struct AnomalyConfig {
83    /// List of instruments to monitor for anomalies
84    pub instruments: Vec<InstrumentId>,
85    /// Detection sensitivity from 0.0 (low) to 1.0 (high)
86    pub sensitivity: f64,
87    /// Time window for analysis in milliseconds
88    pub window_size_ms: u64,
89    /// List of detection algorithms to apply
90    pub algorithms: Vec<AnomalyAlgorithm>,
91}
92
93/// Anomaly detection algorithm types
94///
95/// Different algorithms for detecting various types of anomalous
96/// behavior in trading data and system performance.
97#[derive(Debug, Clone)]
98pub enum AnomalyAlgorithm {
99    /// Detect statistical outliers using standard deviation
100    StatisticalOutlier {
101        /// Number of standard deviations for outlier threshold
102        threshold_std_dev: f64,
103    },
104    /// Detect deviations from learned patterns
105    PatternDeviation {
106        /// Length of pattern sequence to analyze
107        pattern_length: usize,
108    },
109    /// Detect abnormal trading volume
110    VolumeAnomaly {
111        /// Multiple of average volume to trigger alert
112        volume_threshold: f64,
113    },
114    /// Detect unusual latency spikes
115    LatencyAnomaly {
116        /// Latency threshold in nanoseconds
117        latency_threshold_ns: u64,
118    },
119    /// Detect abnormal price movements
120    PriceAnomaly {
121        /// Price change threshold as a fraction
122        price_change_threshold: f64,
123    },
124}