Testing PagerDuty: trigger, acknowledge and resolve test incidents

An alerting path that has never been tested is a guess. The monitor fires, the webhook posts, PagerDuty creates an incident, someone's phone rings. Each step can break, and the first time you find out is the outage. PagerDuty's two APIs let you test the path on purpose.

Step 1: A test service

Create a service called "API tests" with an escalation policy that notifies only you (or nobody), and add the Events API v2 integration. Copy its integration key: that's the routing key.

VariableValue
pdEventshttps://events.pagerduty.com/v2/enqueue
pdRoutingKeythe integration key, marked as secret
pdApihttps://api.pagerduty.com
pdTokena REST API key, marked as secret
pdServiceIdthe test service's id

Step 2: Trigger

HTTP
POST {{pdEvents}}
Content-Type: application/json

{
  "routing_key": "{{pdRoutingKey}}",
  "event_action": "trigger",
  "dedup_key": "api-test-{{$timestamp}}",
  "payload": {
    "summary": "API test alert: orders-api error rate above 5%",
    "source": "requestarium-tests",
    "severity": "warning",
    "custom_details": {"error_rate": "6.2%", "runbook": "https://wiki.example.com/orders"}
  }
}
JavaScript
const r = pm.response.json();
pm.test('accepted', () => {
  pm.expect(pm.response.code).to.equal(202);
  pm.expect(r.status).to.equal('success');
});
pm.environment.set('dedupKey', r.dedup_key);

severity must be one of critical, error, warning or info. A missing summary or source answers 400 with an errors array.

Step 3: Find the incident

The event is accepted before the incident exists, so give it a few seconds:

HTTP
GET {{pdApi}}/incidents?service_ids[]={{pdServiceId}}&statuses[]=triggered&statuses[]=acknowledged
Authorization: Token token={{pdToken}}
Accept: application/vnd.pagerduty+json;version=2
JavaScript
const incidents = pm.response.json().incidents;
const mine = incidents.find((i) => i.incident_key === pm.environment.get('dedupKey'));
pm.test('incident created for the alert', () => pm.expect(mine).to.exist);

Use API key auth for the REST requests: header Authorization, value Token token={{pdToken}}. Note the unusual format: it's not a Bearer token.

Step 4: Acknowledge and resolve

HTTP
POST {{pdEvents}}
Content-Type: application/json

{"routing_key": "{{pdRoutingKey}}", "event_action": "resolve", "dedup_key": "{{dedupKey}}"}

Use "event_action": "acknowledge" first if you want to test that step too. The same dedup_key ties all three to one incident. Sending trigger again with the same key while it's open doesn't create a second incident; it adds to the first. That's the de-duplication your monitors rely on, and worth one test.

Step 5: Make it a scheduled check

Put trigger, find, resolve in a folder and run it weekly from CI against the test service:

Terminal
requestarium run-collection "PagerDuty" --folder "Alert path" --env PagerDuty \
  --env-var pdRoutingKey=PD_ROUTING_KEY --env-var pdToken=PD_TOKEN --delay 5000

If the integration key was rotated or the service was deleted, this fails on a quiet Tuesday, not during an incident. See scheduled API monitoring with CI.

Errors

APIStatusCause
Events400invalid event: missing fields, bad severity
Events429too many events for this routing key
REST401bad API key
REST403the key's user can't see that service