Skip to content

Commit 82d05db

Browse files
syepesnitisht
andauthored
Add extra Alertmanager labels (#316)
This adds label for Parseable instance address in alertmanager. Part of #311 Co-authored-by: Nitish Tiwari <[email protected]>
1 parent c123ed8 commit 82d05db

File tree

2 files changed

+86
-33
lines changed

2 files changed

+86
-33
lines changed

server/src/alerts/mod.rs

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod target;
2626
use crate::metrics::ALERTS_STATES;
2727
use crate::storage;
2828
use crate::utils::uid;
29+
use crate::CONFIG;
2930

3031
pub use self::rule::Rule;
3132
use self::target::Target;
@@ -66,8 +67,8 @@ impl Alert {
6667
ALERTS_STATES
6768
.with_label_values(&[
6869
context.stream.as_str(),
69-
context.alert_name.as_str(),
70-
context.alert_state.to_string().as_str(),
70+
context.alert_info.alert_name.as_str(),
71+
context.alert_info.alert_state.to_string().as_str(),
7172
])
7273
.inc();
7374
for target in &self.targets {
@@ -78,7 +79,13 @@ impl Alert {
7879
}
7980

8081
fn get_context(&self, stream_name: String, alert_state: AlertState, rule: &Rule) -> Context {
82+
let deployment_instance = format!(
83+
"{}://{}",
84+
CONFIG.parseable.get_scheme(),
85+
CONFIG.parseable.address
86+
);
8187
let deployment_id = storage::StorageMetadata::global().deployment_id;
88+
let deployment_mode = storage::StorageMetadata::global().mode.to_string();
8289
let additional_labels =
8390
serde_json::to_value(rule).expect("rule is perfectly deserializable");
8491
let mut flatten_additional_labels = serde_json::json!({});
@@ -93,11 +100,13 @@ impl Alert {
93100

94101
Context::new(
95102
stream_name,
96-
self.name.clone(),
97-
self.message.clone(),
98-
self.rule.trigger_reason(),
99-
alert_state,
100-
deployment_id,
103+
AlertInfo::new(
104+
self.name.clone(),
105+
self.message.clone(),
106+
rule.trigger_reason(),
107+
alert_state,
108+
),
109+
DeploymentInfo::new(deployment_instance, deployment_id, deployment_mode),
101110
flatten_additional_labels,
102111
)
103112
}
@@ -110,44 +119,86 @@ pub trait CallableTarget {
110119
#[derive(Debug, Clone)]
111120
pub struct Context {
112121
stream: String,
113-
alert_name: String,
114-
message: String,
115-
reason: String,
116-
alert_state: AlertState,
117-
deployment_id: uid::Uid,
122+
alert_info: AlertInfo,
123+
deployment_info: DeploymentInfo,
118124
additional_labels: serde_json::Value,
119125
}
120126

121127
impl Context {
122128
pub fn new(
123129
stream: String,
124-
alert_name: String,
125-
message: String,
126-
reason: String,
127-
alert_state: AlertState,
128-
deployment_id: uid::Uid,
130+
alert_info: AlertInfo,
131+
deployment_info: DeploymentInfo,
129132
additional_labels: serde_json::Value,
130133
) -> Self {
131134
Self {
132135
stream,
133-
alert_name,
134-
message,
135-
reason,
136-
alert_state,
137-
deployment_id,
136+
alert_info,
137+
deployment_info,
138138
additional_labels,
139139
}
140140
}
141141

142142
fn default_alert_string(&self) -> String {
143143
format!(
144144
"{} triggered on {}\nMessage: {}\nFailing Condition: {}",
145-
self.alert_name, self.stream, self.message, self.reason
145+
self.alert_info.alert_name,
146+
self.stream,
147+
self.alert_info.message,
148+
self.alert_info.reason
146149
)
147150
}
148151

149152
fn default_resolved_string(&self) -> String {
150-
format!("{} on {} is now resolved ", self.alert_name, self.stream)
153+
format!(
154+
"{} on {} is now resolved ",
155+
self.alert_info.alert_name, self.stream
156+
)
157+
}
158+
}
159+
160+
#[derive(Debug, Clone)]
161+
pub struct AlertInfo {
162+
alert_name: String,
163+
message: String,
164+
reason: String,
165+
alert_state: AlertState,
166+
}
167+
168+
impl AlertInfo {
169+
pub fn new(
170+
alert_name: String,
171+
message: String,
172+
reason: String,
173+
alert_state: AlertState,
174+
) -> Self {
175+
Self {
176+
alert_name,
177+
message,
178+
reason,
179+
alert_state,
180+
}
181+
}
182+
}
183+
184+
#[derive(Debug, Clone)]
185+
pub struct DeploymentInfo {
186+
deployment_instance: String,
187+
deployment_id: uid::Uid,
188+
deployment_mode: String,
189+
}
190+
191+
impl DeploymentInfo {
192+
pub fn new(
193+
deployment_instance: String,
194+
deployment_id: uid::Uid,
195+
deployment_mode: String,
196+
) -> Self {
197+
Self {
198+
deployment_instance,
199+
deployment_id,
200+
deployment_mode,
201+
}
151202
}
152203
}
153204

server/src/alerts/target.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct Target {
5555
impl Target {
5656
pub fn call(&self, context: Context) {
5757
let timeout = &self.timeout;
58-
let resolves = context.alert_state;
58+
let resolves = context.alert_info.alert_state;
5959
let mut state = timeout.state.lock().unwrap();
6060

6161
match resolves {
@@ -129,7 +129,7 @@ impl Target {
129129
// Send and alert stating that this alert will only work once it has seen a RESOLVE
130130
state.lock().unwrap().timed_out = false;
131131
let mut context = alert_context;
132-
context.message = format!(
132+
context.alert_info.message = format!(
133133
"Triggering alert did not resolve itself after {times} retries, This alert is paused until it resolves");
134134
// Send and exit this task.
135135
call_target(target, context);
@@ -229,7 +229,7 @@ impl CallableTarget for SlackWebHook {
229229
.build()
230230
.expect("Client can be constructed on this system");
231231

232-
let alert = match payload.alert_state {
232+
let alert = match payload.alert_info.alert_state {
233233
AlertState::SetToFiring => {
234234
serde_json::json!({ "text": payload.default_alert_string() })
235235
}
@@ -267,7 +267,7 @@ impl CallableTarget for OtherWebHook {
267267
.build()
268268
.expect("Client can be constructed on this system");
269269

270-
let alert = match payload.alert_state {
270+
let alert = match payload.alert_info.alert_state {
271271
AlertState::SetToFiring => payload.default_alert_string(),
272272
AlertState::Resolved => payload.default_resolved_string(),
273273
_ => unreachable!(),
@@ -317,13 +317,15 @@ impl CallableTarget for AlertManager {
317317

318318
let mut alerts = serde_json::json!([{
319319
"labels": {
320-
"alertname": payload.alert_name,
320+
"alertname": payload.alert_info.alert_name,
321321
"stream": payload.stream,
322-
"deployment_id": payload.deployment_id
322+
"deployment_instance": payload.deployment_info.deployment_instance,
323+
"deployment_id": payload.deployment_info.deployment_id,
324+
"deployment_mode": payload.deployment_info.deployment_mode
323325
},
324326
"annotations": {
325-
"message": payload.message,
326-
"reason": payload.reason
327+
"message": payload.alert_info.message,
328+
"reason": payload.alert_info.reason
327329
}
328330
}]);
329331

@@ -341,7 +343,7 @@ impl CallableTarget for AlertManager {
341343
);
342344

343345
// fill in status label accordingly
344-
match payload.alert_state {
346+
match payload.alert_info.alert_state {
345347
AlertState::SetToFiring => alert["labels"]["status"] = "firing".into(),
346348
AlertState::Resolved => {
347349
alert["labels"]["status"] = "resolved".into();

0 commit comments

Comments
 (0)