feat: added an example of adding an msteams channel
This commit is contained in:
		
							parent
							
								
									78aadadd22
								
							
						
					
					
						commit
						f96572848a
					
				
							
								
								
									
										14
									
								
								examples/ms_teams_alert_channel/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								examples/ms_teams_alert_channel/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | [package] | ||||||
|  | name = "ms_teams_alert_channel" | ||||||
|  | edition = "2024" | ||||||
|  | version.workspace = true | ||||||
|  | readme.workspace = true | ||||||
|  | license.workspace = true | ||||||
|  | 
 | ||||||
|  | [dependencies] | ||||||
|  | harmony = { version = "0.1.0", path = "../../harmony" } | ||||||
|  | harmony_cli = { version = "0.1.0", path = "../../harmony_cli" } | ||||||
|  | serde = "1.0.219" | ||||||
|  | tokio.workspace = true | ||||||
|  | typetag = "0.2.20" | ||||||
|  | url.workspace = true | ||||||
							
								
								
									
										65
									
								
								examples/ms_teams_alert_channel/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								examples/ms_teams_alert_channel/src/main.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | |||||||
|  | mod prometheus_msteams; | ||||||
|  | use harmony::{ | ||||||
|  |     interpret::InterpretError, inventory::Inventory, maestro::Maestro, modules::{helm::chart::HelmChartScore, monitoring::{kube_prometheus::{prometheus_alert_channel::PrometheusAlertChannel, types::{AlertChannelConfig, AlertChannelReceiver, AlertChannelRoute, WebhookConfig}}, monitoring_alerting::MonitoringAlertingScore}}, topology::K8sAnywhereTopology | ||||||
|  | }; | ||||||
|  | use prometheus_msteams::prometheus_msteams_score; | ||||||
|  | use url::Url; | ||||||
|  | use serde::{Serialize, Deserialize}; | ||||||
|  | 
 | ||||||
|  | #[tokio::main] | ||||||
|  | async fn main() { | ||||||
|  |     let alert_channels: Vec<Box<dyn PrometheusAlertChannel>> = vec![Box::new(MSTeamsChannel { | ||||||
|  |         connector: "teams-test".to_string(), | ||||||
|  |         webhook_url: url::Url::parse( | ||||||
|  |         "https://msteams.com/services/dummy/dummy/dummy", | ||||||
|  |     ) | ||||||
|  |     .expect("invalid URL"), | ||||||
|  |     })]; | ||||||
|  | 
 | ||||||
|  |     let monitoring_score = MonitoringAlertingScore { | ||||||
|  |         alert_channels, | ||||||
|  |         namespace: None, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     let mut maestro = Maestro::<K8sAnywhereTopology>::initialize( | ||||||
|  |         Inventory::autoload(), | ||||||
|  |         K8sAnywhereTopology::new(), | ||||||
|  |     ) | ||||||
|  |     .await | ||||||
|  |     .unwrap(); | ||||||
|  | 
 | ||||||
|  |     maestro.register_all(vec![Box::new(monitoring_score)]); | ||||||
|  |     harmony_cli::init(maestro, None).await.unwrap(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[derive(Debug, Clone, Serialize, Deserialize)] | ||||||
|  | struct MSTeamsChannel { | ||||||
|  |     connector: String, | ||||||
|  |     webhook_url: Url, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[typetag::serde] | ||||||
|  | impl PrometheusAlertChannel for MSTeamsChannel { | ||||||
|  |     fn get_alert_manager_config_contribution(&self) -> Result<AlertChannelConfig, InterpretError> { | ||||||
|  |     Ok(AlertChannelConfig{ 
 | ||||||
|  |                 receiver: AlertChannelReceiver{ 
 | ||||||
|  |                     name: format!("MSTeams-{}",self.connector), 
 | ||||||
|  |                     slack_configs: None, 
 | ||||||
|  |                     webhook_configs: Some(vec![WebhookConfig{ | ||||||
|  |                         url: url::Url::parse("http://prometheus-msteams-prometheus-msteams.monitoring.svc.cluster.local:2000/alertmanager").expect("invalid url"), | ||||||
|  |                         send_resolved: true,}]) | ||||||
|  |                     }, | ||||||
|  |                 route: AlertChannelRoute{ 
 | ||||||
|  |                     receiver: format!("MSTeams-{}", self.connector), 
 | ||||||
|  |                     matchers: vec!["alertname!=Watchdog".to_string()], 
 | ||||||
|  |                     r#continue: true, | ||||||
|  |                 }, 
 | ||||||
|  |                 global_config: None, }) | ||||||
|  |     } | ||||||
|  |     fn get_dependency_score(&self, ns: String) -> Option<HelmChartScore> { | ||||||
|  |         Some(prometheus_msteams_score(self.connector.clone(), self.webhook_url.clone(), ns.clone())) | ||||||
|  |     } | ||||||
|  | }   
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
							
								
								
									
										30
									
								
								examples/ms_teams_alert_channel/src/prometheus_msteams.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/ms_teams_alert_channel/src/prometheus_msteams.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | |||||||
|  | use std::str::FromStr; | ||||||
|  | 
 | ||||||
|  | use harmony::modules::helm::chart::{HelmChartScore, NonBlankString}; | ||||||
|  | use url::Url; | ||||||
|  | 
 | ||||||
|  | pub fn prometheus_msteams_score( | ||||||
|  |     name: String, | ||||||
|  |     webhook_url: Url, | ||||||
|  |     namespace: String, | ||||||
|  | ) -> HelmChartScore { | ||||||
|  |     let values = format!( | ||||||
|  |         r#" | ||||||
|  |  connectors: | ||||||
|  |    - default: "{webhook_url}" | ||||||
|  |  "#,
 | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     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/prometheus-msteams") | ||||||
|  |             .unwrap(), | ||||||
|  |         chart_version: None, | ||||||
|  |         values_overrides: None, | ||||||
|  |         values_yaml: Some(values.to_string()), | ||||||
|  |         create_namespace: true, | ||||||
|  |         install_only: true, | ||||||
|  |         repository: None, | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user