2023-06-29 22:24:36 +03:00
|
|
|
use crate::{
|
|
|
|
config::TopConfig,
|
|
|
|
pagerduty::{pagerduty_alert, pagerduty_alert_for_config},
|
|
|
|
};
|
2023-01-24 11:05:41 +03:00
|
|
|
use argh::FromArgs;
|
2023-01-24 13:45:48 +03:00
|
|
|
use pagerduty_rs::{eventsv2async::EventsV2 as PagerdutyAsyncEventsV2, types::Event};
|
2023-06-22 22:10:23 +03:00
|
|
|
use serde_json::json;
|
2023-06-24 02:28:45 +03:00
|
|
|
use tracing::{error, info};
|
2023-01-24 11:05:41 +03:00
|
|
|
|
|
|
|
#[derive(FromArgs, PartialEq, Debug, Eq)]
|
|
|
|
/// Quickly create a pagerduty alert
|
|
|
|
#[argh(subcommand, name = "pagerduty")]
|
|
|
|
pub struct PagerdutySubCommand {
|
|
|
|
#[argh(positional)]
|
|
|
|
/// short description of the alert
|
|
|
|
summary: String,
|
|
|
|
|
2023-01-24 15:17:39 +03:00
|
|
|
/// the chain id to require. Only used if not using --config.
|
|
|
|
#[argh(option)]
|
|
|
|
chain_id: Option<u64>,
|
|
|
|
|
2023-01-24 11:05:41 +03:00
|
|
|
#[argh(option)]
|
|
|
|
/// the class/type of the event
|
|
|
|
class: Option<String>,
|
|
|
|
|
2023-01-24 13:45:48 +03:00
|
|
|
#[argh(option)]
|
|
|
|
/// the component of the event
|
|
|
|
component: Option<String>,
|
|
|
|
|
2023-01-24 11:05:41 +03:00
|
|
|
#[argh(option)]
|
|
|
|
/// deduplicate alerts based on this key.
|
|
|
|
/// If there are no open incidents with this key, a new incident will be created.
|
|
|
|
/// If there is an open incident with a matching key, the new event will be appended to that incident's Alerts log as an additional Trigger log entry.
|
|
|
|
dedup_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PagerdutySubCommand {
|
|
|
|
pub async fn main(
|
2023-01-24 13:45:48 +03:00
|
|
|
self,
|
2023-01-24 11:05:41 +03:00
|
|
|
pagerduty_async: Option<PagerdutyAsyncEventsV2>,
|
|
|
|
top_config: Option<TopConfig>,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-01-24 14:12:23 +03:00
|
|
|
// TODO: allow customizing severity
|
2023-01-24 13:45:48 +03:00
|
|
|
let event = top_config
|
|
|
|
.map(|top_config| {
|
2023-01-26 01:04:06 +03:00
|
|
|
pagerduty_alert_for_config(
|
2023-01-24 13:45:48 +03:00
|
|
|
self.class.clone(),
|
|
|
|
self.component.clone(),
|
2023-01-24 14:12:23 +03:00
|
|
|
None::<()>,
|
|
|
|
pagerduty_rs::types::Severity::Error,
|
2023-01-24 13:45:48 +03:00
|
|
|
self.summary.clone(),
|
|
|
|
None,
|
2023-01-24 14:12:23 +03:00
|
|
|
top_config,
|
2023-01-24 13:45:48 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
2023-01-24 14:12:23 +03:00
|
|
|
pagerduty_alert(
|
2023-01-24 15:17:39 +03:00
|
|
|
self.chain_id,
|
2023-01-24 13:45:48 +03:00
|
|
|
self.class,
|
2023-01-26 01:04:06 +03:00
|
|
|
None,
|
2023-01-24 13:45:48 +03:00
|
|
|
None,
|
|
|
|
self.component,
|
2023-01-24 14:12:23 +03:00
|
|
|
None::<()>,
|
|
|
|
pagerduty_rs::types::Severity::Error,
|
2023-01-24 13:45:48 +03:00
|
|
|
None,
|
|
|
|
self.summary,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
});
|
2023-01-24 11:05:41 +03:00
|
|
|
|
|
|
|
if let Some(pagerduty_async) = pagerduty_async {
|
2023-06-22 22:10:23 +03:00
|
|
|
info!("sending to pagerduty: {:#}", json!(&event));
|
2023-01-24 11:05:41 +03:00
|
|
|
|
|
|
|
if let Err(err) = pagerduty_async.event(Event::AlertTrigger(event)).await {
|
|
|
|
error!("Failed sending to pagerduty: {}", err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info!(
|
2023-06-22 22:10:23 +03:00
|
|
|
"would send to pagerduty if PAGERDUTY_INTEGRATION_KEY were set: {:#}",
|
|
|
|
json!(&event)
|
2023-01-24 11:05:41 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|