Compare commits
No commits in common. "e8d55d27e4b90f53904910ce12ad66c9799c40f3" and "fea7e9ddb9aa1a0b336ff6961adfa019227c6366" have entirely different histories.
e8d55d27e4
...
fea7e9ddb9
@ -1,2 +1 @@
|
|||||||
pub mod discord_alert_channel;
|
pub mod discord_alert_channel;
|
||||||
pub mod webhook_receiver;
|
|
||||||
|
|||||||
@ -1,124 +0,0 @@
|
|||||||
use async_trait::async_trait;
|
|
||||||
use serde::Serialize;
|
|
||||||
use serde_yaml::{Mapping, Value};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
interpret::{InterpretError, Outcome},
|
|
||||||
modules::monitoring::kube_prometheus::{
|
|
||||||
prometheus::{Prometheus, PrometheusReceiver},
|
|
||||||
types::{AlertChannelConfig, AlertManagerChannelConfig},
|
|
||||||
},
|
|
||||||
topology::{Url, oberservability::monitoring::AlertReceiver},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
|
||||||
pub struct WebhookReceiver {
|
|
||||||
pub name: String,
|
|
||||||
pub url: Url,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl AlertReceiver<Prometheus> for WebhookReceiver {
|
|
||||||
async fn install(&self, sender: &Prometheus) -> Result<Outcome, InterpretError> {
|
|
||||||
sender.install_receiver(self).await
|
|
||||||
}
|
|
||||||
fn clone_box(&self) -> Box<dyn AlertReceiver<Prometheus>> {
|
|
||||||
Box::new(self.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl PrometheusReceiver for WebhookReceiver {
|
|
||||||
fn name(&self) -> String {
|
|
||||||
self.name.clone()
|
|
||||||
}
|
|
||||||
async fn configure_receiver(&self) -> AlertManagerChannelConfig {
|
|
||||||
self.get_config().await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl AlertChannelConfig for WebhookReceiver {
|
|
||||||
async fn get_config(&self) -> AlertManagerChannelConfig {
|
|
||||||
let channel_global_config = None;
|
|
||||||
let channel_receiver = self.alert_channel_receiver().await;
|
|
||||||
let channel_route = self.alert_channel_route().await;
|
|
||||||
|
|
||||||
AlertManagerChannelConfig {
|
|
||||||
channel_global_config,
|
|
||||||
channel_receiver,
|
|
||||||
channel_route,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WebhookReceiver {
|
|
||||||
async fn alert_channel_route(&self) -> serde_yaml::Value {
|
|
||||||
let mut route = Mapping::new();
|
|
||||||
route.insert(
|
|
||||||
Value::String("receiver".to_string()),
|
|
||||||
Value::String(self.name.clone()),
|
|
||||||
);
|
|
||||||
route.insert(
|
|
||||||
Value::String("matchers".to_string()),
|
|
||||||
Value::Sequence(vec![Value::String("alertname!=Watchdog".to_string())]),
|
|
||||||
);
|
|
||||||
route.insert(Value::String("continue".to_string()), Value::Bool(true));
|
|
||||||
Value::Mapping(route)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn alert_channel_receiver(&self) -> serde_yaml::Value {
|
|
||||||
let mut receiver = Mapping::new();
|
|
||||||
receiver.insert(
|
|
||||||
Value::String("name".to_string()),
|
|
||||||
Value::String(self.name.clone()),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut webhook_config = Mapping::new();
|
|
||||||
webhook_config.insert(
|
|
||||||
Value::String("url".to_string()),
|
|
||||||
Value::String(self.url.to_string()),
|
|
||||||
);
|
|
||||||
|
|
||||||
receiver.insert(
|
|
||||||
Value::String("webhook_configs".to_string()),
|
|
||||||
Value::Sequence(vec![Value::Mapping(webhook_config)]),
|
|
||||||
);
|
|
||||||
|
|
||||||
Value::Mapping(receiver)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[tokio::test]
|
|
||||||
async fn webhook_serialize_should_match() {
|
|
||||||
let webhook_receiver = WebhookReceiver {
|
|
||||||
name: "test-webhook".to_string(),
|
|
||||||
url: Url::Url(url::Url::parse("https://webhook.i.dont.exist.com").unwrap()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let webhook_receiver_receiver =
|
|
||||||
serde_yaml::to_string(&webhook_receiver.alert_channel_receiver().await).unwrap();
|
|
||||||
println!("receiver \n{:#}", webhook_receiver_receiver);
|
|
||||||
let webhook_receiver_receiver_yaml = r#"name: test-webhook
|
|
||||||
webhook_configs:
|
|
||||||
- url: https://webhook.i.dont.exist.com/
|
|
||||||
"#
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let webhook_receiver_route =
|
|
||||||
serde_yaml::to_string(&webhook_receiver.alert_channel_route().await).unwrap();
|
|
||||||
println!("route \n{:#}", webhook_receiver_route);
|
|
||||||
let webhook_receiver_route_yaml = r#"receiver: test-webhook
|
|
||||||
matchers:
|
|
||||||
- alertname!=Watchdog
|
|
||||||
continue: true
|
|
||||||
"#
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
assert_eq!(webhook_receiver_receiver, webhook_receiver_receiver_yaml);
|
|
||||||
assert_eq!(webhook_receiver_route, webhook_receiver_route_yaml);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user