Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use details field from alert node in pagerduty #1512

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor Author

@desa desa Aug 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default format that gets generated.


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