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
use super::Result;
use inflector::cases::pascalcase::is_pascal_case;
use regex::Regex;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PrometheusAlert {
pub name: String,
pub summary: String,
pub description: String,
pub expr: String,
pub min_duration: String,
pub severity: PrometheusAlertSeverity,
}
#[serde(rename_all = "lowercase")]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum PrometheusAlertSeverity {
Warning,
Error,
}
impl PrometheusAlert {
pub fn verify(&self, svc: &str) -> Result<()> {
if !is_pascal_case(&self.name) {
bail!("Prometheus alert for {} needs a non-empty PascalCaseName", svc);
}
if self.summary.is_empty() {
bail!(
"Prometheus alert for {} needs a summary of the problem it identifies",
svc
);
}
if self.description.is_empty() {
bail!(
"Prometheus alert for {} needs a description of the problem it identifies",
svc
);
}
if !Regex::new(r"^\d+[mh]$").unwrap().is_match(&self.min_duration) {
bail!("Prometheus alert has invalid min_duration value (needs to be like '15m' or '1h')");
}
if let Err(e) = prometheus_parser::parse_expr(&self.expr) {
bail!("Prometheus alert expression for {} invalid: {:?}", svc, e);
}
Ok(())
}
}