Skip to content

Commit

Permalink
Fix utils.ncHistogramLeRate formatting issues (grafana#1297)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Nikolic <[email protected]>
  • Loading branch information
duricanikolic authored Aug 6, 2024
1 parent 661b885 commit d9ba581
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
24 changes: 12 additions & 12 deletions mixin-utils/test/test_native-classic-histogram.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '0.1'),
expected={
classic: 'sum (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[$__rate_interval])\n)\n',
native: 'histogram_fraction(0, 0.1, sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n*\nhistogram_count(sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n',
classic: 'sum (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[$__rate_interval]))',
native: 'histogram_fraction(0, 0.1, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))*histogram_count(sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))',
},
)
)
Expand All @@ -163,8 +163,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '10'),
expected={
classic: 'sum (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le=~"10|10\\\\.0"}[$__rate_interval])\n)\n',
native: 'histogram_fraction(0, 10.0, sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n*\nhistogram_count(sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n',
classic: 'sum (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le=~"10|10\\\\.0"}[$__rate_interval]))',
native: 'histogram_fraction(0, 10.0, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))*histogram_count(sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))',
},
)
)
Expand All @@ -173,8 +173,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '+Inf'),
expected={
classic: 'sum (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="+Inf"}[$__rate_interval])\n)\n',
native: 'histogram_fraction(0, +Inf, sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n*\nhistogram_count(sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n',
classic: 'sum (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="+Inf"}[$__rate_interval]))',
native: 'histogram_fraction(0, +Inf, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))*histogram_count(sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))',
},
)
)
Expand All @@ -183,8 +183,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '-Inf'),
expected={
classic: 'sum (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="-Inf"}[$__rate_interval])\n)\n',
native: 'histogram_fraction(0, -Inf, sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n*\nhistogram_count(sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n',
classic: 'sum (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="-Inf"}[$__rate_interval]))',
native: 'histogram_fraction(0, -Inf, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))*histogram_count(sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))',
},
)
)
Expand All @@ -193,8 +193,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '0.1', '5m'),
expected={
classic: 'sum (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[5m])\n)\n',
native: 'histogram_fraction(0, 0.1, sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[5m]))\n)\n*\nhistogram_count(sum (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[5m]))\n)\n',
classic: 'sum (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[5m]))',
native: 'histogram_fraction(0, 0.1, sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[5m])))*histogram_count(sum (rate(request_duration_seconds{cluster="cluster1", job="job1"}[5m])))',
},
)
)
Expand All @@ -203,8 +203,8 @@ test.new(std.thisFile)
test=test.expect.eq(
actual=utils.ncHistogramLeRate('request_duration_seconds', 'cluster="cluster1", job="job1"', '0.1', sum_by=['cluster', 'namespace']),
expected={
classic: 'sum by (cluster, namespace) (\n rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[$__rate_interval])\n)\n',
native: 'histogram_fraction(0, 0.1, sum by (cluster, namespace) (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n*\nhistogram_count(sum by (cluster, namespace) (\n rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval]))\n)\n',
classic: 'sum by (cluster, namespace) (rate(request_duration_seconds_bucket{cluster="cluster1", job="job1", le="0.1"}[$__rate_interval]))',
native: 'histogram_fraction(0, 0.1, sum by (cluster, namespace) (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))*histogram_count(sum by (cluster, namespace) (rate(request_duration_seconds{cluster="cluster1", job="job1"}[$__rate_interval])))',
},
)
)
Expand Down
16 changes: 2 additions & 14 deletions mixin-utils/utils.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,14 @@ local g = import 'grafana-builder/grafana.libsonnet';
local sumBy = if std.length(sum_by) > 0 then ' by (%(lbls)s) ' % { lbls: std.join(', ', sum_by) } else ' ';
local isWholeNumber(str) = str != '' && std.foldl(function(acc, c) acc && (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'), std.stringChars(str), true);
{
native: |||
histogram_fraction(0, %(le)s, sum%(sumBy)s(
rate(%(metric)s{%(selector)s}[%(rateInterval)s]))
)
*
histogram_count(sum%(sumBy)s(
rate(%(metric)s{%(selector)s}[%(rateInterval)s]))
)
||| % {
native: 'histogram_fraction(0, %(le)s, sum%(sumBy)s(rate(%(metric)s{%(selector)s}[%(rateInterval)s])))*histogram_count(sum%(sumBy)s(rate(%(metric)s{%(selector)s}[%(rateInterval)s])))' % {
le: if isWholeNumber(le) then le + '.0' else le, // Treated as float number.
metric: metric,
rateInterval: rate_interval,
selector: selector,
sumBy: sumBy,
},
classic: |||
sum%(sumBy)s(
rate(%(metric)s_bucket{%(selector)s, %(le)s}[%(rateInterval)s])
)
||| % {
classic: 'sum%(sumBy)s(rate(%(metric)s_bucket{%(selector)s, %(le)s}[%(rateInterval)s]))' % {
// le is treated as string, thus it needs to account for Prometheus text format not having '.0', but OpenMetrics having it.
// Also the resulting string in yaml is stored directly, so the \\ needs to be escaped to \\\\.
le: if isWholeNumber(le) then 'le=~"%(le)s|%(le)s\\\\.0"' % { le: le } else 'le="%s"' % le,
Expand Down

0 comments on commit d9ba581

Please sign in to comment.