From 5a070fed0eaaaa2463b0a8203d2bb025fa7cfdd7 Mon Sep 17 00:00:00 2001 From: Iain Sproat <68657+iainsproat@users.noreply.github.com> Date: Wed, 16 Nov 2022 10:03:29 +0000 Subject: [PATCH] Attributes are now included in Discord embed field --- pkg/alertforwarder/alertforwarder_test.go | 9 ++++----- pkg/alertforwarder/translator.go | 21 ++++++++++----------- pkg/alertmanager/types.go | 5 +---- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/pkg/alertforwarder/alertforwarder_test.go b/pkg/alertforwarder/alertforwarder_test.go index 38702f1..ca04642 100644 --- a/pkg/alertforwarder/alertforwarder_test.go +++ b/pkg/alertforwarder/alertforwarder_test.go @@ -227,14 +227,13 @@ func Test_TransformAndForward_StatusResolved_HappyPath(t *testing.T) { } // alert with a label 'instance'='localhost' and 'exported_instance' label is set, should have the instance replaced by 'exported_instance' -func Test_TransformAndForward_ExportedInstance_HappyPath(t *testing.T) { +func Test_TransformAndForward_Annotations_AreAddedAsEmbedFields_HappyPath(t *testing.T) { ao := alertmanager.Out{ Alerts: []alertmanager.Alert{ { Status: alertmanager.StatusFiring, - Labels: map[string]string{ - "instance": "localhost", - "exported_instance": "exported_instance_value", + Annotations: map[string]string{ + "environment": "development", }, }, }, @@ -252,7 +251,7 @@ func Test_TransformAndForward_ExportedInstance_HappyPath(t *testing.T) { assert.Equal(t, 1, len(do.Embeds), "Discord message embed length") assert.Equal(t, 10038562, do.Embeds[0].Color, "Discord message embed color") assert.Equal(t, 1, len(do.Embeds[0].Fields), "Discord message embed fields length") - assert.Contains(t, do.Embeds[0].Fields[0].Name, "exported_instance_value", "Discord message embed field Name should contain instance") + assert.Contains(t, do.Embeds[0].Fields[0].Value, "development", "Discord message embed field Name should contain attribute") assert.Equal(t, "", do.Content, "Discord message content") } diff --git a/pkg/alertforwarder/translator.go b/pkg/alertforwarder/translator.go index e6bbf4a..520d9f5 100644 --- a/pkg/alertforwarder/translator.go +++ b/pkg/alertforwarder/translator.go @@ -11,8 +11,12 @@ import ( func TranslateAlertManagerToDiscord(status string, amo *alertmanager.Out, alerts []alertmanager.Alert) discord.Out { DO := discord.Out{} + if amo.CommonAnnotations.Summary != "" { + DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary) + } + RichEmbed := discord.Embed{ - Title: fmt.Sprintf("[%s:%d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname), + Title: fmt.Sprintf("[%s: %d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname), Description: amo.CommonAnnotations.Summary, Color: discord.ColorGrey, Fields: []discord.EmbedField{}, @@ -25,19 +29,14 @@ func TranslateAlertManagerToDiscord(status string, amo *alertmanager.Out, alerts RichEmbed.Color = discord.ColorGreen } - if amo.CommonAnnotations.Summary != "" { - DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary) - } - for _, alert := range alerts { - realname := alert.Labels["instance"] - if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" { - realname = alert.Labels["exported_instance"] + var annotations strings.Builder + for key, val := range alert.Annotations { + annotations.WriteString(fmt.Sprintf("%s: %s\n", key, val)) } - RichEmbed.Fields = append(RichEmbed.Fields, discord.EmbedField{ - Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname), - Value: alert.Annotations.Description, + Name: "Alert details", + Value: annotations.String(), }) } diff --git a/pkg/alertmanager/types.go b/pkg/alertmanager/types.go index 05523e9..1845046 100644 --- a/pkg/alertmanager/types.go +++ b/pkg/alertmanager/types.go @@ -6,10 +6,7 @@ const ( ) type Alert struct { - Annotations struct { - Description string `json:"description"` - Summary string `json:"summary"` - } `json:"annotations"` + Annotations map[string]string `json:"annotations"` EndsAt string `json:"endsAt"` GeneratorURL string `json:"generatorURL"` Labels map[string]string `json:"labels"`