From e7d353ee6a86299afe4ad2f5783fdf89eca781df Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 4 Jun 2021 20:41:50 +0300 Subject: [PATCH] lib/promrelabel: add tests for labelsToString() function --- lib/promrelabel/relabel.go | 2 +- lib/promrelabel/relabel_test.go | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index 60abc845c..fa8875986 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -446,7 +446,7 @@ func labelsToString(labels []prompbmarshal.Label) string { break } } - if len(labelsCopy) <= 1 { + if mname != "" && len(labelsCopy) <= 1 { return mname } b := []byte(mname) diff --git a/lib/promrelabel/relabel_test.go b/lib/promrelabel/relabel_test.go index 1eb5d67e4..a77e40abe 100644 --- a/lib/promrelabel/relabel_test.go +++ b/lib/promrelabel/relabel_test.go @@ -7,6 +7,53 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" ) +func TestLabelsToString(t *testing.T) { + f := func(labels []prompbmarshal.Label, sExpected string) { + t.Helper() + s := labelsToString(labels) + if s != sExpected { + t.Fatalf("unexpected result;\ngot\n%s\nwant\n%s", s, sExpected) + } + } + f(nil, "{}") + f([]prompbmarshal.Label{ + { + Name: "__name__", + Value: "foo", + }, + }, "foo") + f([]prompbmarshal.Label{ + { + Name: "foo", + Value: "bar", + }, + }, `{foo="bar"}`) + f([]prompbmarshal.Label{ + { + Name: "foo", + Value: "bar", + }, + { + Name: "a", + Value: "bc", + }, + }, `{a="bc",foo="bar"}`) + f([]prompbmarshal.Label{ + { + Name: "foo", + Value: "bar", + }, + { + Name: "__name__", + Value: "xxx", + }, + { + Name: "a", + Value: "bc", + }, + }, `xxx{a="bc",foo="bar"}`) +} + func TestApplyRelabelConfigs(t *testing.T) { f := func(config string, labels []prompbmarshal.Label, isFinalize bool, resultExpected []prompbmarshal.Label) { t.Helper()