Skip to content

Commit e008614

Browse files
authored
feat(mixin-utils): add ncHistogramCountIncrease (#1560)
* feat(mixin-utils): add ncHistogramCountIncrease Based on ncHistogramCountRate, but it cannot use the recording rule, because that does `rate` and not `incease`. Signed-off-by: György Krajcsovits <[email protected]> * rename local vars Signed-off-by: György Krajcsovits <[email protected]> --------- Signed-off-by: György Krajcsovits <[email protected]>
1 parent ca23803 commit e008614

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,23 @@ test.new(std.thisFile)
238238
}
239239
)
240240
)
241+
+ test.case.new(
242+
name='histogram count increase',
243+
test=test.expect.eq(
244+
actual=utils.ncHistogramCountIncrease('request_duration_seconds', 'cluster="cluster1", job="job1"'),
245+
expected={
246+
classic: 'increase(request_duration_seconds_count{cluster="cluster1", job="job1"}[$__rate_interval])',
247+
native: 'histogram_count(increase(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))',
248+
}
249+
)
250+
)
251+
+ test.case.new(
252+
name='histogram count increase with rate interval',
253+
test=test.expect.eq(
254+
actual=utils.ncHistogramCountIncrease('request_duration_seconds', 'cluster="cluster1", job="job1"', rate_interval='5m'),
255+
expected={
256+
classic: 'increase(request_duration_seconds_count{cluster="cluster1", job="job1"}[5m])',
257+
native: 'histogram_count(increase(request_duration_seconds{cluster="cluster1", job="job1"}[5m]))',
258+
}
259+
)
260+
)

mixin-utils/utils.libsonnet

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ local g = import 'grafana-builder/grafana.libsonnet';
9494
},
9595
},
9696

97+
// The ncHistogramCountIncrease (native classic histogram count rate) function is
98+
// used to calculate the histogram increase of count from native histograms or
99+
// classic histograms. Metric name should be provided without _count suffix.
100+
ncHistogramCountIncrease(metric, selector, rate_interval='$__rate_interval')::
101+
local increaseOpen = 'increase(';
102+
local increaseClose = '[%s])' % rate_interval;
103+
{
104+
classic: '%(increaseOpen)s%(metric)s_count{%(selector)s}%(increaseClose)s' % {
105+
metric: metric,
106+
rateInterval: rate_interval,
107+
increaseOpen: increaseOpen,
108+
increaseClose: increaseClose,
109+
selector: selector,
110+
},
111+
native: 'histogram_count(%(increaseOpen)s%(metric)s{%(selector)s}%(increaseClose)s)' % {
112+
metric: metric,
113+
rateInterval: rate_interval,
114+
increaseOpen: increaseOpen,
115+
increaseClose: increaseClose,
116+
selector: selector,
117+
},
118+
},
119+
97120
// TODO(krajorama) Switch to histogram_avg function for native histograms later.
98121
// ncHistogramAverageRate (native classic histogram average rate) function is
99122
// used to calculate the histogram average rate from native histograms or

0 commit comments

Comments
 (0)