From d3a590ca193bc25b394f4f40ecac0a552b531a34 Mon Sep 17 00:00:00 2001 From: Levi Harrison <54278938+LeviHarrison@users.noreply.github.com> Date: Tue, 2 Mar 2021 04:37:35 -0500 Subject: [PATCH] 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 * Changed name of empty metric in Discord alert Signed-off-by: Levi Harrison --- pkg/services/alerting/notifiers/discord.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/services/alerting/notifiers/discord.go b/pkg/services/alerting/notifiers/discord.go index 5bbc5039b93..7c8f504ebe6 100644 --- a/pkg/services/alerting/notifiers/discord.go +++ b/pkg/services/alerting/notifiers/discord.go @@ -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 "" + } + + return metric +}