mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-20 07:19:17 +01:00
app/vmselect/promql: extract rmoeveGroupTags
function for removing unneeded tags from MetricName according to the given modifierExpr
This commit is contained in:
parent
2db685c19c
commit
9203170eb2
@ -6,6 +6,9 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||||
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
var aggrFuncs = map[string]aggrFunc{
|
var aggrFuncs = map[string]aggrFunc{
|
||||||
@ -67,33 +70,26 @@ func newAggrFunc(afe func(tss []*timeseries) []*timeseries) aggrFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeGroupTags(metricName *storage.MetricName, modifier *modifierExpr) {
|
||||||
|
groupOp := strings.ToLower(modifier.Op)
|
||||||
|
switch groupOp {
|
||||||
|
case "", "by":
|
||||||
|
metricName.RemoveTagsOn(modifier.Args)
|
||||||
|
case "without":
|
||||||
|
metricName.RemoveTagsIgnoring(modifier.Args)
|
||||||
|
default:
|
||||||
|
logger.Panicf("BUG: unknown group modifier: %q", groupOp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func aggrFuncExt(afe func(tss []*timeseries) []*timeseries, argOrig []*timeseries, modifier *modifierExpr, keepOriginal bool) ([]*timeseries, error) {
|
func aggrFuncExt(afe func(tss []*timeseries) []*timeseries, argOrig []*timeseries, modifier *modifierExpr, keepOriginal bool) ([]*timeseries, error) {
|
||||||
arg := copyTimeseriesMetricNames(argOrig)
|
arg := copyTimeseriesMetricNames(argOrig)
|
||||||
|
|
||||||
// Filter out superflouos tags.
|
|
||||||
var groupTags []string
|
|
||||||
groupOp := "by"
|
|
||||||
if modifier.Op != "" {
|
|
||||||
groupTags = modifier.Args
|
|
||||||
groupOp = strings.ToLower(modifier.Op)
|
|
||||||
}
|
|
||||||
switch groupOp {
|
|
||||||
case "by":
|
|
||||||
for _, ts := range arg {
|
|
||||||
ts.MetricName.RemoveTagsOn(groupTags)
|
|
||||||
}
|
|
||||||
case "without":
|
|
||||||
for _, ts := range arg {
|
|
||||||
ts.MetricName.RemoveTagsIgnoring(groupTags)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf(`unknown modifier: %q`, groupOp)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform grouping.
|
// Perform grouping.
|
||||||
m := make(map[string][]*timeseries)
|
m := make(map[string][]*timeseries)
|
||||||
bb := bbPool.Get()
|
bb := bbPool.Get()
|
||||||
for i, ts := range arg {
|
for i, ts := range arg {
|
||||||
|
removeGroupTags(&ts.MetricName, modifier)
|
||||||
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName)
|
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName)
|
||||||
if keepOriginal {
|
if keepOriginal {
|
||||||
ts = argOrig[i]
|
ts = argOrig[i]
|
||||||
|
@ -2208,6 +2208,17 @@ func TestExecSuccess(t *testing.T) {
|
|||||||
resultExpected := []netstorage.Result{r}
|
resultExpected := []netstorage.Result{r}
|
||||||
f(q, resultExpected)
|
f(q, resultExpected)
|
||||||
})
|
})
|
||||||
|
t.Run(`avg(scalar) wiTHout (xx, yy)`, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
q := `avg wiTHout (xx, yy) (123)`
|
||||||
|
r := netstorage.Result{
|
||||||
|
MetricName: metricNameExpected,
|
||||||
|
Values: []float64{123, 123, 123, 123, 123, 123},
|
||||||
|
Timestamps: timestampsExpected,
|
||||||
|
}
|
||||||
|
resultExpected := []netstorage.Result{r}
|
||||||
|
f(q, resultExpected)
|
||||||
|
})
|
||||||
t.Run(`sum(time)`, func(t *testing.T) {
|
t.Run(`sum(time)`, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
q := `sum(time()/100)`
|
q := `sum(time()/100)`
|
||||||
@ -3854,6 +3865,13 @@ func TestExecError(t *testing.T) {
|
|||||||
label_set(time()+200, "__name__", "bar", "a", "x"),
|
label_set(time()+200, "__name__", "bar", "a", "x"),
|
||||||
) + 10`)
|
) + 10`)
|
||||||
|
|
||||||
|
// Invalid aggregates
|
||||||
|
f(`sum(1, 2)`)
|
||||||
|
f(`sum(1) foo (bar)`)
|
||||||
|
f(`sum foo () (bar)`)
|
||||||
|
f(`sum(foo) by (1)`)
|
||||||
|
f(`count(foo) without ("bar")`)
|
||||||
|
|
||||||
// With expressions
|
// With expressions
|
||||||
f(`ttf()`)
|
f(`ttf()`)
|
||||||
f(`ttf(1, 2)`)
|
f(`ttf(1, 2)`)
|
||||||
|
Loading…
Reference in New Issue
Block a user