diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index f6f8dfb01..a7c015d9b 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -1480,7 +1480,7 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r} f(q, resultExpected) }) - t.Run(`label_replace(mismatch)`, func(t *testing.T) { + t.Run(`label_replace(nonexisting_src)`, func(t *testing.T) { t.Parallel() q := `label_replace(time(), "__name__", "x${1}y", "foo", ".+")` r := netstorage.Result{ @@ -1491,6 +1491,21 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r} f(q, resultExpected) }) + t.Run(`label_replace(mismatch)`, func(t *testing.T) { + t.Parallel() + q := `label_replace(label_set(time(), "foo", "foobar"), "__name__", "x${1}y", "foo", "bar(.+)")` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1000, 1200, 1400, 1600, 1800, 2000}, + Timestamps: timestampsExpected, + } + r.MetricName.Tags = []storage.Tag{{ + Key: []byte("foo"), + Value: []byte("foobar"), + }} + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) t.Run(`label_replace(match)`, func(t *testing.T) { t.Parallel() q := `label_replace(time(), "__name__", "x${1}y", "foo", ".*")` diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go index b08f9e8a4..1f0f85812 100644 --- a/app/vmselect/promql/transform.go +++ b/app/vmselect/promql/transform.go @@ -1219,6 +1219,9 @@ func labelReplace(tss []*timeseries, srcLabel string, r *regexp.Regexp, dstLabel mn := &ts.MetricName dstValue := getDstValue(mn, dstLabel) srcValue := mn.GetTagValue(srcLabel) + if !r.Match(srcValue) { + continue + } b := r.ReplaceAll(srcValue, replacementBytes) *dstValue = append((*dstValue)[:0], b...) if len(b) == 0 {