5
5
"encoding/json"
6
6
"fmt"
7
7
"github.com/codingpot/alertmanager-discord/alertman"
8
- "github.com/sirupsen/logrus"
8
+ log "github.com/sirupsen/logrus"
9
9
"net/http"
10
10
"strings"
11
11
)
@@ -42,7 +42,7 @@ func SendWebhook(amo *alertman.AlertManOut, discordWebhookURL string) {
42
42
}
43
43
44
44
for status , alerts := range groupedAlerts {
45
- DO := DiscordOut {}
45
+ var discordOut DiscordOut
46
46
47
47
RichEmbed := DiscordEmbed {
48
48
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) {
58
58
}
59
59
60
60
if amo .CommonAnnotations .Summary != "" {
61
- DO .Content = fmt .Sprintf (" === %s === \n " , amo .CommonAnnotations .Summary )
61
+ discordOut .Content = fmt .Sprintf (" === %s === \n " , amo .CommonAnnotations .Summary )
62
62
}
63
63
64
64
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" ]
68
68
}
69
69
70
70
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 ),
72
72
Value : alert .Annotations .Description ,
73
73
})
74
74
}
75
75
76
- DO .Embeds = []DiscordEmbed {RichEmbed }
76
+ discordOut .Embeds = []DiscordEmbed {RichEmbed }
77
+ DOD , _ := json .Marshal (discordOut )
77
78
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
+ }
80
84
}
81
85
}
82
86
@@ -87,11 +91,11 @@ func SendRawPromAlertWarn(discordWebhookURL string) {
87
91
`for guidance on how to configure it for alertmanager` + "\n " +
88
92
`or https://prometheus.io/docs/alerting/latest/configuration/#webhook_config`
89
93
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 )
93
97
94
- DO := DiscordOut {
98
+ discordOutBytes , _ := json . Marshal ( DiscordOut {
95
99
Content : "" ,
96
100
Embeds : []DiscordEmbed {
97
101
{
@@ -101,12 +105,10 @@ func SendRawPromAlertWarn(discordWebhookURL string) {
101
105
Fields : []DiscordEmbedField {},
102
106
},
103
107
},
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 ))
108
110
if err != nil {
109
- logrus .Printf ("failed to sned the alert" )
111
+ log .Printf ("failed to sned the alert" )
110
112
return
111
113
}
112
114
}
0 commit comments