Skip to content

Commit 86f29cf

Browse files
authored
feat(grafana-builder): let users define what quantiles to show (#1558)
The test that I added was created with the original code to verify the new code. Signed-off-by: György Krajcsovits <[email protected]>
1 parent a64f0ca commit 86f29cf

File tree

6 files changed

+122
-39
lines changed

6 files changed

+122
-39
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ update-mixins:
8787

8888
tests:
8989
pushd . && cd ./common-lib && make vendor && make tests
90+
pushd . && cd ./grafana-builder/test && make tests
9091
pushd . && cd ./mixin-utils/test && make tests

grafana-builder/grafana.libsonnet

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ local utils = import 'mixin-utils/utils.libsonnet';
551551
},
552552

553553
// Assumes that there is a dashboard variable named latency_metrics, values are -1 (native) or 1 (classic)
554-
latencyPanelNativeHistogram(metricName, selector, multiplier='1e3'):: {
554+
// By default it shows the 99th and 50th quantile.
555+
latencyPanelNativeHistogram(metricName, selector, multiplier='1e3', quantile=[99, 50]):: {
555556
nullPointMode: 'null as zero',
556557
fieldConfig+: {
557558
defaults+: {
@@ -561,44 +562,45 @@ local utils = import 'mixin-utils/utils.libsonnet';
561562
unit: 'ms',
562563
},
563564
},
564-
targets: [
565-
{
566-
expr: utils.showNativeHistogramQuery(utils.ncHistogramQuantile('0.99', metricName, selector, multiplier=multiplier)),
567-
format: 'time_series',
568-
legendFormat: '99th percentile',
569-
refId: 'A',
570-
},
571-
{
572-
expr: utils.showClassicHistogramQuery(utils.ncHistogramQuantile('0.99', metricName, selector, multiplier=multiplier)),
573-
format: 'time_series',
574-
legendFormat: '99th percentile',
575-
refId: 'A_classic',
576-
},
577-
{
578-
expr: utils.showNativeHistogramQuery(utils.ncHistogramQuantile('0.50', metricName, selector, multiplier=multiplier)),
579-
format: 'time_series',
580-
legendFormat: '50th percentile',
581-
refId: 'B',
582-
},
583-
{
584-
expr: utils.showClassicHistogramQuery(utils.ncHistogramQuantile('0.50', metricName, selector, multiplier=multiplier)),
585-
format: 'time_series',
586-
legendFormat: '50th percentile',
587-
refId: 'B_classic',
588-
},
589-
{
590-
expr: utils.showNativeHistogramQuery(utils.ncHistogramAverageRate(metricName, selector, multiplier=multiplier)),
591-
format: 'time_series',
592-
legendFormat: 'Average',
593-
refId: 'C',
594-
},
595-
{
596-
expr: utils.showClassicHistogramQuery(utils.ncHistogramAverageRate(metricName, selector, multiplier=multiplier)),
597-
format: 'time_series',
598-
legendFormat: 'Average',
599-
refId: 'C_classic',
600-
},
601-
],
565+
targets:
566+
local getNextRefId(targets) = std.char(std.codepoint('A') + std.length(targets) / 2);
567+
local qTargets =
568+
std.foldl(
569+
function(acc, q)
570+
local qStr = std.toString(q);
571+
acc + [
572+
{
573+
expr: utils.showNativeHistogramQuery(utils.ncHistogramQuantile('0.' + qStr, metricName, selector, multiplier=multiplier)),
574+
format: 'time_series',
575+
legendFormat: qStr + 'th percentile',
576+
refId: getNextRefId(acc),
577+
},
578+
{
579+
expr: utils.showClassicHistogramQuery(utils.ncHistogramQuantile('0.' + qStr, metricName, selector, multiplier=multiplier)),
580+
format: 'time_series',
581+
legendFormat: qStr + 'th percentile',
582+
refId: getNextRefId(acc) + '_classic',
583+
},
584+
]
585+
,
586+
quantile,
587+
[]
588+
);
589+
qTargets +
590+
[
591+
{
592+
expr: utils.showNativeHistogramQuery(utils.ncHistogramAverageRate(metricName, selector, multiplier=multiplier)),
593+
format: 'time_series',
594+
legendFormat: 'Average',
595+
refId: getNextRefId(qTargets),
596+
},
597+
{
598+
expr: utils.showClassicHistogramQuery(utils.ncHistogramAverageRate(metricName, selector, multiplier=multiplier)),
599+
format: 'time_series',
600+
legendFormat: 'Average',
601+
refId: getNextRefId(qTargets) + '_classic',
602+
},
603+
],
602604
yaxes: $.yaxes('ms'),
603605
},
604606

grafana-builder/test/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
jsonnetfile.lock.json

grafana-builder/test/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.PHONY: tests
2+
3+
vendor jsonnetfile.lock.json: jsonnetfile.json
4+
jb install
5+
6+
tests: jsonnetfile.lock.json vendor
7+
jsonnet -J vendor/ ./test_native-classic-histogram.libsonnet
8+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": 1,
3+
"dependencies": [
4+
{
5+
"source": {
6+
"git": {
7+
"remote": "https://github.com/jsonnet-libs/testonnet.git"
8+
}
9+
},
10+
"version": "master"
11+
},
12+
{
13+
"source": {
14+
"local": {
15+
"directory": "../../mixin-utils"
16+
}
17+
}
18+
}
19+
],
20+
"legacyImports": true
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local builder = import '../grafana.libsonnet';
2+
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';
3+
4+
test.new(std.thisFile)
5+
6+
+ test.case.new(
7+
name='LatencyPanel fieldConfig',
8+
test=test.expect.eq(
9+
actual=std.get(builder.latencyPanelNativeHistogram('request_duration_seconds', 'cluster="cluster1", job="job1"'), 'targets', []),
10+
expected=[
11+
{
12+
expr: '(histogram_quantile(0.99, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))) * 1e3) and on() (vector($latency_metrics) == -1)',
13+
format: 'time_series',
14+
legendFormat: '99th percentile',
15+
refId: 'A',
16+
},
17+
{
18+
expr: '(histogram_quantile(0.99, sum by (le) (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1"}[$__rate_interval]))) * 1e3) and on() (vector($latency_metrics) == 1)',
19+
format: 'time_series',
20+
legendFormat: '99th percentile',
21+
refId: 'A_classic',
22+
},
23+
{
24+
expr: '(histogram_quantile(0.50, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))) * 1e3) and on() (vector($latency_metrics) == -1)',
25+
format: 'time_series',
26+
legendFormat: '50th percentile',
27+
refId: 'B',
28+
},
29+
{
30+
expr: '(histogram_quantile(0.50, sum by (le) (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1"}[$__rate_interval]))) * 1e3) and on() (vector($latency_metrics) == 1)',
31+
format: 'time_series',
32+
legendFormat: '50th percentile',
33+
refId: 'B_classic',
34+
},
35+
{
36+
expr: '(1e3 * sum(histogram_sum(rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))) /\nsum(histogram_count(rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))\n) and on() (vector($latency_metrics) == -1)',
37+
format: 'time_series',
38+
legendFormat: 'Average',
39+
refId: 'C',
40+
},
41+
{
42+
expr: '(1e3 * sum(rate(request_duration_seconds_sum{cluster="cluster1", job="job1"}[$__rate_interval])) /\nsum(rate(request_duration_seconds_count{cluster="cluster1", job="job1"}[$__rate_interval]))\n) and on() (vector($latency_metrics) == 1)',
43+
format: 'time_series',
44+
legendFormat: 'Average',
45+
refId: 'C_classic',
46+
},
47+
],
48+
)
49+
)

0 commit comments

Comments
 (0)