From 02c779cfa3ce3946689921bef87fbc1ddb9306a2 Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Tue, 11 Feb 2020 15:28:06 +0200 Subject: [PATCH] Prometheus: make $__range more precise (#21722) * Make $__range more precise in Prometheus It is now always equivalent to `${__range_s}s`, rather than rounding down to an integer multiple of the biggest possible unit. For example, a range of 47 hours is now represented as `169200s` rather than `1d`. Closes #21689. * Update a unit test to match new __range calc --- docs/sources/features/datasources/prometheus.md | 2 +- public/app/plugins/datasource/prometheus/datasource.test.ts | 2 +- public/app/plugins/datasource/prometheus/datasource.ts | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/sources/features/datasources/prometheus.md b/docs/sources/features/datasources/prometheus.md index 0ef05f8b57c..39793e0d453 100755 --- a/docs/sources/features/datasources/prometheus.md +++ b/docs/sources/features/datasources/prometheus.md @@ -111,7 +111,7 @@ Query: query_result(topk(5, sum(rate(http_requests_total[$__range])) by (instanc Regex: /"([^"]+)"/ ``` -Populate a variable with the instances having a certain state over the time range shown in the dashboard, using the more precise `$__range_s`: +Populate a variable with the instances having a certain state over the time range shown in the dashboard, using `$__range_s`: ``` Query: query_result(max_over_time([${__range_s}s]) != ) diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 692a100fd52..7550e596f17 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -1456,7 +1456,7 @@ describe('PrometheusDatasource', () => { it('should use overridden ranges, not dashboard ranges', async () => { const expectedRangeSecond = 3600; - const expectedRangeString = '1h'; + const expectedRangeString = '3600s'; const query = { range: { from: time({}), diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 8a1f3559414..996f50ac40e 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -519,11 +519,10 @@ export class PrometheusDatasource extends DataSourceApi getRangeScopedVars(range: TimeRange = getTimeSrv().timeRange()) { const msRange = range.to.diff(range.from); const sRange = Math.round(msRange / 1000); - const regularRange = kbn.secondsToHms(msRange / 1000); return { __range_ms: { text: msRange, value: msRange }, __range_s: { text: sRange, value: sRange }, - __range: { text: regularRange, value: regularRange }, + __range: { text: sRange + 's', value: sRange + 's' }, }; }