Skip to content

Commit 177e016

Browse files
author
Adil Baig
authored
Events v2 (#4)
Completed implementation for EventsAPI V2
1 parent 788fb52 commit 177e016

File tree

6 files changed

+218
-133
lines changed

6 files changed

+218
-133
lines changed

README.md

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
PHP PagerDuty Events API
22
=========
3-
PHP implementation of the [PagerDuty Events API](https://v2.developer.pagerduty.com/v2/docs/events-api)
3+
PHP implementation of the [PagerDuty Events API V2](https://v2.developer.pagerduty.com/docs/events-api-v2)
44

5-
**Important**: v2 is a complete rewrite of the library. It is not backwards compatible with v1.
65

7-
For all new projects i suggest using v2. It is more flexible and easier to use than v1, and overcomes some of its predecessor's design limitations.
6+
UPGRADE NOTICE
7+
---
8+
The [Events API V2](https://v2.developer.pagerduty.com/docs/events-api-v2) is **not backwards compatible** with the [Events API V1](https://v2.developer.pagerduty.com/docs/events-api). Hence, this API has changed. If you are upgrading from a [2.* release](https://github.com/adilbaig/pagerduty/releases), make sure you pay attention to the contructor of the `TriggerEvent`
89

910
[![Latest Stable Version](https://poser.pugx.org/adilbaig/pagerduty/v/stable.svg)](https://packagist.org/packages/adilbaig/pagerduty) [![Total Downloads](https://poser.pugx.org/adilbaig/pagerduty/downloads.svg)](https://packagist.org/packages/adilbaig/pagerduty)
1011

11-
Features :
12-
----
12+
Features
13+
---
1314

15+
- Compatible with [PD Events API V2](https://v2.developer.pagerduty.com/v2/docs/#the-events-api).
1416
- Trigger, acknowledge and resolve incidents.
15-
- Support for [Event contexts](https://v2.developer.pagerduty.com/v2/docs/trigger-events#contexts).
16-
- Works with [Events API V1](https://v2.developer.pagerduty.com/v2/docs/#the-events-api).
17+
- Supports [Event contexts](https://v2.developer.pagerduty.com/v2/docs/trigger-events#contexts). Attach links and images to your incident reports.
1718
- Unit Tests
1819

1920

20-
Installation :
21-
----
21+
Installation
22+
---
2223
Add this line to your project's `composer.json`
2324
````
2425
{
2526
...
2627
"require": {
27-
"adilbaig/pagerduty": "2.*"
28+
"adilbaig/pagerduty": "3.*"
2829
}
2930
...
3031
}
3132
````
3233

3334
The packagist URL is https://packagist.org/packages/adilbaig/pagerduty
3435

35-
Usage:
36-
----
36+
Usage
37+
---
3738

3839
Trigger an event
3940

4041
````php
4142
use \PagerDuty\TriggerEvent;
4243
use \PagerDuty\PagerDutyException;
4344

44-
$serviceKey = "1d334a4819fc4b67a795b1c54f9a"; //Replace this with the integration key of your service.
45+
$routingKey = "1d334a4819fc4b67a795b1c54f9a"; //Replace this with the integration key of your service.
4546

46-
// In this example, we're triggering a "Service is down" message.
47+
// In this example, we're triggering a "Service is down" message from a web server.
4748
try {
48-
$responseCode = (new TriggerEvent($serviceKey, "Service is down"))->send();
49+
$event = new TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true);
50+
$responseCode = $event->send();
4951
if($responseCode == 200)
5052
echo "Success";
51-
elseif($responseCode == 403)
53+
elseif($responseCode == 429)
5254
echo "Rate Limited"; //You're being throttled. Slow down.
55+
else // An error occured. Try again later
56+
echo "Some error has occured. Try again later";
5357
} catch(PagerDutyException $exception) { //This doesn't happen unless you've broken their guidelines. The API tries to minimize user mistakes
5458
var_dump($exception->getErrors());
5559
}
@@ -60,53 +64,73 @@ Automatically send only one PagerDuty incident for repeated errors
6064

6165
````php
6266

63-
//After this example, you will see just one incident on PD
64-
65-
(new TriggerEvent($serviceKey, "Service is down", true))->send();
66-
(new TriggerEvent($serviceKey, "Service is down", true))->send();
67-
(new TriggerEvent($serviceKey, "Service is down", true))->send();
67+
//You will only see one incident on PD
68+
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();
69+
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();
70+
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();
6871

6972
````
7073

71-
Create a detailed 'trigger' event, add optional data. Dump the event and inspect
72-
response from PD
74+
Create a detailed 'trigger' event, add optional data. Dump the event and inspect response from PD
7375

7476
````php
7577
use \PagerDuty\TriggerEvent;
7678
use \PagerDuty\Context\LinkContext;
7779
use \PagerDuty\Context\ImageContext;
7880

79-
//Taken from the `trigger` example @ https://v2.developer.pagerduty.com/v2/docs/trigger-events
81+
//Taken from the `trigger` example @ https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
82+
//Send a detailed event, and store the `dedup_key` generated on the server
8083

81-
$event = new TriggerEvent($serviceKey, "FAILURE for production/HTTP on machine srv01.acme.com");
84+
$event = new TriggerEvent(
85+
$routingKey,
86+
"Example alert on host1.example.com",
87+
"monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003",
88+
TriggerEvent::INFO
89+
);
8290
$event
83-
->setClient("Sample Monitoring Service")
84-
->setClientURL("https://monitoring.service.com")
85-
->setDetails(["ping time" => "1500ms", "load avg" => 0.75])
86-
->addContext(new LinkContext("http://acme.pagerduty.com"))
87-
->addContext(new LinkContext("http://acme.pagerduty.com", "View the incident on PagerDuty"))
88-
->addContext(new ImageContext("https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1"));
91+
->setPayloadTimestamp("2015-07-17T08:42:58.315+0000")
92+
->setPayloadComponent("postgres")
93+
->setPayloadGroup("prod-datapipe")
94+
->setPayloadClass("deploy")
95+
->setPayloadCustomDetails(["ping_time" => "1500ms", "load_avg" => 0.75])
96+
->addContext(new LinkContext("https://example.com/", "Link text"))
97+
->addContext(new ImageContext("https://www.pagerduty.com/wp-content/uploads/2016/05/pagerduty-logo-green.png", "https://example.com/", "Example text"))
98+
;
8999

90100
// Pass in the '$response' variable by reference if you want to inspect PD's response. This is optional, and you probably don't need this in production.
91101
$response = null;
92102
$responseCode = $event->send($response);
93-
var_dump($response);
103+
// In this case, we will save the `dedup_key` generated by the PD server
104+
var_dump($response['dedup_key']);
94105
````
95106

96107
Acknowledge an event
108+
----
97109

98110
````php
99-
(new AcknowledgeEvent($serviceKey, "incident key"))->send();
111+
(new AcknowledgeEvent($routingKey, "dedup key"))->send();
100112
````
101113

102114
Resolve an event
103-
115+
----
104116
````php
105-
(new ResolveEvent($serviceKey, "incident key"))->send();
117+
(new ResolveEvent($routingKey, "dedup key"))->send();
118+
````
119+
120+
UnitTests
121+
---
122+
123+
````bash
124+
> ./vendor/bin/phpunit test/
125+
..... 5 / 5 (100%)
126+
127+
Time: 37 ms, Memory: 4.00MB
128+
129+
OK (5 tests, 6 assertions)
106130
````
107131

108132
Questions
109-
----
133+
---
110134

111135
**Q.** How do i get the service key from PagerDuty?
112136

@@ -115,7 +139,7 @@ Questions
115139
Read more here : https://v2.developer.pagerduty.com/v2/docs/events-api#getting-started
116140

117141
Requirements
118-
----
142+
---
119143
This library needs the [curl pecl extension](https://php.net/curl).
120144

121145
In Ubuntu 16.04, install it like so :

src/PagerDuty/AcknowledgeEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
class AcknowledgeEvent extends Event
1111
{
1212

13-
public function __construct($serviceKey, $incidentKey)
13+
public function __construct($routingKey, $dedupKey)
1414
{
15-
parent::__construct($serviceKey, 'acknowledge');
15+
parent::__construct($routingKey, 'acknowledge');
1616

17-
$this->setIncidentKey($incidentKey);
17+
$this->setDeDupKey($dedupKey);
1818
}
1919
}

src/PagerDuty/Event.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* An abstract Event
7-
*
7+
*
88
* @author adil
99
*/
1010
abstract class Event implements \ArrayAccess, \JsonSerializable
@@ -14,40 +14,40 @@ abstract class Event implements \ArrayAccess, \JsonSerializable
1414

1515
/**
1616
* ctor
17-
*
18-
* @param string $serviceKey
19-
* @param string $type - One of trigger, acknowledge or resolve
17+
*
18+
* @param string $routingKey
19+
* @param string $type - One of 'trigger', 'acknowledge' or 'resolve'
2020
*/
21-
protected function __construct($serviceKey, $type)
21+
protected function __construct($routingKey, $type)
2222
{
23-
$this->dict['service_key'] = (string) $serviceKey;
24-
$this->dict['event_type'] = (string) $type;
23+
$this->dict['routing_key'] = (string) $routingKey;
24+
$this->dict['event_action'] = (string) $type;
2525
}
2626

2727
/**
28-
* A unique incident key to identify an outage.
29-
* Multiple events with the same $incidentKey will be grouped into one open incident. From the PD docs :
30-
*
31-
* `Submitting subsequent events for the same incident_key will result in those events being applied to an open incident
32-
* matching that incident_key. Once the incident is resolved, any further events with the same incident_key will
33-
* create a new incident (for trigger events) or be dropped (for acknowledge and resolve events).`
34-
*
35-
* @link https://v2.developer.pagerduty.com/docs/events-api#incident-de-duplication-and-incident_key
36-
*
37-
* @param string $incidentKey
38-
*
28+
* A unique key to identify an outage.
29+
* Multiple events with the same $key will be grouped into one open incident. From the PD docs :
30+
*
31+
* `Submitting subsequent events for the same `dedup_key` will result in those events being applied to an open alert
32+
* matching that `dedup_key`. Once the alert is resolved, any further events with the same `dedup_key` will create a
33+
* new alert (for `trigger` events) or be dropped (for `acknowledge` and `resolve` events).`
34+
*
35+
* @link https://v2.developer.pagerduty.com/docs/events-api-v2#alert-de-duplication
36+
*
37+
* @param string $key
38+
*
3939
* @return self
4040
*/
41-
public function setIncidentKey($incidentKey)
41+
public function setDeDupKey($key)
4242
{
43-
$this->dict['incident_key'] = (string) $incidentKey;
43+
$this->dict['dedup_key'] = substr((string) $key, 0, 255);
4444
return $this;
4545
}
4646

4747
/**
48-
* Get the array
48+
* Get the request json as an array
4949
* Useful for debugging or logging.
50-
*
50+
*
5151
* @return array
5252
*/
5353
public function toArray()
@@ -57,27 +57,27 @@ public function toArray()
5757

5858
/**
5959
* Send the event to PagerDuty
60-
*
60+
*
6161
* @param array $result (Opt)(Pass by reference) - If this parameter is given the result of the CURL call will be filled here. The response is an associative array.
62-
*
62+
*
6363
* @throws PagerDutyException - If status code == 400
64-
*
64+
*
6565
* @return int - HTTP response code
66-
* 200 - Event Processed
66+
* 202 - Event Processed
6767
* 400 - Invalid Event. Throws a PagerDutyException
6868
* 403 - Rate Limited. Slow down and try again later.
6969
*/
7070
public function send(&$result = null)
7171
{
7272
$jsonStr = json_encode($this);
7373

74-
$curl = curl_init("https://events.pagerduty.com/generic/2010-04-15/create_event.json");
74+
$curl = curl_init("https://events.pagerduty.com/v2/enqueue");
7575
curl_setopt($curl, CURLOPT_POST, 1);
7676
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonStr);
7777
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
7878
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
7979
'Content-Type: application/json',
80-
'Content-Length: ' . strlen($jsonStr)
80+
'Content-Length: ' . strlen($jsonStr),
8181
));
8282

8383
$result = json_decode(curl_exec($curl), true);

src/PagerDuty/ResolveEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
class ResolveEvent extends Event
1111
{
1212

13-
public function __construct($serviceKey, $incidentKey)
13+
public function __construct($routingKey, $dedupKey)
1414
{
15-
parent::__construct($serviceKey, 'resolve');
15+
parent::__construct($routingKey, 'resolve');
1616

17-
$this->setIncidentKey($incidentKey);
17+
$this->setDeDupKey($dedupKey);
1818
}
1919
}

0 commit comments

Comments
 (0)