Skip to content

Commit

Permalink
Merge pull request influxdata#1512 from influxdata/md-issue#743
Browse files Browse the repository at this point in the history
Use details field from alert node in pagerduty
  • Loading branch information
desa authored Aug 8, 2017
2 parents 3b5512f + 363325a commit 6dff1be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 4 additions & 2 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7828,6 +7828,8 @@ func TestStream_AlertPagerDuty(t *testing.T) {
ts := pagerdutytest.NewServer()
defer ts.Close()

defaultDetails := "{"Name":"cpu","TaskName":"TestStream_Alert","Group":"host=serverA","Tags":{"host":"serverA"},"ServerInfo":{"Hostname":"","ClusterID":"","ServerID":""},"ID":"kapacitor/cpu/serverA","Fields":{"count":10},"Level":"CRITICAL","Time":"1971-01-01T00:00:10Z","Message":"CRITICAL alert for kapacitor/cpu/serverA"}\n"

var script = `
stream
|from()
Expand Down Expand Up @@ -7872,7 +7874,7 @@ stream
Description: "CRITICAL alert for kapacitor/cpu/serverA",
Client: "kapacitor",
ClientURL: kapacitorURL,
Details: `{"series":[{"name":"cpu","tags":{"host":"serverA"},"columns":["time","count"],"values":[["1971-01-01T00:00:10Z",10]]}]}`,
Details: defaultDetails,
},
},
pagerdutytest.Request{
Expand All @@ -7883,7 +7885,7 @@ stream
Description: "CRITICAL alert for kapacitor/cpu/serverA",
Client: "kapacitor",
ClientURL: kapacitorURL,
Details: `{"series":[{"name":"cpu","tags":{"host":"serverA"},"columns":["time","count"],"values":[["1971-01-01T00:00:10Z",10]]}]}`,
Details: defaultDetails,
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7264,6 +7264,7 @@ func TestServer_ListServiceTests(t *testing.T) {
"incident-key": "testIncidentKey",
"description": "test pagerduty message",
"level": "CRITICAL",
"details": "",
},
},
{
Expand Down Expand Up @@ -8082,7 +8083,7 @@ func TestServer_AlertHandlers(t *testing.T) {
Description: "message",
Client: "kapacitor",
ClientURL: kapacitorURL,
Details: resultJSON,
Details: "details",
},
}}
if !reflect.DeepEqual(exp, got) {
Expand Down
18 changes: 7 additions & 11 deletions services/pagerduty/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync/atomic"

"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/models"
)

type Service struct {
Expand Down Expand Up @@ -64,6 +63,7 @@ func (s *Service) Global() bool {
type testOptions struct {
IncidentKey string `json:"incident-key"`
Description string `json:"description"`
Details string `json:"details"`
Level alert.Level `json:"level"`
}

Expand All @@ -86,11 +86,11 @@ func (s *Service) Test(options interface{}) error {
o.IncidentKey,
o.Description,
o.Level,
models.Result{},
o.Details,
)
}

func (s *Service) Alert(serviceKey, incidentKey, desc string, level alert.Level, details models.Result) error {
func (s *Service) Alert(serviceKey, incidentKey, desc string, level alert.Level, details string) error {
url, post, err := s.preparePost(serviceKey, incidentKey, desc, level, details)
if err != nil {
return err
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *Service) Alert(serviceKey, incidentKey, desc string, level alert.Level,
return nil
}

func (s *Service) preparePost(serviceKey, incidentKey, desc string, level alert.Level, details models.Result) (string, io.Reader, error) {
func (s *Service) preparePost(serviceKey, incidentKey, desc string, level alert.Level, details string) (string, io.Reader, error) {

c := s.config()
if !c.Enabled {
Expand Down Expand Up @@ -147,16 +147,12 @@ func (s *Service) preparePost(serviceKey, incidentKey, desc string, level alert.
pData["client"] = "kapacitor"
pData["client_url"] = s.HTTPDService.URL()

b, err := json.Marshal(details)
if err != nil {
return "", nil, err
}
pData["details"] = string(b)
pData["details"] = details

// Post data to PagerDuty
var post bytes.Buffer
enc := json.NewEncoder(&post)
err = enc.Encode(pData)
err := enc.Encode(pData)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -190,7 +186,7 @@ func (h *handler) Handle(event alert.Event) {
event.State.ID,
event.State.Message,
event.State.Level,
event.Data.Result,
event.State.Details,
); err != nil {
h.logger.Println("E! failed to send event to PagerDuty", err)
}
Expand Down

0 comments on commit 6dff1be

Please sign in to comment.