diff --git a/config/notifiers.go b/config/notifiers.go index f7f627c8..ba4517e8 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -42,6 +42,14 @@ var ( // DefaultEmailSubject defines the default Subject header of an Email. DefaultEmailSubject = `{{ template "email.default.subject" . }}` + // DefaultPagerdutyDetails defines the default values for PagerDuty details. + DefaultPagerdutyDetails = map[string]string{ + "firing": `{{ template "pagerduty.default.instances" .Alerts.Firing }}`, + "resolved": `{{ template "pagerduty.default.instances" .Alerts.Resolved }}`, + "num_firing": `{{ .Alerts.Firing | len }}`, + "num_resolved": `{{ .Alerts.Resolved | len }}`, + } + // DefaultPagerdutyConfig defines default values for PagerDuty configurations. DefaultPagerdutyConfig = PagerdutyConfig{ NotifierConfig: NotifierConfig{ @@ -50,12 +58,6 @@ var ( Description: `{{ template "pagerduty.default.description" .}}`, Client: `{{ template "pagerduty.default.client" . }}`, ClientURL: `{{ template "pagerduty.default.clientURL" . }}`, - Details: map[string]string{ - "firing": `{{ template "pagerduty.default.instances" .Alerts.Firing }}`, - "resolved": `{{ template "pagerduty.default.instances" .Alerts.Resolved }}`, - "num_firing": `{{ .Alerts.Firing | len }}`, - "num_resolved": `{{ .Alerts.Resolved | len }}`, - }, } // DefaultSlackConfig defines default values for Slack configurations. @@ -217,6 +219,14 @@ func (c *PagerdutyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error if c.RoutingKey == "" && c.ServiceKey == "" { return fmt.Errorf("missing service or routing key in PagerDuty config") } + if c.Details == nil { + c.Details = make(map[string]string) + } + for k, v := range DefaultPagerdutyDetails { + if _, ok := c.Details[k]; !ok { + c.Details[k] = v + } + } return nil } diff --git a/config/notifiers_test.go b/config/notifiers_test.go index 859337ca..c3f897f8 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -91,6 +91,63 @@ service_key: '' } } +func TestPagerdutyDetails(t *testing.T) { + + var tests = []struct { + in string + checkFn func(map[string]string) + }{ + { + in: ` +routing_key: 'xyz' +`, + checkFn: func(d map[string]string) { + if len(d) != 4 { + t.Errorf("expected 4 items, got: %d", len(d)) + } + }, + }, + { + in: ` +routing_key: 'xyz' +details: + key1: val1 +`, + checkFn: func(d map[string]string) { + if len(d) != 5 { + t.Errorf("expected 5 items, got: %d", len(d)) + } + }, + }, + { + in: ` +routing_key: 'xyz' +details: + key1: val1 + key2: val2 + firing: firing +`, + checkFn: func(d map[string]string) { + if len(d) != 6 { + t.Errorf("expected 6 items, got: %d", len(d)) + } + }, + }, + } + for _, tc := range tests { + var cfg PagerdutyConfig + err := yaml.UnmarshalStrict([]byte(tc.in), &cfg) + + if err != nil { + t.Errorf("expected no error, got:%v", err) + } + + if tc.checkFn != nil { + tc.checkFn(cfg.Details) + } + } +} + func TestHipchatRoomIDIsPresent(t *testing.T) { in := ` room_id: ''