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.
| Variable | Value |
|---|---|
pdEvents | https://events.pagerduty.com/v2/enqueue |
pdRoutingKey | the integration key, marked as secret |
pdApi | https://api.pagerduty.com |
pdToken | a REST API key, marked as secret |
pdServiceId | the test service's id |
Step 2: Trigger
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"}
}
}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:
GET {{pdApi}}/incidents?service_ids[]={{pdServiceId}}&statuses[]=triggered&statuses[]=acknowledged
Authorization: Token token={{pdToken}}
Accept: application/vnd.pagerduty+json;version=2const 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
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:
requestarium run-collection "PagerDuty" --folder "Alert path" --env PagerDuty \
--env-var pdRoutingKey=PD_ROUTING_KEY --env-var pdToken=PD_TOKEN --delay 5000If 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
| API | Status | Cause |
|---|---|---|
| Events | 400 | invalid event: missing fields, bad severity |
| Events | 429 | too many events for this routing key |
| REST | 401 | bad API key |
| REST | 403 | the key's user can't see that service |