1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::policy::Expression;
use crate::ConceptId;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Metric {
pub id: ConceptId,
pub name: String,
pub namespace: String,
pub expression: Expression,
#[serde(default, with = "crate::timefmt::serde_duration_option")]
pub refresh_interval: Option<Duration>,
pub unit: Option<String>,
/// Breach condition: the metric is considered in violation when its
/// value is `>= threshold` (the alerting-industry convention this crate
/// follows — e.g. `error_rate.threshold = 5.0` breaches at 5% or above).
/// If a metric's semantics are "breach when below" (e.g. an SLA
/// on-time-delivery rate), invert the expression itself rather than the
/// threshold direction, so this convention stays uniform for every
/// projection target that renders it (M2).
pub threshold: Option<Decimal>,
pub severity: Option<Severity>,
pub target: Option<Decimal>,
#[serde(default, with = "crate::timefmt::serde_duration_option")]
pub window: Option<Duration>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Severity {
Info,
Warning,
Error,
Critical,
}
impl Metric {
pub fn new(name: String, namespace: String, expression: Expression) -> Self {
let id = ConceptId::from_concept(&namespace, &name);
Self {
id,
name,
namespace,
expression,
refresh_interval: None,
unit: None,
threshold: None,
severity: None,
target: None,
window: None,
}
}
pub fn id(&self) -> &ConceptId {
&self.id
}
pub fn with_refresh_interval(mut self, duration: Duration) -> Self {
self.refresh_interval = Some(duration);
self
}
pub fn with_unit(mut self, unit: String) -> Self {
self.unit = Some(unit);
self
}
pub fn with_threshold(mut self, threshold: Decimal) -> Self {
self.threshold = Some(threshold);
self
}
pub fn with_severity(mut self, severity: Severity) -> Self {
self.severity = Some(severity);
self
}
pub fn with_target(mut self, target: Decimal) -> Self {
self.target = Some(target);
self
}
pub fn with_window(mut self, window: Duration) -> Self {
self.window = Some(window);
self
}
/// M2/M3: catches two silent-default gaps that previously only surfaced
/// as confusing behavior downstream in projection targets:
/// - `severity` implies a metric is actionable, which requires a
/// `threshold` to compare against; nothing enforced that pairing.
/// - `refresh_interval`/`window` use signed `time::Duration` and
/// nothing rejected a negative one.
///
/// `unit` is deliberately *not* checked against the `UnitRegistry` here
/// (unlike M1's original recommendation): metric units in this codebase
/// are commonly free-form business labels ("orders", "widgets") rather
/// than physical/dimensional units, and real fixtures
/// (`fixtures/projection_cell/basic/model.sea`) rely on that. Forcing
/// registry membership would reject legitimate domain vocabulary, not
/// just typos. `unit_from_string`'s fallback (see U1 in units/mod.rs)
/// now keys unregistered units by their own symbol as a distinct
/// dimension, so free-form labels no longer collide with each other —
/// the only remaining unit-shaped defect is an empty/whitespace label,
/// which is never meaningful and is rejected below.
pub fn validate(&self) -> Result<(), String> {
if self.unit.as_deref().is_some_and(|u| u.trim().is_empty()) {
return Err(format!(
"Metric '{}': `unit` must not be empty or whitespace-only.",
self.name
));
}
if self.severity.is_some() && self.threshold.is_none() {
return Err(format!(
"Metric '{}': `severity` requires a `threshold` to compare against.",
self.name
));
}
if self.refresh_interval.is_some_and(|d| d < Duration::ZERO) {
return Err(format!(
"Metric '{}': refresh_interval must not be negative.",
self.name
));
}
if self.window.is_some_and(|d| d < Duration::ZERO) {
return Err(format!(
"Metric '{}': window must not be negative.",
self.name
));
}
Ok(())
}
}