From 998d89685f0dfaf714854adfc8c6c199c7a808e2 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Tue, 20 Dec 2022 23:32:45 +0100 Subject: [PATCH] vmagent: respect `-usePromCompatibleNaming` if no relabeling is set (#3511) * vmagent: respect `-usePromCompatibleNaming` if no relabeling is set See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3493 Signed-off-by: hagen1778 * vmagent: upd test Signed-off-by: hagen1778 Signed-off-by: hagen1778 Co-authored-by: Aliaksandr Valialkin --- app/vmagent/remotewrite/relabel.go | 2 +- app/vmagent/remotewrite/relabel_test.go | 49 +++++++++++++++++++++++++ docs/CHANGELOG.md | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/vmagent/remotewrite/relabel_test.go diff --git a/app/vmagent/remotewrite/relabel.go b/app/vmagent/remotewrite/relabel.go index 1b796e24c..44147439c 100644 --- a/app/vmagent/remotewrite/relabel.go +++ b/app/vmagent/remotewrite/relabel.go @@ -86,7 +86,7 @@ func initLabelsGlobal() { } func (rctx *relabelCtx) applyRelabeling(tss []prompbmarshal.TimeSeries, extraLabels []prompbmarshal.Label, pcs *promrelabel.ParsedConfigs) []prompbmarshal.TimeSeries { - if len(extraLabels) == 0 && pcs.Len() == 0 { + if len(extraLabels) == 0 && pcs.Len() == 0 && !*usePromCompatibleNaming { // Nothing to change. return tss } diff --git a/app/vmagent/remotewrite/relabel_test.go b/app/vmagent/remotewrite/relabel_test.go new file mode 100644 index 000000000..eb3540002 --- /dev/null +++ b/app/vmagent/remotewrite/relabel_test.go @@ -0,0 +1,49 @@ +package remotewrite + +import ( + "reflect" + "testing" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils" +) + +func TestApplyRelabeling(t *testing.T) { + f := func(extraLabels []prompbmarshal.Label, pcs *promrelabel.ParsedConfigs, sTss, sExpTss string) { + rctx := &relabelCtx{} + tss, expTss := parseSeries(sTss), parseSeries(sExpTss) + gotTss := rctx.applyRelabeling(tss, extraLabels, pcs) + if !reflect.DeepEqual(gotTss, expTss) { + t.Fatalf("expected to have: \n%v;\ngot: \n%v", expTss, gotTss) + } + } + + f(nil, nil, "up", "up") + f([]prompbmarshal.Label{{Name: "foo", Value: "bar"}}, nil, "up", `up{foo="bar"}`) + f([]prompbmarshal.Label{{Name: "foo", Value: "bar"}}, nil, `up{foo="baz"}`, `up{foo="bar"}`) + + pcs, err := promrelabel.ParseRelabelConfigsData([]byte(` +- target_label: "foo" + replacement: "aaa" +- action: labeldrop + regex: "env.*" +`)) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + f(nil, pcs, `up{foo="baz", env="prod"}`, `up{foo="aaa"}`) + + oldVal := *usePromCompatibleNaming + *usePromCompatibleNaming = true + f(nil, nil, `foo.bar`, `foo_bar`) + *usePromCompatibleNaming = oldVal +} + +func parseSeries(data string) []prompbmarshal.TimeSeries { + var tss []prompbmarshal.TimeSeries + tss = append(tss, prompbmarshal.TimeSeries{ + Labels: promutils.MustNewLabelsFromString(data).GetLabels(), + }) + return tss +} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c37125df6..69722f387 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -20,6 +20,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * BUGFIX: fix `error when searching for TSIDs by metricIDs in the previous indexdb: EOF` error, which can occur during queries after unclean shutdown of VictoriaMetrics (e.g. via hardware reset, out of memory crash or `kill -9`). The error has been introduced in [v1.85.2](https://docs.victoriametrics.com/CHANGELOG.html#v1852). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3515). * BUGFIX: [VictoriaMetrics enterprise](https://docs.victoriametrics.com/enterprise.html): expose proper values for `vm_downsampling_partitions_scheduled` and `vm_downsampling_partitions_scheduled_size_bytes` metrics, which were added at [v1.78.0](https://docs.victoriametrics.com/CHANGELOG.html#v1780). See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2612). * BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): never extend explicitly set lookbehind window for [rate()](https://docs.victoriametrics.com/MetricsQL.html#rate) function. This reduces the level of confusion when the user expects the needed results after explicitly seting the lookbehind window `[d]` in the query `rate(m[d])`. Previously VictoriaMetrics could silently extend the lookbehind window, so it covers at least two raw samples. Now this behavior works only if the lookbehind window in square brackets isn't set explicitly, e.g. in the case of `rate(m)`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3483) for details. +* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): respect `-usePromCompatibleNaming` flag if no relabeling or extra labels were set. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3511) for details. * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix the wrong legend when queries are hidden. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3512).