lib/promrelabel: export MustParseMetricWithLabels function, which can be used for simplifying tests

This commit is contained in:
Aliaksandr Valialkin 2022-10-01 16:05:51 +03:00
parent d81285c814
commit e5aa34b2e3
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 56 additions and 65 deletions

View File

@ -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
}

View File

@ -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)

50
lib/promrelabel/util.go Normal file
View File

@ -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
}