From e5aa34b2e36f9b6a948173bd4662218c02182723 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 1 Oct 2022 16:05:51 +0300 Subject: [PATCH] lib/promrelabel: export MustParseMetricWithLabels function, which can be used for simplifying tests --- lib/promrelabel/if_expression_test.go | 51 ++------------------------- lib/promrelabel/relabel_test.go | 20 +++-------- lib/promrelabel/util.go | 50 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 65 deletions(-) create mode 100644 lib/promrelabel/util.go diff --git a/lib/promrelabel/if_expression_test.go b/lib/promrelabel/if_expression_test.go index 18b654f4c5..900c1da955 100644 --- a/lib/promrelabel/if_expression_test.go +++ b/lib/promrelabel/if_expression_test.go @@ -3,12 +3,8 @@ package promrelabel import ( "bytes" "encoding/json" - "fmt" - "strings" "testing" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus" "gopkg.in/yaml.v2" ) @@ -125,10 +121,7 @@ func TestIfExpressionMatch(t *testing.T) { if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil { t.Fatalf("unexpected error during unmarshal: %s", err) } - labels, err := parseMetricWithLabels(metricWithLabels) - if err != nil { - t.Fatalf("cannot parse %s: %s", metricWithLabels, err) - } + labels := MustParseMetricWithLabels(metricWithLabels) if !ie.Match(labels) { t.Fatalf("unexpected mismatch of ifExpr=%s for %s", ifExpr, metricWithLabels) } @@ -162,10 +155,7 @@ func TestIfExpressionMismatch(t *testing.T) { if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil { t.Fatalf("unexpected error during unmarshal: %s", err) } - labels, err := parseMetricWithLabels(metricWithLabels) - if err != nil { - t.Fatalf("cannot parse %s: %s", metricWithLabels, err) - } + labels := MustParseMetricWithLabels(metricWithLabels) if ie.Match(labels) { t.Fatalf("unexpected match of ifExpr=%s for %s", ifExpr, metricWithLabels) } @@ -187,40 +177,3 @@ func TestIfExpressionMismatch(t *testing.T) { f(`'{foo!~"bar|"}'`, `abc`) f(`'{foo!~"bar|"}'`, `abc{foo="bar"}`) } - -func parseMetricWithLabels(metricWithLabels string) ([]prompbmarshal.Label, error) { - stripDummyMetric := false - if strings.HasPrefix(metricWithLabels, "{") { - // Add a dummy metric name, since the parser needs it - metricWithLabels = "dummy_metric" + metricWithLabels - stripDummyMetric = true - } - // add a value to metricWithLabels, so it could be parsed by prometheus protocol parser. - s := metricWithLabels + " 123" - var rows prometheus.Rows - var err error - rows.UnmarshalWithErrLogger(s, func(s string) { - err = fmt.Errorf("error during metric parse: %s", s) - }) - if err != nil { - return nil, err - } - if len(rows.Rows) != 1 { - return nil, fmt.Errorf("unexpected number of rows parsed; got %d; want 1", len(rows.Rows)) - } - r := rows.Rows[0] - var lfs []prompbmarshal.Label - if !stripDummyMetric { - lfs = append(lfs, prompbmarshal.Label{ - Name: "__name__", - Value: r.Metric, - }) - } - for _, tag := range r.Tags { - lfs = append(lfs, prompbmarshal.Label{ - Name: tag.Key, - Value: tag.Value, - }) - } - return lfs, nil -} diff --git a/lib/promrelabel/relabel_test.go b/lib/promrelabel/relabel_test.go index f799e67770..40dd24fd65 100644 --- a/lib/promrelabel/relabel_test.go +++ b/lib/promrelabel/relabel_test.go @@ -77,10 +77,7 @@ func TestApplyRelabelConfigs(t *testing.T) { if err != nil { t.Fatalf("cannot parse %q: %s", config, err) } - labels, err := parseMetricWithLabels(metric) - if err != nil { - t.Fatalf("cannot parse %s: %s", metric, err) - } + labels := MustParseMetricWithLabels(metric) resultLabels := pcs.Apply(labels, 0, isFinalize) result := labelsToString(resultLabels) if result != resultExpected { @@ -696,10 +693,7 @@ func TestApplyRelabelConfigs(t *testing.T) { func TestFinalizeLabels(t *testing.T) { f := func(metric, resultExpected string) { t.Helper() - labels, err := parseMetricWithLabels(metric) - if err != nil { - t.Fatalf("cannot parse %s: %s", metric, err) - } + labels := MustParseMetricWithLabels(metric) resultLabels := FinalizeLabels(nil, labels) result := labelsToString(resultLabels) if result != resultExpected { @@ -715,10 +709,7 @@ func TestFinalizeLabels(t *testing.T) { func TestRemoveMetaLabels(t *testing.T) { f := func(metric, resultExpected string) { t.Helper() - labels, err := parseMetricWithLabels(metric) - if err != nil { - t.Fatalf("cannot parse %s: %s", metric, err) - } + labels := MustParseMetricWithLabels(metric) resultLabels := RemoveMetaLabels(nil, labels) result := labelsToString(resultLabels) if result != resultExpected { @@ -734,10 +725,7 @@ func TestRemoveMetaLabels(t *testing.T) { func TestFillLabelReferences(t *testing.T) { f := func(replacement, metric, resultExpected string) { t.Helper() - labels, err := parseMetricWithLabels(metric) - if err != nil { - t.Fatalf("cannot parse %s: %s", metric, err) - } + labels := MustParseMetricWithLabels(metric) result := fillLabelReferences(nil, replacement, labels) if string(result) != resultExpected { t.Fatalf("unexpected result; got\n%q\nwant\n%q", result, resultExpected) diff --git a/lib/promrelabel/util.go b/lib/promrelabel/util.go new file mode 100644 index 0000000000..b93fe4c93d --- /dev/null +++ b/lib/promrelabel/util.go @@ -0,0 +1,50 @@ +package promrelabel + +import ( + "fmt" + "strings" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus" +) + +// MustParseMetricWithLabels parses s, which can have the form `metric{labels}`. +// +// This function is indended mostly for tests. +func MustParseMetricWithLabels(metricWithLabels string) []prompbmarshal.Label { + stripDummyMetric := false + if strings.HasPrefix(metricWithLabels, "{") { + // Add a dummy metric name, since the parser needs it + metricWithLabels = "dummy_metric" + metricWithLabels + stripDummyMetric = true + } + // add a value to metricWithLabels, so it could be parsed by prometheus protocol parser. + s := metricWithLabels + " 123" + var rows prometheus.Rows + var err error + rows.UnmarshalWithErrLogger(s, func(s string) { + err = fmt.Errorf("error during metric parse: %s", s) + }) + if err != nil { + logger.Panicf("BUG: cannot parse %q: %s", metricWithLabels, err) + } + if len(rows.Rows) != 1 { + logger.Panicf("BUG: unexpected number of rows parsed; got %d; want 1", len(rows.Rows)) + } + r := rows.Rows[0] + var lfs []prompbmarshal.Label + if !stripDummyMetric { + lfs = append(lfs, prompbmarshal.Label{ + Name: "__name__", + Value: r.Metric, + }) + } + for _, tag := range r.Tags { + lfs = append(lfs, prompbmarshal.Label{ + Name: tag.Key, + Value: tag.Value, + }) + } + return lfs +}