Skip to content

Commit

Permalink
Allow .serviceKey for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
onlynone committed May 30, 2018
1 parent 45595b0 commit 25662ac
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
106 changes: 106 additions & 0 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9354,6 +9354,112 @@ stream
}
}

func TestStream_AlertPagerDuty2_ServiceKey(t *testing.T) {
ts := pagerduty2test.NewServer()
defer ts.Close()

detailsTmpl := map[string]interface{}{
"result": map[string]interface{}{
"series": []interface{}{
map[string]interface{}{
"name": "cpu",
"tags": map[string]interface{}{
"host": "serverA",
},
"columns": []interface{}{"time", "count"},
"values": []interface{}{
[]interface{}{"1971-01-01T00:00:10Z", float64(10)},
},
},
},
},
}

var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|alert()
.id('kapacitor/{{ .Name }}/{{ index .Tags "host" }}')
.message('{{ .Level }} alert for {{ .ID }}')
.info(lambda: "count" > 6.0)
.warn(lambda: "count" > 7.0)
.crit(lambda: "count" > 8.0)
.pagerDuty2()
.pagerDuty2()
.serviceKey('test_override_key')
`

var kapacitorURL string
tmInit := func(tm *kapacitor.TaskMaster) {
c := pagerduty2.NewConfig()
c.Enabled = true
c.URL = ts.URL
c.RoutingKey = "routing_key"
pd := pagerduty2.NewService(c, diagService.NewPagerDuty2Handler())
pd.HTTPDService = tm.HTTPDService
tm.PagerDuty2Service = pd

kapacitorURL = tm.HTTPDService.URL()
}
testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

exp := []interface{}{
pagerduty2test.Request{
URL: "/",
PostData: pagerduty2test.PostData{
Client: "kapacitor",
ClientURL: kapacitorURL,
EventAction: "trigger",
DedupKey: "kapacitor/cpu/serverA",
Payload: &pagerduty2test.PDCEF{
Summary: "CRITICAL alert for kapacitor/cpu/serverA",
Source: "serverA",
Severity: "critical",
Class: "TestStream_Alert",
CustomDetails: detailsTmpl,
Timestamp: "1971-01-01T00:00:10.000000000Z",
},
RoutingKey: "routing_key",
},
},
pagerduty2test.Request{
URL: "/",
PostData: pagerduty2test.PostData{
Client: "kapacitor",
ClientURL: kapacitorURL,
EventAction: "trigger",
DedupKey: "kapacitor/cpu/serverA",
Payload: &pagerduty2test.PDCEF{
Summary: "CRITICAL alert for kapacitor/cpu/serverA",
Source: "serverA",
Severity: "critical",
Class: "TestStream_Alert",
CustomDetails: detailsTmpl,
Timestamp: "1971-01-01T00:00:10.000000000Z",
},
RoutingKey: "test_override_key",
},
},
}

ts.Close()
var got []interface{}
for _, g := range ts.Requests() {
got = append(got, g)
}

if err := compareListIgnoreOrder(got, exp, nil); err != nil {
t.Error(err)
}
}

func TestStream_AlertHTTPPost(t *testing.T) {
ts := httpposttest.NewAlertServer(nil, false)
defer ts.Close()
Expand Down
10 changes: 10 additions & 0 deletions pipeline/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,16 @@ type PagerDuty2Handler struct {
// The routing key to use for the alert.
// Defaults to the value in the configuration if empty.
RoutingKey string `json:"routingKey"`

// tick:ignore
_ string `tick:"ServiceKey"`
}

// Allow ServiceKey as backwards compatible way to set the routing key
// tick:property
func (pd2 *PagerDuty2Handler) ServiceKey(serviceKey string) *PagerDuty2Handler {
pd2.RoutingKey = serviceKey
return pd2
}

// Send the alert to HipChat.
Expand Down
18 changes: 18 additions & 0 deletions pipeline/tick/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ func TestAlertPagerDuty2(t *testing.T) {
PipelineTickTestHelper(t, pipe, want)
}

func TestAlertPagerDuty2_ServiceKey(t *testing.T) {
pipe, _, from := StreamFrom()
handler := from.Alert().PagerDuty2()
handler.ServiceKey("LeafsNation")

want := `stream
|from()
|alert()
.id('{{ .Name }}:{{ .Group }}')
.message('{{ .ID }} is {{ .Level }}')
.details('{{ json . }}')
.history(21)
.pagerDuty2()
.routingKey('LeafsNation')
`
PipelineTickTestHelper(t, pipe, want)
}

func TestAlertPushover(t *testing.T) {
pipe, _, from := StreamFrom()
handler := from.Alert().Pushover()
Expand Down

0 comments on commit 25662ac

Please sign in to comment.