Attributes are now included in Discord embed field

This commit is contained in:
Iain Sproat
2022-11-16 10:03:29 +00:00
parent 5193686610
commit 5a070fed0e
3 changed files with 15 additions and 20 deletions
+4 -5
View File
@@ -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")
}
+10 -11
View File
@@ -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(),
})
}
+1 -4
View File
@@ -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"`