From fdd5b85e06c3bd451af32906a185748bf451e35c Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sun, 4 Feb 2024 11:08:20 +0100 Subject: [PATCH 1/2] promql: faster range-query of label_replace and label_join These functions act on the labels only, so don't need to go step by step over the samples in a range query. Signed-off-by: Bryan Boreham --- promql/engine.go | 9 ++++ promql/functions.go | 129 +++++++++++++++++--------------------------- 2 files changed, 57 insertions(+), 81 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 02004e5f9..36132295b 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1409,6 +1409,15 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio break } } + + // Special handling for functions that work on series not samples. + switch e.Func.Name { + case "label_replace": + return ev.evalLabelReplace(e.Args) + case "label_join": + return ev.evalLabelJoin(e.Args) + } + if !matrixArg { // Does not have a matrix argument. return ev.rangeEval(nil, func(v []parser.Value, _ [][]EvalSeriesHelper, enh *EvalNodeHelper) (Vector, annotations.Annotations) { diff --git a/promql/functions.go b/promql/functions.go index fe1a5644e..eb3eb7e56 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -1321,59 +1321,47 @@ func funcChanges(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelp return append(enh.Out, Sample{F: float64(changes)}), nil } -// === label_replace(Vector parser.ValueTypeVector, dst_label, replacement, src_labelname, regex parser.ValueTypeString) (Vector, Annotations) === -func funcLabelReplace(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) { +// label_replace function operates only on series; does not look at timestamps or values. +func (ev *evaluator) evalLabelReplace(args parser.Expressions) (parser.Value, annotations.Annotations) { var ( - vector = vals[0].(Vector) dst = stringFromArg(args[1]) repl = stringFromArg(args[2]) src = stringFromArg(args[3]) regexStr = stringFromArg(args[4]) ) - if enh.regex == nil { - var err error - enh.regex, err = regexp.Compile("^(?:" + regexStr + ")$") - if err != nil { - panic(fmt.Errorf("invalid regular expression in label_replace(): %s", regexStr)) - } - if !model.LabelNameRE.MatchString(dst) { - panic(fmt.Errorf("invalid destination label name in label_replace(): %s", dst)) - } - enh.Dmn = make(map[uint64]labels.Labels, len(enh.Out)) + regex, err := regexp.Compile("^(?:" + regexStr + ")$") + if err != nil { + panic(fmt.Errorf("invalid regular expression in label_replace(): %s", regexStr)) + } + if !model.LabelNameRE.MatchString(dst) { + panic(fmt.Errorf("invalid destination label name in label_replace(): %s", dst)) } - for _, el := range vector { - h := el.Metric.Hash() - var outMetric labels.Labels - if l, ok := enh.Dmn[h]; ok { - outMetric = l - } else { - srcVal := el.Metric.Get(src) - indexes := enh.regex.FindStringSubmatchIndex(srcVal) - if indexes == nil { - // If there is no match, no replacement should take place. - outMetric = el.Metric - enh.Dmn[h] = outMetric - } else { - res := enh.regex.ExpandString([]byte{}, repl, srcVal, indexes) + val, ws := ev.eval(args[0]) + matrix := val.(Matrix) + lb := labels.NewBuilder(labels.EmptyLabels()) - lb := labels.NewBuilder(el.Metric).Del(dst) - if len(res) > 0 { - lb.Set(dst, string(res)) - } - outMetric = lb.Labels() - enh.Dmn[h] = outMetric - } + for i, el := range matrix { + srcVal := el.Metric.Get(src) + indexes := regex.FindStringSubmatchIndex(srcVal) + if indexes != nil { // Only replace when regexp matches. + res := regex.ExpandString([]byte{}, repl, srcVal, indexes) + lb.Reset(el.Metric) + lb.Set(dst, string(res)) + matrix[i].Metric = lb.Labels() } - - enh.Out = append(enh.Out, Sample{ - Metric: outMetric, - F: el.F, - H: el.H, - }) } - return enh.Out, nil + if matrix.ContainsSameLabelset() { + ev.errorf("vector cannot contain metrics with the same labelset") + } + + return matrix, ws +} + +// === label_replace(Vector parser.ValueTypeVector, dst_label, replacement, src_labelname, regex parser.ValueTypeString) (Vector, Annotations) === +func funcLabelReplace(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) { + panic("funcLabelReplace wrong implementation called") } // === Vector(s Scalar) (Vector, Annotations) === @@ -1385,19 +1373,13 @@ func funcVector(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelpe }), nil } -// === label_join(vector model.ValVector, dest_labelname, separator, src_labelname...) (Vector, Annotations) === -func funcLabelJoin(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) { +// label_join function operates only on series; does not look at timestamps or values. +func (ev *evaluator) evalLabelJoin(args parser.Expressions) (parser.Value, annotations.Annotations) { var ( - vector = vals[0].(Vector) dst = stringFromArg(args[1]) sep = stringFromArg(args[2]) srcLabels = make([]string, len(args)-3) ) - - if enh.Dmn == nil { - enh.Dmn = make(map[uint64]labels.Labels, len(enh.Out)) - } - for i := 3; i < len(args); i++ { src := stringFromArg(args[i]) if !model.LabelName(src).IsValid() { @@ -1406,42 +1388,27 @@ func funcLabelJoin(vals []parser.Value, args parser.Expressions, enh *EvalNodeHe srcLabels[i-3] = src } - if !model.LabelName(dst).IsValid() { - panic(fmt.Errorf("invalid destination label name in label_join(): %s", dst)) - } - + val, ws := ev.eval(args[0]) + matrix := val.(Matrix) srcVals := make([]string, len(srcLabels)) - for _, el := range vector { - h := el.Metric.Hash() - var outMetric labels.Labels - if l, ok := enh.Dmn[h]; ok { - outMetric = l - } else { + lb := labels.NewBuilder(labels.EmptyLabels()) - for i, src := range srcLabels { - srcVals[i] = el.Metric.Get(src) - } - - lb := labels.NewBuilder(el.Metric) - - strval := strings.Join(srcVals, sep) - if strval == "" { - lb.Del(dst) - } else { - lb.Set(dst, strval) - } - - outMetric = lb.Labels() - enh.Dmn[h] = outMetric + for i, el := range matrix { + for i, src := range srcLabels { + srcVals[i] = el.Metric.Get(src) } - - enh.Out = append(enh.Out, Sample{ - Metric: outMetric, - F: el.F, - H: el.H, - }) + strval := strings.Join(srcVals, sep) + lb.Reset(el.Metric) + lb.Set(dst, strval) + matrix[i].Metric = lb.Labels() } - return enh.Out, nil + + return matrix, ws +} + +// === label_join(vector model.ValVector, dest_labelname, separator, src_labelname...) (Vector, Annotations) === +func funcLabelJoin(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) { + panic("funcLabelReplace wrong implementation called") } // Common code for date related functions. From d3c1f0d8e061ea098ff209103e90069dc7612a5d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sun, 4 Feb 2024 11:32:26 +0100 Subject: [PATCH 2/2] promql: can now remove regex field from EvalNodeHelper Signed-off-by: Bryan Boreham --- promql/engine.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 36132295b..cc2a54318 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -30,7 +30,6 @@ import ( "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/grafana/regexp" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "go.opentelemetry.io/otel" @@ -1080,8 +1079,6 @@ type EvalNodeHelper struct { Dmn map[uint64]labels.Labels // funcHistogramQuantile for classic histograms. signatureToMetricWithBuckets map[string]*metricWithBuckets - // label_replace. - regex *regexp.Regexp lb *labels.Builder lblBuf []byte