mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
e7f1ceeb84
Automatically add common filters from one side of binary operation to the other side before sending the query to storage subsystem. See https://grafana.com/blog/2021/08/04/how-to-use-promql-joins-for-more-effective-queries-of-prometheus-metrics-at-scale/ and https://www.robustperception.io/exposing-the-software-version-to-prometheus
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package promql
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
)
|
|
|
|
func TestGetCommonLabelFilters(t *testing.T) {
|
|
f := func(metrics string, lfsExpected string) {
|
|
t.Helper()
|
|
var tss []*timeseries
|
|
var rows prometheus.Rows
|
|
rows.UnmarshalWithErrLogger(metrics, func(errStr string) {
|
|
t.Fatalf("unexpected error when parsing %s: %s", metrics, errStr)
|
|
})
|
|
for _, row := range rows.Rows {
|
|
var tags []storage.Tag
|
|
for _, tag := range row.Tags {
|
|
tags = append(tags, storage.Tag{
|
|
Key: []byte(tag.Key),
|
|
Value: []byte(tag.Value),
|
|
})
|
|
}
|
|
var ts timeseries
|
|
ts.MetricName.Tags = tags
|
|
tss = append(tss, &ts)
|
|
}
|
|
lfs := getCommonLabelFilters(tss)
|
|
me := &metricsql.MetricExpr{
|
|
LabelFilters: lfs,
|
|
}
|
|
lfsMarshaled := me.AppendString(nil)
|
|
if string(lfsMarshaled) != lfsExpected {
|
|
t.Fatalf("unexpected common label filters;\ngot\n%s\nwant\n%s", lfsMarshaled, lfsExpected)
|
|
}
|
|
}
|
|
f(``, `{}`)
|
|
f(`m 1`, `{}`)
|
|
f(`m{a="b"} 1`, `{a="b"}`)
|
|
f(`m{c="d",a="b"} 1`, `{a="b", c="d"}`)
|
|
f(`m1{a="foo"} 1
|
|
m2{a="bar"} 1`, `{a=~"bar|foo"}`)
|
|
f(`m1{a="foo"} 1
|
|
m2{b="bar"} 1`, `{}`)
|
|
f(`m1{a="foo",b="bar"} 1
|
|
m2{b="bar",c="x"} 1`, `{b="bar"}`)
|
|
}
|