This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add PagerDuty code * Adding files * remove test file * go fmt * Address PR comments * Add new files * remove api key * Add comments * move function around * Fix tests * Add pagerduty test for details * json marshal test detail struct * Edit comment * Pointer receiver * Address PR comments
- Loading branch information
1 parent
9e9a804
commit 5e46ae0
Showing
10 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ vendor/ | |
.vscode/ | ||
build/ | ||
bin/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package api | ||
|
||
// Alert interface is used for the various monitoring and alerting tools for Kelp. | ||
type Alert interface { | ||
Trigger(description string, details interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package monitoring | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lightyeario/kelp/api" | ||
) | ||
|
||
// MakeAlert creates an Alert based on the type of the service (eg Pager Duty) and its corresponding API key. | ||
func MakeAlert(alertType string, apiKey string) (api.Alert, error) { | ||
switch alertType { | ||
case "PagerDuty": | ||
return makePagerDuty(apiKey) | ||
default: | ||
return nil, fmt.Errorf("cannot make alert - invalid alert type: %s", alertType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package monitoring | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
"github.com/lightyeario/kelp/api" | ||
) | ||
|
||
type pagerDuty struct { | ||
serviceKey string | ||
} | ||
|
||
// ensure pagerDuty implements the api.Alert interface | ||
var _ api.Alert = &pagerDuty{} | ||
|
||
func makePagerDuty(serviceKey string) (api.Alert, error) { | ||
return &pagerDuty{ | ||
serviceKey: serviceKey, | ||
}, nil | ||
} | ||
|
||
// Trigger creates a PagerDuty trigger. The description is required and cannot be empty. Supplementary | ||
// details can be optionally provided as key-value pairs as part of the details parameter. | ||
func (p *pagerDuty) Trigger(description string, details interface{}) error { | ||
event := pagerduty.Event{ | ||
ServiceKey: p.serviceKey, | ||
Type: "trigger", | ||
Description: description, | ||
Details: details, | ||
} | ||
response, e := pagerduty.CreateEvent(event) | ||
if e != nil { | ||
return fmt.Errorf("encountered an error while sending a PagerDuty alert: %s", e) | ||
} | ||
log.Printf("Triggered PagerDuty alert. Incident key for reference: %s\n", response.IncidentKey) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package monitoring | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTriggerPagerDuty(t *testing.T) { | ||
const kelpServiceKey = "" // Fill in pager duty service key here during testing. | ||
testCases := []struct { | ||
testName string | ||
serviceKey string | ||
description string | ||
details interface{} | ||
errorExpected bool | ||
}{ | ||
{ | ||
testName: "Tests that a Pager Duty alert was triggered successfully", | ||
serviceKey: kelpServiceKey, | ||
description: "Testing monitoring package. Not a real incident!", | ||
details: nil, | ||
errorExpected: false, | ||
}, | ||
// Description cannot be empty | ||
{ | ||
testName: "Tests that a missing description causes an error", | ||
serviceKey: kelpServiceKey, | ||
description: "", | ||
details: nil, | ||
errorExpected: true, | ||
}, | ||
// Service key is invalid | ||
{ | ||
testName: "Tests that an invalid API key causes an error", | ||
serviceKey: "", | ||
description: "Testing monitoring package. Not a real incident!", | ||
details: nil, | ||
errorExpected: true, | ||
}, | ||
{ | ||
testName: "Tests that details can be passed through", | ||
serviceKey: kelpServiceKey, | ||
description: "Testing monitoring package. Not a real incident!", | ||
details: struct { | ||
LoadAvg float64 `json:"load_avg"` | ||
NumRequests int `json:"num_requests"` | ||
}{ | ||
LoadAvg: 0.5, | ||
NumRequests: 100, | ||
}, | ||
errorExpected: false, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.testName, func(t *testing.T) { | ||
pagerDutyAlert, e := MakeAlert("PagerDuty", tc.serviceKey) | ||
if !assert.Nil(t, e) { | ||
return | ||
} | ||
e = pagerDutyAlert.Trigger(tc.description, tc.details) | ||
if tc.errorExpected { | ||
assert.NotNil(t, e) | ||
} else { | ||
assert.Nil(t, e) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters