forked from NationTech/harmony
wip: applied comments in pr, changed naming of AlertChannel to AlertReceiver and added rust doc to Monitor for clarity
This commit is contained in:
parent
56dc1e93c1
commit
0d56fbc09d
@ -8,26 +8,32 @@ use crate::interpret::InterpretError;
|
||||
|
||||
use crate::{interpret::Outcome, topology::Topology};
|
||||
|
||||
/// Represents an entity responsible for collecting and organizing observability data
|
||||
/// from various telemetry sources
|
||||
/// A `Monitor` abstracts the logic required to scrape, aggregate, and structure
|
||||
/// monitoring data, enabling consistent processing regardless of the underlying data source.
|
||||
#[async_trait]
|
||||
pub trait Monitor<T: Topology>: Debug + Send + Sync {
|
||||
async fn deploy_monitor(
|
||||
&self,
|
||||
topology: &T,
|
||||
config: Vec<Box<dyn AlertChannelConfig<T>>>,
|
||||
alert_receivers: Vec<Box<dyn AlertReceiver<T>>>,
|
||||
) -> Result<Outcome, InterpretError>;
|
||||
|
||||
async fn delete_monitor(
|
||||
&self,
|
||||
topolgy: &T,
|
||||
config: Vec<Box<dyn AlertChannelConfig<T>>>,
|
||||
alert_receivers: Vec<Box<dyn AlertReceiver<T>>>,
|
||||
) -> Result<Outcome, InterpretError>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait AlertChannelConfig<T>: Debug + DynClone + Send + Sync {
|
||||
fn channel_identifier(&self) -> String;
|
||||
|
||||
fn webhook_url(&self) -> Option<Url>;
|
||||
|
||||
fn send_resolved_notifications(&self) -> bool;
|
||||
pub trait AlertReceiver<T>: Debug + DynClone + Send + Sync {
|
||||
fn get_alert_receiver_config(&self) -> AlertReceiverConfig;
|
||||
}
|
||||
|
||||
pub struct AlertReceiverConfig {
|
||||
pub receiver_id: String,
|
||||
pub receiver_webhook_url: Option<Url>,
|
||||
pub send_resolved: bool,
|
||||
}
|
||||
|
||||
@ -1,30 +1,25 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use non_blank_string_rs::NonBlankString;
|
||||
use url::Url;
|
||||
|
||||
use crate::modules::helm::chart::HelmChartScore;
|
||||
|
||||
use super::{config::KubePrometheusConfig, monitoring_alerting::AlertChannel};
|
||||
|
||||
fn get_discord_alert_manager_score(config: &KubePrometheusConfig) -> Option<HelmChartScore> {
|
||||
let (url, name) = config.alert_channel.iter().find_map(|channel| {
|
||||
if let AlertChannel::Discord { webhook_url, name } = channel {
|
||||
Some((webhook_url, name))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})?;
|
||||
|
||||
pub fn discord_alert_manager_score(
|
||||
webhook_url: Url,
|
||||
namespace: String,
|
||||
name: String,
|
||||
) -> HelmChartScore {
|
||||
let values = format!(
|
||||
r#"
|
||||
environment:
|
||||
- name: "DISCORD_WEBHOOK"
|
||||
value: "{url}"
|
||||
value: "{webhook_url}"
|
||||
"#,
|
||||
);
|
||||
|
||||
Some(HelmChartScore {
|
||||
namespace: Some(NonBlankString::from_str(&config.namespace).unwrap()),
|
||||
HelmChartScore {
|
||||
namespace: Some(NonBlankString::from_str(&namespace).unwrap()),
|
||||
release_name: NonBlankString::from_str(&name).unwrap(),
|
||||
chart_name: NonBlankString::from_str(
|
||||
"oci://hub.nationtech.io/library/alertmanager-discord",
|
||||
@ -36,13 +31,5 @@ environment:
|
||||
create_namespace: true,
|
||||
install_only: true,
|
||||
repository: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn discord_alert_manager_score(config: &KubePrometheusConfig) -> HelmChartScore {
|
||||
if let Some(chart) = get_discord_alert_manager_score(config) {
|
||||
chart
|
||||
} else {
|
||||
panic!("Expected discord alert manager helm chart");
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,10 @@ use url::Url;
|
||||
|
||||
use crate::{
|
||||
interpret::{InterpretError, Outcome},
|
||||
topology::{K8sAnywhereTopology, Topology, oberservability::monitoring::AlertChannelConfig},
|
||||
topology::{
|
||||
K8sAnywhereTopology, Topology,
|
||||
oberservability::monitoring::{AlertReceiver, AlertReceiverConfig},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -13,41 +16,38 @@ pub struct DiscordWebhookConfig {
|
||||
pub send_resolved_notifications: bool,
|
||||
}
|
||||
|
||||
pub trait DiscordWebhookSender {
|
||||
fn deploy_discord_webhook_sender(
|
||||
pub trait DiscordWebhookReceiver {
|
||||
fn deploy_discord_webhook_receiver(
|
||||
&self,
|
||||
_notification_adapter_id: &str,
|
||||
) -> Result<Outcome, InterpretError>;
|
||||
|
||||
fn delete_discord_webhook_sender(
|
||||
fn delete_discord_webhook_receiver(
|
||||
&self,
|
||||
_notification_adapter_id: &str,
|
||||
) -> Result<Outcome, InterpretError>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology + DiscordWebhookSender> AlertChannelConfig<T> for DiscordWebhookConfig {
|
||||
fn channel_identifier(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
fn webhook_url(&self) -> Option<Url> {
|
||||
Some(self.webhook_url.clone())
|
||||
}
|
||||
|
||||
fn send_resolved_notifications(&self) -> bool {
|
||||
self.send_resolved_notifications.clone()
|
||||
impl<T: DiscordWebhookReceiver> AlertReceiver<T> for DiscordWebhookConfig {
|
||||
fn get_alert_receiver_config(&self) -> AlertReceiverConfig {
|
||||
AlertReceiverConfig {
|
||||
receiver_id: self.name.clone(),
|
||||
receiver_webhook_url: Some(self.webhook_url.clone()),
|
||||
send_resolved: self.send_resolved_notifications.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DiscordWebhookSender for K8sAnywhereTopology {
|
||||
fn deploy_discord_webhook_sender(
|
||||
impl DiscordWebhookReceiver for K8sAnywhereTopology {
|
||||
fn deploy_discord_webhook_receiver(
|
||||
&self,
|
||||
_notification_adapter_id: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
todo!()
|
||||
}
|
||||
fn delete_discord_webhook_sender(
|
||||
fn delete_discord_webhook_receiver(
|
||||
&self,
|
||||
_notification_adapter_id: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
|
||||
@ -96,28 +96,28 @@ impl MonitoringAlertingStackInterpret {
|
||||
topology: &T,
|
||||
config: &KubePrometheusConfig,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let mut outcomes = vec![];
|
||||
//let mut outcomes = vec![];
|
||||
|
||||
for channel in &self.score.alert_channel {
|
||||
let outcome = match channel {
|
||||
AlertChannel::Discord { .. } => {
|
||||
discord_alert_manager_score(config)
|
||||
.create_interpret()
|
||||
.execute(inventory, topology)
|
||||
.await
|
||||
}
|
||||
AlertChannel::Slack { .. } => Ok(Outcome::success(
|
||||
"No extra configs for slack alerting".to_string(),
|
||||
)),
|
||||
AlertChannel::Smpt { .. } => {
|
||||
todo!()
|
||||
}
|
||||
};
|
||||
outcomes.push(outcome);
|
||||
}
|
||||
for result in outcomes {
|
||||
result?;
|
||||
}
|
||||
//for channel in &self.score.alert_channel {
|
||||
// let outcome = match channel {
|
||||
// AlertChannel::Discord { .. } => {
|
||||
// discord_alert_manager_score(config)
|
||||
// .create_interpret()
|
||||
// .execute(inventory, topology)
|
||||
// .await
|
||||
// }
|
||||
// AlertChannel::Slack { .. } => Ok(Outcome::success(
|
||||
// "No extra configs for slack alerting".to_string(),
|
||||
// )),
|
||||
// AlertChannel::Smpt { .. } => {
|
||||
// todo!()
|
||||
// }
|
||||
// };
|
||||
// outcomes.push(outcome);
|
||||
//}
|
||||
//for result in outcomes {
|
||||
// result?;
|
||||
//}
|
||||
|
||||
Ok(Outcome::success("All alert channels deployed".to_string()))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user