diff --git a/mixin-utils/test/test_native-classic-histogram.libsonnet b/mixin-utils/test/test_native-classic-histogram.libsonnet index b9e1140bc..5d762c710 100644 --- a/mixin-utils/test/test_native-classic-histogram.libsonnet +++ b/mixin-utils/test/test_native-classic-histogram.libsonnet @@ -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])))', }, ) ) @@ -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])))', }, ) ) @@ -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])))', }, ) ) @@ -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])))', }, ) ) @@ -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])))', }, ) ) @@ -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])))', }, ) ) diff --git a/mixin-utils/utils.libsonnet b/mixin-utils/utils.libsonnet index 3dc12910f..f80fc681e 100644 --- a/mixin-utils/utils.libsonnet +++ b/mixin-utils/utils.libsonnet @@ -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,