From b9469de41051c3cd88da06361b638aabf00a96d1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 2 Apr 2021 23:55:54 +0300 Subject: [PATCH] app/vmselect/promql: add ability to set label value additionally to label name for the remaining sum of time series returned from `topk_*` and `bottomk_*` functions in the form: `topk_min(N, m, "label=value")` --- app/vmselect/promql/aggr.go | 8 +++++++- app/vmselect/promql/exec_test.go | 4 ++-- docs/MetricsQL.md | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/vmselect/promql/aggr.go b/app/vmselect/promql/aggr.go index 5d6a71bf9..46ebc4323 100644 --- a/app/vmselect/promql/aggr.go +++ b/app/vmselect/promql/aggr.go @@ -693,8 +693,14 @@ func getRemainingSumTimeseries(tss []*timeseries, modifier *metricsql.ModifierEx var dst timeseries dst.CopyFromShallowTimestamps(tss[0]) removeGroupTags(&dst.MetricName, modifier) + tagValue := remainingSumTagName + n := strings.IndexByte(remainingSumTagName, '=') + if n >= 0 { + tagValue = remainingSumTagName[n+1:] + remainingSumTagName = remainingSumTagName[:n] + } dst.MetricName.RemoveTag(remainingSumTagName) - dst.MetricName.AddTag(remainingSumTagName, remainingSumTagName) + dst.MetricName.AddTag(remainingSumTagName, tagValue) for i, k := range ks { kn := getIntK(k, len(tss)) var sum float64 diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index fea34e448..d6d22735e 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -4823,7 +4823,7 @@ func TestExecSuccess(t *testing.T) { }) t.Run(`topk_max(1, remaining_sum)`, func(t *testing.T) { t.Parallel() - q := `sort_desc(topk_max(1, label_set(10, "foo", "bar") or label_set(time()/150, "baz", "sss"), "remaining_sum"))` + q := `sort_desc(topk_max(1, label_set(10, "foo", "bar") or label_set(time()/150, "baz", "sss"), "remaining_sum=foo"))` r1 := netstorage.Result{ MetricName: metricNameExpected, Values: []float64{nan, nan, nan, 10.666666666666666, 12, 13.333333333333334}, @@ -4841,7 +4841,7 @@ func TestExecSuccess(t *testing.T) { r2.MetricName.Tags = []storage.Tag{ { Key: []byte("remaining_sum"), - Value: []byte("remaining_sum"), + Value: []byte("foo"), }, } resultExpected := []netstorage.Result{r1, r2} diff --git a/docs/MetricsQL.md b/docs/MetricsQL.md index b67cb4d3c..53be6f107 100644 --- a/docs/MetricsQL.md +++ b/docs/MetricsQL.md @@ -121,7 +121,7 @@ This functionality can be tried at [an editable Grafana dashboard](http://play-g - `bottomk_avg(k, q)` - returns bottom K time series with the min averages on the given time range - `bottomk_median(k, q)` - returns bottom K time series with the min medians on the given time range. - All the `topk_*` and `bottomk_*` functions accept optional third argument - label name for the sum of the remaining time series outside top K or bottom K time series. For example, `topk_max(3, process_resident_memory_bytes, "remaining_sum")` would return up to 3 time series with the maximum value for `process_resident_memory_bytes` plus fourth time series with the sum of the remaining time series if any. The fourth time series will contain `remaining_sum="remaining_sum"` additional label. + All the `topk_*` and `bottomk_*` functions accept optional third argument - label to add to the sum of the remaining time series outside top K or bottom K time series. For example, `topk_max(3, sum(process_resident_memory_bytes) by (job), "job=other")` would return up to 3 time series with the maximum value for `sum(process_resident_memory_bytes) by (job)` plus fourth time series with the sum of the remaining time series if any. The fourth time series will contain `job="other"` label. - `share_le_over_time(m[d], le)` - returns share (in the range 0..1) of values in `m` over `d`, which are smaller or equal to `le`. Useful for calculating SLI and SLO. Example: `share_le_over_time(memory_usage_bytes[24h], 100*1024*1024)` returns the share of time series values for the last 24 hours when memory usage was below or equal to 100MB. - `share_gt_over_time(m[d], gt)` - returns share (in the range 0..1) of values in `m` over `d`, which are bigger than `gt`. Useful for calculating SLI and SLO.