Skip to content

Commit b71b7f0

Browse files
committed
bugfix and alerts retry
1 parent ece7953 commit b71b7f0

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/alerts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl AlertConfig {
659659

660660
// validate condition config
661661
let agg1 = &self.aggregates.aggregate_config[0];
662-
let agg2 = &self.aggregates.aggregate_config[0];
662+
let agg2 = &self.aggregates.aggregate_config[1];
663663

664664
validate_condition_config(&agg1.conditions)?;
665665
validate_condition_config(&agg2.conditions)?;

src/alerts/target.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use http::{header::AUTHORIZATION, HeaderMap, HeaderValue};
2929
use humantime_serde::re::humantime;
3030
use reqwest::ClientBuilder;
3131
use tracing::{error, trace, warn};
32+
use url::Url;
3233

3334
use super::ALERTS;
3435

@@ -255,9 +256,9 @@ fn default_client_builder() -> ClientBuilder {
255256
ClientBuilder::new()
256257
}
257258

258-
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
259+
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
259260
pub struct SlackWebHook {
260-
endpoint: String,
261+
endpoint: Url,
261262
}
262263

263264
#[async_trait]
@@ -279,7 +280,7 @@ impl CallableTarget for SlackWebHook {
279280
}
280281
};
281282

282-
if let Err(e) = client.post(&self.endpoint).json(&alert).send().await {
283+
if let Err(e) = client.post(self.endpoint.clone()).json(&alert).send().await {
283284
error!("Couldn't make call to webhook, error: {}", e)
284285
}
285286
}
@@ -288,7 +289,7 @@ impl CallableTarget for SlackWebHook {
288289
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
289290
#[serde(rename_all = "camelCase")]
290291
pub struct OtherWebHook {
291-
endpoint: String,
292+
endpoint: Url,
292293
#[serde(default)]
293294
headers: HashMap<String, String>,
294295
#[serde(default)]
@@ -314,7 +315,7 @@ impl CallableTarget for OtherWebHook {
314315
};
315316

316317
let request = client
317-
.post(&self.endpoint)
318+
.post(self.endpoint.clone())
318319
.headers((&self.headers).try_into().expect("valid_headers"));
319320

320321
if let Err(e) = request.body(alert).send().await {
@@ -326,7 +327,7 @@ impl CallableTarget for OtherWebHook {
326327
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
327328
#[serde(rename_all = "camelCase")]
328329
pub struct AlertManager {
329-
endpoint: String,
330+
endpoint: Url,
330331
#[serde(default)]
331332
skip_tls_check: bool,
332333
#[serde(flatten)]
@@ -404,7 +405,7 @@ impl CallableTarget for AlertManager {
404405
}
405406
};
406407

407-
if let Err(e) = client.post(&self.endpoint).json(&alerts).send().await {
408+
if let Err(e) = client.post(self.endpoint.clone()).json(&alerts).send().await {
408409
error!("Couldn't make call to alertmanager, error: {}", e)
409410
}
410411
}

src/sync.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,25 @@ pub async fn alert_runtime(mut rx: mpsc::Receiver<AlertTask>) -> Result<(), anyh
246246
let alert = alert.clone();
247247
let id = alert.id;
248248
let handle = tokio::spawn(async move {
249+
let mut retry_counter = 0;
250+
let mut sleep_duration = alert.get_eval_frequency();
249251
loop {
250252
match alerts_utils::evaluate_alert(&alert).await {
251-
Ok(_) => {}
253+
Ok(_) => {
254+
retry_counter = 0;
255+
}
252256
Err(err) => {
253-
error!("Error while evaluation- {err}");
254-
break;
257+
warn!("Error while evaluation- {}\nRetrying after sleeping for 1 minute", err);
258+
sleep_duration = 1;
259+
retry_counter += 1;
260+
261+
if retry_counter > 3 {
262+
error!("Alert with id {} failed to evaluate after 3 retries with err- {}", id, err);
263+
break;
264+
}
255265
}
256266
}
257-
tokio::time::sleep(Duration::from_secs(alert.get_eval_frequency() * 60))
258-
.await;
267+
tokio::time::sleep(Duration::from_secs(sleep_duration * 60)).await;
259268
}
260269
});
261270

0 commit comments

Comments
 (0)