web3-proxy/web3_proxy/src/bin/web3_proxy_cli/pagerduty.rs

92 lines
2.9 KiB
Rust
Raw Normal View History

2023-01-24 11:05:41 +03:00
use argh::FromArgs;
use log::{error, info};
2023-01-24 13:45:48 +03:00
use pagerduty_rs::{eventsv2async::EventsV2 as PagerdutyAsyncEventsV2, types::Event};
use web3_proxy::{
config::TopConfig,
2023-01-24 14:12:23 +03:00
pagerduty::{pagerduty_alert, pagerduty_event_for_config},
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,
#[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>,
#[argh(option, default = "\"web3-proxy\".to_string()")]
/// a cluster or grouping of sources.
/// For example, sources "ethereum-proxy" and "polygon-proxy" might both be part of "web3-proxy".
group: 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| {
pagerduty_event_for_config(
self.class.clone(),
self.component.clone(),
2023-01-24 14:12:23 +03:00
None::<()>,
2023-01-24 13:45:48 +03:00
Some(self.group.clone()),
2023-01-24 14:12:23 +03:00
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 13:45:48 +03:00
None,
self.class,
2023-01-24 14:12:23 +03:00
"web3-proxy".to_string(),
2023-01-24 13:45:48 +03:00
None,
self.component,
2023-01-24 14:12:23 +03:00
None::<()>,
2023-01-24 13:45:48 +03:00
Some(self.group),
2023-01-24 14:12:23 +03:00
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 {
info!(
"sending to pagerduty: {}",
serde_json::to_string_pretty(&event)?
);
if let Err(err) = pagerduty_async.event(Event::AlertTrigger(event)).await {
error!("Failed sending to pagerduty: {}", err);
}
} else {
info!(
"would send to pagerduty if PAGERDUTY_INTEGRATION_KEY were set: {}",
serde_json::to_string_pretty(&event)?
);
}
Ok(())
}
}