-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
386 additions
and
124 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
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
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,88 @@ | ||
package alerting | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/PagerDuty/go-pagerduty" | ||
"github.com/moov-io/achgateway/internal/service" | ||
) | ||
|
||
type PagerDuty struct { | ||
client *pagerduty.Client | ||
routingKey string | ||
} | ||
|
||
func NewPagerDutyAlerter(cfg *service.PagerDutyAlerting) (*PagerDuty, error) { | ||
notifier := &PagerDuty{ | ||
client: pagerduty.NewClient(cfg.ApiKey), | ||
routingKey: cfg.RoutingKey, | ||
} | ||
if err := notifier.ping(); err != nil { | ||
return nil, err | ||
} | ||
return notifier, nil | ||
} | ||
|
||
func (pd *PagerDuty) AlertError(e error) error { | ||
if e == nil { | ||
return nil | ||
} | ||
|
||
details := make(map[string]string) | ||
|
||
hostName, err := os.Hostname() | ||
if err != nil { | ||
return fmt.Errorf("getting host name: %v", err) | ||
} | ||
|
||
dedupKey := e.Error() | ||
if _, file, line, ok := runtime.Caller(1); ok { | ||
location := fmt.Sprintf("%s:%d", file, line) | ||
details["location"] = location | ||
dedupKey += location | ||
} | ||
|
||
errorHash := fmt.Sprintf("%x", sha256.Sum256([]byte(dedupKey))) | ||
|
||
event := &pagerduty.V2Event{ | ||
RoutingKey: pd.routingKey, | ||
Action: "trigger", | ||
DedupKey: errorHash, | ||
Payload: &pagerduty.V2Payload{ | ||
Summary: e.Error(), | ||
Source: hostName, | ||
Severity: "critical", | ||
Timestamp: time.Now().Format(time.RFC3339), | ||
Details: details, | ||
}, | ||
} | ||
|
||
_, err = pd.client.ManageEvent(event) | ||
if err != nil { | ||
return fmt.Errorf("creating event in PagerDuty: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (pd *PagerDuty) ping() error { | ||
if pd == nil || pd.client == nil { | ||
return errors.New("pagerduty: nil") | ||
} | ||
|
||
// make a call and verify we don't error | ||
resp, err := pd.client.ListAbilities() | ||
if err != nil { | ||
return fmt.Errorf("pagerduty list abilities: %v", err) | ||
} | ||
if len(resp.Abilities) <= 0 { | ||
return fmt.Errorf("pagerduty: missing abilities") | ||
} | ||
|
||
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,28 @@ | ||
package alerting | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/moov-io/achgateway/internal/service" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPagerDutyErrorAlert(t *testing.T) { | ||
if os.Getenv("PD_API_KEY") == "" { | ||
t.Skip("Skip PagerDuty notification as PD_API_KEY and PD_ROUTING_KEY are not set") | ||
} | ||
|
||
cfg := &service.PagerDutyAlerting{ | ||
ApiKey: os.Getenv("PD_API_KEY"), | ||
RoutingKey: os.Getenv("PD_ROUTING_KEY"), | ||
} | ||
|
||
notifier, err := NewPagerDutyAlerter(cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, notifier) | ||
|
||
err = notifier.AlertError(errors.New("error message")) | ||
require.NoError(t, err) | ||
} |
Oops, something went wrong.