mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 00:13:30 +01:00
lib/promrelabel: export MustParseMetricWithLabels function, which can be used for simplifying tests
This commit is contained in:
parent
d81285c814
commit
e5aa34b2e3
@ -3,12 +3,8 @@ package promrelabel
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -125,10 +121,7 @@ func TestIfExpressionMatch(t *testing.T) {
|
|||||||
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
||||||
t.Fatalf("unexpected error during unmarshal: %s", err)
|
t.Fatalf("unexpected error during unmarshal: %s", err)
|
||||||
}
|
}
|
||||||
labels, err := parseMetricWithLabels(metricWithLabels)
|
labels := MustParseMetricWithLabels(metricWithLabels)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metricWithLabels, err)
|
|
||||||
}
|
|
||||||
if !ie.Match(labels) {
|
if !ie.Match(labels) {
|
||||||
t.Fatalf("unexpected mismatch of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
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 {
|
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
||||||
t.Fatalf("unexpected error during unmarshal: %s", err)
|
t.Fatalf("unexpected error during unmarshal: %s", err)
|
||||||
}
|
}
|
||||||
labels, err := parseMetricWithLabels(metricWithLabels)
|
labels := MustParseMetricWithLabels(metricWithLabels)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metricWithLabels, err)
|
|
||||||
}
|
|
||||||
if ie.Match(labels) {
|
if ie.Match(labels) {
|
||||||
t.Fatalf("unexpected match of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
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`)
|
||||||
f(`'{foo!~"bar|"}'`, `abc{foo="bar"}`)
|
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
|
|
||||||
}
|
|
||||||
|
@ -77,10 +77,7 @@ func TestApplyRelabelConfigs(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot parse %q: %s", config, err)
|
t.Fatalf("cannot parse %q: %s", config, err)
|
||||||
}
|
}
|
||||||
labels, err := parseMetricWithLabels(metric)
|
labels := MustParseMetricWithLabels(metric)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metric, err)
|
|
||||||
}
|
|
||||||
resultLabels := pcs.Apply(labels, 0, isFinalize)
|
resultLabels := pcs.Apply(labels, 0, isFinalize)
|
||||||
result := labelsToString(resultLabels)
|
result := labelsToString(resultLabels)
|
||||||
if result != resultExpected {
|
if result != resultExpected {
|
||||||
@ -696,10 +693,7 @@ func TestApplyRelabelConfigs(t *testing.T) {
|
|||||||
func TestFinalizeLabels(t *testing.T) {
|
func TestFinalizeLabels(t *testing.T) {
|
||||||
f := func(metric, resultExpected string) {
|
f := func(metric, resultExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
labels, err := parseMetricWithLabels(metric)
|
labels := MustParseMetricWithLabels(metric)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metric, err)
|
|
||||||
}
|
|
||||||
resultLabels := FinalizeLabels(nil, labels)
|
resultLabels := FinalizeLabels(nil, labels)
|
||||||
result := labelsToString(resultLabels)
|
result := labelsToString(resultLabels)
|
||||||
if result != resultExpected {
|
if result != resultExpected {
|
||||||
@ -715,10 +709,7 @@ func TestFinalizeLabels(t *testing.T) {
|
|||||||
func TestRemoveMetaLabels(t *testing.T) {
|
func TestRemoveMetaLabels(t *testing.T) {
|
||||||
f := func(metric, resultExpected string) {
|
f := func(metric, resultExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
labels, err := parseMetricWithLabels(metric)
|
labels := MustParseMetricWithLabels(metric)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metric, err)
|
|
||||||
}
|
|
||||||
resultLabels := RemoveMetaLabels(nil, labels)
|
resultLabels := RemoveMetaLabels(nil, labels)
|
||||||
result := labelsToString(resultLabels)
|
result := labelsToString(resultLabels)
|
||||||
if result != resultExpected {
|
if result != resultExpected {
|
||||||
@ -734,10 +725,7 @@ func TestRemoveMetaLabels(t *testing.T) {
|
|||||||
func TestFillLabelReferences(t *testing.T) {
|
func TestFillLabelReferences(t *testing.T) {
|
||||||
f := func(replacement, metric, resultExpected string) {
|
f := func(replacement, metric, resultExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
labels, err := parseMetricWithLabels(metric)
|
labels := MustParseMetricWithLabels(metric)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot parse %s: %s", metric, err)
|
|
||||||
}
|
|
||||||
result := fillLabelReferences(nil, replacement, labels)
|
result := fillLabelReferences(nil, replacement, labels)
|
||||||
if string(result) != resultExpected {
|
if string(result) != resultExpected {
|
||||||
t.Fatalf("unexpected result; got\n%q\nwant\n%q", result, resultExpected)
|
t.Fatalf("unexpected result; got\n%q\nwant\n%q", result, resultExpected)
|
||||||
|
50
lib/promrelabel/util.go
Normal file
50
lib/promrelabel/util.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user