Alerting: Fix bug in Discord for when name for metric value is absent (#31257)

* Make sure Metric field in Discord notification is never empty

Discord uniquely does not send the alert if the metric field is empty, which can happen in some cases, such as when the legend is {{hostname}}.

Signed-off-by: Levi Harrison <levisamuelharrison@gmail.com>

* Changed name of empty metric in Discord alert

Signed-off-by: Levi Harrison <levisamuelharrison@gmail.com>
This commit is contained in:
Levi Harrison
2021-03-02 11:37:35 +02:00
committed by GitHub
parent 4bc6a7c407
commit d3a590ca19
+11 -1
View File
@@ -89,7 +89,9 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
for _, evt := range evalContext.EvalMatches {
fields = append(fields, map[string]interface{}{
"name": evt.Metric,
// Discord uniquely does not send the alert if the metric field is empty,
// which it can be in some cases
"name": notEmpty(evt.Metric),
"value": evt.Value.FullString(),
"inline": true,
})
@@ -213,3 +215,11 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str
return nil
}
func notEmpty(metric string) string {
if metric == "" {
return "<NO_METRIC_NAME>"
}
return metric
}