Skip to main content

obs_core/
alert.rs

1use core_types::Timestamp;
2
3/// Severity level of an alert.
4#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub enum AlertSeverity {
6    /// Informational notice, no action required.
7    Info,
8    /// Threshold crossed; operator should be aware.
9    Warn,
10    /// Component degraded; timely investigation needed.
11    Error,
12    /// Critical failure; immediate action required.
13    Critical,
14}
15
16impl AlertSeverity {
17    pub fn as_str(&self) -> &'static str {
18        match self {
19            Self::Info => "info",
20            Self::Warn => "warn",
21            Self::Error => "error",
22            Self::Critical => "critical",
23        }
24    }
25}
26
27/// An alert fired by a component or rule.
28#[derive(Clone, Debug)]
29pub struct Alert {
30    /// The component or subsystem that raised the alert.
31    pub source: String,
32    /// Free-form alert code (e.g. `"CPU_OVER_THRESHOLD"`).
33    pub code: String,
34    /// Human-readable description of what happened.
35    pub message: String,
36    /// Alert severity.
37    pub severity: AlertSeverity,
38    /// When the alert was created (nanos since Unix epoch).
39    pub timestamp: Timestamp,
40}
41
42impl Alert {
43    pub fn new(
44        source: impl Into<String>,
45        code: impl Into<String>,
46        message: impl Into<String>,
47        severity: AlertSeverity,
48        timestamp: Timestamp,
49    ) -> Self {
50        Self {
51            source: source.into(),
52            code: code.into(),
53            message: message.into(),
54            severity,
55            timestamp,
56        }
57    }
58}