Skip to content

Commit addd7cd

Browse files
authored
mixin-utils: add sum_by parameters to ncHistogramAverageRate (#1554)
1 parent d1eede9 commit addd7cd

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mixin-utils/test/test_native-classic-histogram.libsonnet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ test.new(std.thisFile)
116116
},
117117
)
118118
)
119+
+ test.case.new(
120+
name='rate of average with sum_by labels',
121+
test=test.expect.eq(
122+
actual=utils.ncHistogramAverageRate('request_duration_seconds', 'cluster="cluster1", job="job1"', sum_by=['namespace']),
123+
expected={
124+
classic: 'sum by (namespace) (rate(request_duration_seconds_sum{cluster="cluster1", job="job1"}[$__rate_interval])) /\nsum by (namespace) (rate(request_duration_seconds_count{cluster="cluster1", job="job1"}[$__rate_interval]))\n',
125+
native: 'sum by (namespace) (histogram_sum(rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))) /\nsum by (namespace) (histogram_count(rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))\n',
126+
},
127+
)
128+
)
119129
+ test.case.new(
120130
name='rate of average in recording rule with different interval, multiplier',
121131
test=test.expect.eq(

mixin-utils/utils.libsonnet

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,27 @@ local g = import 'grafana-builder/grafana.libsonnet';
100100
// classic histograms.
101101
// If from_recording is true, the function will assume :sum_rate metric
102102
// suffix and no rate needed.
103-
ncHistogramAverageRate(metric, selector, rate_interval='$__rate_interval', multiplier='', from_recording=false)::
103+
ncHistogramAverageRate(metric, selector, rate_interval='$__rate_interval', multiplier='', from_recording=false, sum_by=[])::
104+
local sumBy = if std.length(sum_by) > 0 then ' by (%s) ' % std.join(', ', sum_by) else '';
104105
local multiplierStr = if multiplier == '' then '' else '%s * ' % multiplier;
105106
{
106107
classic: |||
107-
%(multiplier)ssum(%(sumMetricQuery)s) /
108-
sum(%(countMetricQuery)s)
108+
%(multiplier)ssum%(sumBy)s(%(sumMetricQuery)s) /
109+
sum%(sumBy)s(%(countMetricQuery)s)
109110
||| % {
110111
sumMetricQuery: $.ncHistogramSumRate(metric, selector, rate_interval, from_recording).classic,
111112
countMetricQuery: $.ncHistogramCountRate(metric, selector, rate_interval, from_recording).classic,
112113
multiplier: multiplierStr,
114+
sumBy: sumBy,
113115
},
114116
native: |||
115-
%(multiplier)ssum(%(sumMetricQuery)s) /
116-
sum(%(countMetricQuery)s)
117+
%(multiplier)ssum%(sumBy)s(%(sumMetricQuery)s) /
118+
sum%(sumBy)s(%(countMetricQuery)s)
117119
||| % {
118120
sumMetricQuery: $.ncHistogramSumRate(metric, selector, rate_interval, from_recording).native,
119121
countMetricQuery: $.ncHistogramCountRate(metric, selector, rate_interval, from_recording).native,
120122
multiplier: multiplierStr,
123+
sumBy: sumBy,
121124
},
122125
},
123126

0 commit comments

Comments
 (0)