diff --git a/alerting/provider/email/email.go b/alerting/provider/email/email.go index 79bece3f..01dd3fd4 100644 --- a/alerting/provider/email/email.go +++ b/alerting/provider/email/email.go @@ -166,7 +166,14 @@ func (provider *AlertProvider) buildMessageSubjectAndBody(ep *endpoint.Endpoint, if alertDescription := alert.GetDescription(); len(alertDescription) > 0 { description = "\n\nAlert description: " + alertDescription } - return subject, message + description + formattedConditionResults + var extraLabels string + if len(ep.ExtraLabels) > 0 { + extraLabels = "\n\nExtra labels:\n" + for key, value := range ep.ExtraLabels { + extraLabels += fmt.Sprintf(" %s: %s\n", key, value) + } + } + return subject, message + description + extraLabels + formattedConditionResults } // GetDefaultAlert returns the provider's default alert configuration diff --git a/alerting/provider/email/email_test.go b/alerting/provider/email/email_test.go index 00e398aa..2e4c2aa6 100644 --- a/alerting/provider/email/email_test.go +++ b/alerting/provider/email/email_test.go @@ -76,6 +76,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider AlertProvider Alert alert.Alert Resolved bool + Endpoint *endpoint.Endpoint ExpectedSubject string ExpectedBody string }{ @@ -84,6 +85,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, + Endpoint: &endpoint.Endpoint{Name: "endpoint-name"}, ExpectedSubject: "[endpoint-name] Alert triggered", ExpectedBody: "An alert for endpoint-name has been triggered due to having failed 3 time(s) in a row\n\nAlert description: description-1\n\nCondition results:\n❌ [CONNECTED] == true\n❌ [STATUS] == 200\n", }, @@ -92,14 +94,42 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, + Endpoint: &endpoint.Endpoint{Name: "endpoint-name"}, ExpectedSubject: "[endpoint-name] Alert resolved", ExpectedBody: "An alert for endpoint-name has been resolved after passing successfully 5 time(s) in a row\n\nAlert description: description-2\n\nCondition results:\n✅ [CONNECTED] == true\n✅ [STATUS] == 200\n", }, + { + Name: "triggered-with-single-extra-label", + Provider: AlertProvider{}, + Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, + Resolved: false, + Endpoint: &endpoint.Endpoint{Name: "endpoint-name", ExtraLabels: map[string]string{"environment": "production"}}, + ExpectedSubject: "[endpoint-name] Alert triggered", + ExpectedBody: "An alert for endpoint-name has been triggered due to having failed 3 time(s) in a row\n\nAlert description: description-1\n\nExtra labels:\n environment: production\n\n\nCondition results:\n❌ [CONNECTED] == true\n❌ [STATUS] == 200\n", + }, + { + Name: "resolved-with-single-extra-label", + Provider: AlertProvider{}, + Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, + Resolved: true, + Endpoint: &endpoint.Endpoint{Name: "endpoint-name", ExtraLabels: map[string]string{"service": "api"}}, + ExpectedSubject: "[endpoint-name] Alert resolved", + ExpectedBody: "An alert for endpoint-name has been resolved after passing successfully 5 time(s) in a row\n\nAlert description: description-2\n\nExtra labels:\n service: api\n\n\nCondition results:\n✅ [CONNECTED] == true\n✅ [STATUS] == 200\n", + }, + { + Name: "triggered-with-no-extra-labels", + Provider: AlertProvider{}, + Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, + Resolved: false, + Endpoint: &endpoint.Endpoint{Name: "endpoint-name", ExtraLabels: map[string]string{}}, + ExpectedSubject: "[endpoint-name] Alert triggered", + ExpectedBody: "An alert for endpoint-name has been triggered due to having failed 3 time(s) in a row\n\nAlert description: description-1\n\nCondition results:\n❌ [CONNECTED] == true\n❌ [STATUS] == 200\n", + }, } for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) { subject, body := scenario.Provider.buildMessageSubjectAndBody( - &endpoint.Endpoint{Name: "endpoint-name"}, + scenario.Endpoint, &scenario.Alert, &endpoint.Result{ ConditionResults: []*endpoint.ConditionResult{