Skip to content

Commit

Permalink
fix(bigpanda): fields of alerting data point are serialized to proper…
Browse files Browse the repository at this point in the history
… types (#2592)
  • Loading branch information
bednar authored Jul 15, 2021
1 parent 5da2576 commit df99b44
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
### Bugfixes
- [#2564](https://github.com/influxdata/kapacitor/pull/2564): Fix a panic in the scraper handler when debug mode is enabled
- [#2579](https://github.com/influxdata/kapacitor/pull/2579): Fix: cli auth and error handling for flux tasks
- [#2592](https://github.com/influxdata/kapacitor/pull/2592): Fix: payload serialization for BigPanda

## v1.5.9 [2021-04-01]

Expand Down
2 changes: 1 addition & 1 deletion services/bigpanda/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *Service) preparePost(id string, message string, details string, level a
}

for k, v := range data.Fields {
bpData[k] = fmt.Sprintf("%v", v)
bpData[k] = v
}

var post bytes.Buffer
Expand Down
66 changes: 66 additions & 0 deletions services/bigpanda/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package bigpanda

import (
"bytes"
"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/models"
"testing"
"time"
)

func TestService_SerializeEventData(t *testing.T) {
config := Config{Enabled: true, AppKey: "key"}
s, err := NewService(config, nil)
if err != nil {
t.Fatal(err)
}

testCases := []struct {
fields map[string]interface{}
expBody string
}{
{
fields: map[string]interface{}{"primitive_type": 10},
expBody: "{\"app_key\":\"key\",\"check\":\"id\",\"description\":\"message\",\"details\":\"details\",\"primitive_type\":10,\"status\":\"ok\",\"task\":\":test\",\"timestamp\":31536038}\n",
},
{
fields: map[string]interface{}{"escape": "\n"},
expBody: "{\"app_key\":\"key\",\"check\":\"id\",\"description\":\"message\",\"details\":\"details\",\"escape\":\"\\n\",\"status\":\"ok\",\"task\":\":test\",\"timestamp\":31536038}\n",
},
{
fields: map[string]interface{}{"array": []interface{}{10, true, "string value"}},
expBody: "{\"app_key\":\"key\",\"array\":[10,true,\"string value\"],\"check\":\"id\",\"description\":\"message\",\"details\":\"details\",\"status\":\"ok\",\"task\":\":test\",\"timestamp\":31536038}\n",
},
}

for _, tc := range testCases {
r, err := s.preparePost(
"id",
"message",
"details",
alert.OK,
time.Date(1971, 1, 1, 0, 0, 38, 0, time.UTC),
alert.EventData{
Name: "test",
Tags: make(map[string]string),
Fields: tc.fields,
Result: models.Result{},
},
&HandlerConfig{})

if err != nil {
t.Fatal(err)
}

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(r.Body)
if err != nil {
t.Fatal(err)
}
newStr := buf.String()

if tc.expBody != newStr {
t.Fatalf("unexpected content: got '%s' exp '%s'", newStr, tc.expBody)
}
}
}

0 comments on commit df99b44

Please sign in to comment.