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
This commit is contained in:
Bruce Merry 2020-02-11 15:28:06 +02:00 committed by GitHub
parent e612d7a2f9
commit 02c779cfa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 4 deletions

View File

@ -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(<metric>[${__range_s}s]) != <state>)

View File

@ -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({}),

View File

@ -519,11 +519,10 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
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' },
};
}