From f0099c4db775a105d194db243a7d3c4df3d81442 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Fri, 8 Dec 2023 14:53:35 +0100 Subject: [PATCH] app/vmalert: sanitize label names before sending to Alertmanager (#5442) Before, vmalert would send notifications with labels containing characters not supported by Alertmanager validator, resulting into validation errors like `msg="Failed to validate alerts" err="invalid label set: invalid name "foo.bar"` Signed-off-by: hagen1778 --- app/vmalert/notifier/alert.go | 2 +- app/vmalert/notifier/alert_test.go | 5 +++++ docs/CHANGELOG.md | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/vmalert/notifier/alert.go b/app/vmalert/notifier/alert.go index 35a68c54c..91839f98c 100644 --- a/app/vmalert/notifier/alert.go +++ b/app/vmalert/notifier/alert.go @@ -191,7 +191,7 @@ func (a Alert) toPromLabels(relabelCfg *promrelabel.ParsedConfigs) []prompbmarsh var labels []prompbmarshal.Label for k, v := range a.Labels { labels = append(labels, prompbmarshal.Label{ - Name: k, + Name: promrelabel.SanitizeMetricName(k), Value: v, }) } diff --git a/app/vmalert/notifier/alert_test.go b/app/vmalert/notifier/alert_test.go index 9999f6f42..fe9affaf7 100644 --- a/app/vmalert/notifier/alert_test.go +++ b/app/vmalert/notifier/alert_test.go @@ -234,6 +234,11 @@ func TestAlert_toPromLabels(t *testing.T) { []prompbmarshal.Label{{Name: "a", Value: "baz"}, {Name: "foo", Value: "bar"}}, nil, ) + fn( + map[string]string{"foo.bar": "baz", "service!name": "qux"}, + []prompbmarshal.Label{{Name: "foo_bar", Value: "baz"}, {Name: "service_name", Value: "qux"}}, + nil, + ) pcs, err := promrelabel.ParseRelabelConfigsData([]byte(` - target_label: "foo" diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c76b99a90..2048bc59a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -18,6 +18,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * SECURITY: upgrade base docker image (Alpine) from 3.18.4 to 3.18.5. See [alpine 3.18.5 release notes](https://www.alpinelinux.org/posts/Alpine-3.15.11-3.16.8-3.17.6-3.18.5-released.html). * SECURITY: upgrade Go builder from Go1.21.4 to Go1.21.5. See [the list of issues addressed in Go1.21.5](https://github.com/golang/go/issues?q=milestone%3AGo1.21.5+label%3ACherryPickApproved). +* BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert.html): sanitize label names before sending the alert notification to Alertmanager. Before, vmalert would send notifications with labels containing characters not supported by Alertmanager validator, resulting into validation errors like `msg="Failed to validate alerts" err="invalid label set: invalid name "foo.bar"`. * BUGFIX: properly escape `<` character in responses returned via [`/federate`](https://docs.victoriametrics.com/#federation) endpoint. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5431). ## [v1.87.11](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.87.11)