From 363325a5a07a2f49dcdb461267d1f72bcc64967c Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Fri, 4 Aug 2017 12:07:47 -0400 Subject: [PATCH] Use details field from alert node in pagerduty --- integrations/streamer_test.go | 6 ++++-- server/server_test.go | 3 ++- services/pagerduty/service.go | 18 +++++++----------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/integrations/streamer_test.go b/integrations/streamer_test.go index 6fac623c7..0b12c0b4d 100644 --- a/integrations/streamer_test.go +++ b/integrations/streamer_test.go @@ -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() @@ -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{ @@ -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, }, }, } diff --git a/server/server_test.go b/server/server_test.go index 002adae58..a9f3764ed 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -7264,6 +7264,7 @@ func TestServer_ListServiceTests(t *testing.T) { "incident-key": "testIncidentKey", "description": "test pagerduty message", "level": "CRITICAL", + "details": "", }, }, { @@ -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) { diff --git a/services/pagerduty/service.go b/services/pagerduty/service.go index 56c058273..10428cc42 100644 --- a/services/pagerduty/service.go +++ b/services/pagerduty/service.go @@ -12,7 +12,6 @@ import ( "sync/atomic" "github.com/influxdata/kapacitor/alert" - "github.com/influxdata/kapacitor/models" ) type Service struct { @@ -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"` } @@ -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 @@ -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 { @@ -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 } @@ -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) }