Skip to content

Add infrastructure for pagerduty integration #1498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/bin/on_call/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
extern crate cargo_registry;
extern crate reqwest;

use self::reqwest::{header, StatusCode as Status};
use std::env;

use self::cargo_registry::util::*;

#[derive(Serialize, Debug)]
#[serde(rename_all = "snake_case", tag = "event_type")]
pub enum Event {
Trigger {
incident_key: Option<String>,
description: String,
},
Acknowledge {
incident_key: String,
description: Option<String>,
},
Resolve {
incident_key: String,
description: Option<String>,
},
}

impl Event {
/// Sends the event to pagerduty.
///
/// If the variant is `Trigger`, this will page whoever is on call
/// (potentially waking them up at 3 AM).
pub fn send(self) -> CargoResult<()> {
let api_token = env::var("PAGERDUTY_API_TOKEN")?;
let service_key = env::var("PAGERDUTY_INTEGRATION_KEY")?;

let mut response = reqwest::Client::new()
.post("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
.header(header::ACCEPT, "application/vnd.pagerduty+json;version=2")
.header(header::AUTHORIZATION, format!("Token token={}", api_token))
.json(&FullEvent {
service_key,
event: self,
}).send()?;

match response.status() {
s if s.is_success() => Ok(()),
Status::BAD_REQUEST => {
let error = response.json::<InvalidEvent>()?;
Err(internal(&format_args!("pagerduty error: {:?}", error)))
}
Status::FORBIDDEN => Err(internal("rate limited by pagerduty")),
n => Err(internal(&format_args!(
"Got a non 200 response code from pagerduty: {} with {:?}",
n, response
))),
}
}
}

#[derive(Serialize, Debug)]
struct FullEvent {
service_key: String,
#[serde(flatten)]
event: Event,
}

#[derive(Deserialize, Debug)]
struct InvalidEvent {
message: String,
errors: Vec<String>,
}
39 changes: 39 additions & 0 deletions src/bin/test-pagerduty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Send a test event to pagerduty
//!
//! Usage:
//! cargo run --bin test-pagerduty event_type [description]
//!
//! Event type can be trigger, acknowledge, or resolve

#![deny(warnings)]

#[macro_use]
extern crate serde_derive;

mod on_call;

use std::env::args;

fn main() {
let args = args().collect::<Vec<_>>();

let event_type = &*args[1];
let description = args.get(2).cloned();

let event = match event_type {
"trigger" => on_call::Event::Trigger {
incident_key: Some("test".into()),
description: description.unwrap_or_else(|| "Test event".into()),
},
"acknowledge" => on_call::Event::Acknowledge {
incident_key: "test".into(),
description,
},
"resolve" => on_call::Event::Resolve {
incident_key: "test".into(),
description,
},
_ => panic!("Event type must be trigger, acknowledge, or resolve"),
};
event.send().unwrap()
}