Skip to content

Commit d4bf5f8

Browse files
committed
fix: add error log
1 parent a93a8a4 commit d4bf5f8

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

discord/discord.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"github.com/codingpot/alertmanager-discord/alertman"
8-
"github.com/sirupsen/logrus"
8+
log "github.com/sirupsen/logrus"
99
"net/http"
1010
"strings"
1111
)
@@ -42,7 +42,7 @@ func SendWebhook(amo *alertman.AlertManOut, discordWebhookURL string) {
4242
}
4343

4444
for status, alerts := range groupedAlerts {
45-
DO := DiscordOut{}
45+
var discordOut DiscordOut
4646

4747
RichEmbed := DiscordEmbed{
4848
Title: fmt.Sprintf("[%s:%d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname),
@@ -58,25 +58,29 @@ func SendWebhook(amo *alertman.AlertManOut, discordWebhookURL string) {
5858
}
5959

6060
if amo.CommonAnnotations.Summary != "" {
61-
DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary)
61+
discordOut.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary)
6262
}
6363

6464
for _, alert := range alerts {
65-
realname := alert.Labels["instance"]
66-
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
67-
realname = alert.Labels["exported_instance"]
65+
realName := alert.Labels["instance"]
66+
if strings.Contains(realName, "localhost") && alert.Labels["exported_instance"] != "" {
67+
realName = alert.Labels["exported_instance"]
6868
}
6969

7070
RichEmbed.Fields = append(RichEmbed.Fields, DiscordEmbedField{
71-
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname),
71+
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realName),
7272
Value: alert.Annotations.Description,
7373
})
7474
}
7575

76-
DO.Embeds = []DiscordEmbed{RichEmbed}
76+
discordOut.Embeds = []DiscordEmbed{RichEmbed}
77+
DOD, _ := json.Marshal(discordOut)
7778

78-
DOD, _ := json.Marshal(DO)
79-
http.Post(discordWebhookURL, "application/json", bytes.NewReader(DOD))
79+
_, err := http.Post(discordWebhookURL, "application/json", bytes.NewReader(DOD))
80+
if err != nil {
81+
log.WithError(err).Error("failed to write to webhook")
82+
return
83+
}
8084
}
8185
}
8286

@@ -87,11 +91,11 @@ func SendRawPromAlertWarn(discordWebhookURL string) {
8791
`for guidance on how to configure it for alertmanager` + "\n" +
8892
`or https://prometheus.io/docs/alerting/latest/configuration/#webhook_config`
8993

90-
logrus.Print(`/!\ -- You have misconfigured this software -- /!\`)
91-
logrus.Print(`--- -- -- ---`)
92-
logrus.Print(badString)
94+
log.Println(`/!\ -- You have misconfigured this software -- /!\`)
95+
log.Println(`--- -- -- ---`)
96+
log.Println(badString)
9397

94-
DO := DiscordOut{
98+
discordOutBytes, _ := json.Marshal(DiscordOut{
9599
Content: "",
96100
Embeds: []DiscordEmbed{
97101
{
@@ -101,12 +105,10 @@ func SendRawPromAlertWarn(discordWebhookURL string) {
101105
Fields: []DiscordEmbedField{},
102106
},
103107
},
104-
}
105-
106-
DOD, _ := json.Marshal(DO)
107-
_, err := http.Post(discordWebhookURL, "application/json", bytes.NewReader(DOD))
108+
})
109+
_, err := http.Post(discordWebhookURL, "application/json", bytes.NewReader(discordOutBytes))
108110
if err != nil {
109-
logrus.Printf("failed to sned the alert")
111+
log.Printf("failed to sned the alert")
110112
return
111113
}
112114
}

0 commit comments

Comments
 (0)