From 584f0c01447db25958c007477313b34c88a5b0fb Mon Sep 17 00:00:00 2001 From: Marko Posavec Date: Wed, 25 Aug 2021 10:34:32 +0200 Subject: [PATCH 1/2] added option to specify multiple teams in opsgenie configuration Signed-off-by: Marko Posavec --- docs/configuration.md | 2 +- notify/opsgenie/opsgenie.go | 12 +++++++++++ notify/opsgenie/opsgenie_test.go | 36 ++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ec372308ea..2094b01f4c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -855,7 +855,7 @@ responders: [ name: ] [ username: ] -# "team", "user", "escalation" or "schedule". +# "team", "teams", "user", "escalation" or "schedule". type: ``` diff --git a/notify/opsgenie/opsgenie.go b/notify/opsgenie/opsgenie.go index 190ce2752b..96d72cce88 100644 --- a/notify/opsgenie/opsgenie.go +++ b/notify/opsgenie/opsgenie.go @@ -166,6 +166,18 @@ func (n *Notifier) createRequest(ctx context.Context, as ...*types.Alert) (*http continue } + if responder.Type == "teams" { + teams := safeSplit(responder.Name, ",") + for _, team := range teams { + newResponder := opsGenieCreateMessageResponder{ + Name: tmpl(team), + Type: tmpl("team"), + } + responders = append(responders, newResponder) + } + continue + } + responders = append(responders, responder) } diff --git a/notify/opsgenie/opsgenie_test.go b/notify/opsgenie/opsgenie_test.go index b2a96b1280..755840e40a 100644 --- a/notify/opsgenie/opsgenie_test.go +++ b/notify/opsgenie/opsgenie_test.go @@ -112,7 +112,7 @@ func TestOpsGenie(t *testing.T) { }, expectedEmptyAlertBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"","details":{},"source":""} `, - expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Description":"description","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderType1":"team","ResponderType2":"escalation","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"EscalationA","type":"escalation"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} + expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Description":"description","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderName3":"TeamA,TeamB","ResponderType1":"team","ResponderType2":"escalation","ResponderType3":"teams","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"EscalationA","type":"escalation"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} `, }, { @@ -146,7 +146,37 @@ func TestOpsGenie(t *testing.T) { }, expectedEmptyAlertBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"","details":{"Description":"adjusted "},"source":""} `, - expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Description":"adjusted description","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderType1":"team","ResponderType2":"escalation","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"EscalationA","type":"escalation"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} + expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Description":"adjusted description","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderName3":"TeamA,TeamB","ResponderType1":"team","ResponderType2":"escalation","ResponderType3":"teams","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"EscalationA","type":"escalation"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} +`, + }, + { + title: "config with multiple teams", + cfg: &config.OpsGenieConfig{ + NotifierConfig: config.NotifierConfig{ + VSendResolved: true, + }, + Message: `{{ .CommonLabels.Message }}`, + Description: `{{ .CommonLabels.Description }}`, + Source: `{{ .CommonLabels.Source }}`, + Details: map[string]string{ + "Description": `adjusted {{ .CommonLabels.Description }}`, + }, + Responders: []config.OpsGenieConfigResponder{ + { + Name: `{{ .CommonLabels.ResponderName3 }}`, + Type: `{{ .CommonLabels.ResponderType3 }}`, + }, + }, + Tags: `{{ .CommonLabels.Tags }}`, + Note: `{{ .CommonLabels.Note }}`, + Priority: `{{ .CommonLabels.Priority }}`, + APIKey: `{{ .ExternalURL }}`, + APIURL: &config.URL{URL: u}, + HTTPConfig: &commoncfg.HTTPClientConfig{}, + }, + expectedEmptyAlertBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"","details":{"Description":"adjusted "},"source":""} +`, + expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Description":"adjusted description","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderName3":"TeamA,TeamB","ResponderType1":"team","ResponderType2":"escalation","ResponderType3":"teams","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"TeamB","type":"team"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} `, }, } { @@ -185,6 +215,8 @@ func TestOpsGenie(t *testing.T) { "ResponderType1": "team", "ResponderName2": "EscalationA", "ResponderType2": "escalation", + "ResponderName3": "TeamA,TeamB", + "ResponderType3": "teams", "Tags": "tag1,tag2", "Note": "this is a note", "Priority": "P1", From e0f5e3c1404ca5da9fe8642526ea120261dfe23c Mon Sep 17 00:00:00 2001 From: Marko Posavec Date: Wed, 25 Aug 2021 11:21:55 +0200 Subject: [PATCH 2/2] Added teams to configuration Signed-off-by: Marko Posavec --- config/notifiers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/notifiers.go b/config/notifiers.go index 98eeddb4f3..4c5cdcf6d1 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -467,7 +467,7 @@ type OpsGenieConfig struct { Priority string `yaml:"priority,omitempty" json:"priority,omitempty"` } -const opsgenieValidTypesRe = `^(team|user|escalation|schedule)$` +const opsgenieValidTypesRe = `^(team|teams|user|escalation|schedule)$` var opsgenieTypeMatcher = regexp.MustCompile(opsgenieValidTypesRe)