From a7ac3f141950734020598d049374655c222c60ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Thu, 10 Sep 2020 20:31:53 +0200 Subject: [PATCH] Prometheus: Fix min step variable interpolation (#27505) * Add missing dependency to lockfile * Prometheus: Fix min step variable interpolation --- .../plugins/datasource/prometheus/datasource.test.ts | 10 ++++++++-- public/app/plugins/datasource/prometheus/datasource.ts | 6 +++++- yarn.lock | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 79c426ef0f8..5c5d5592717 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -1640,12 +1640,12 @@ describe('PrometheusDatasource', () => { it('should be 4 times the scrape interval if min step set to 1m and interval is 15s', () => { // For a 5m graph, $__interval is 15s ds.createQuery({ ...target, interval: '1m' }, { interval: '15s' } as any, 0, 300); - expect((templateSrv.replace as any).mock.calls[1][1]['__rate_interval'].value).toBe('240s'); + expect((templateSrv.replace as any).mock.calls[2][1]['__rate_interval'].value).toBe('240s'); }); it('should be interval + scrape interval if min step set to 1m and interval is 5m', () => { // For a 7d graph, $__interval is 5m ds.createQuery({ ...target, interval: '1m' }, { interval: '5m' } as any, 0, 10080); - expect((templateSrv.replace as any).mock.calls[1][1]['__rate_interval'].value).toBe('360s'); + expect((templateSrv.replace as any).mock.calls[2][1]['__rate_interval'].value).toBe('360s'); }); it('should be interval + scrape interval if resolution is set to 1/2 and interval is 10m', () => { // For a 7d graph, $__interval is 10m @@ -1657,6 +1657,12 @@ describe('PrometheusDatasource', () => { ds.createQuery({ ...target, intervalFactor: 2 }, { interval: '15s' } as any, 0, 300); expect((templateSrv.replace as any).mock.calls[1][1]['__rate_interval'].value).toBe('60s'); }); + it('should interpolate min step if set', () => { + templateSrv.replace = jest.fn(() => '15s'); + ds.createQuery({ ...target, interval: '$int' }, { interval: '15s' } as any, 0, 300); + expect((templateSrv.replace as any).mock.calls).toHaveLength(3); + templateSrv.replace = jest.fn((a: string) => a); + }); }); }); diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 83679c18616..202eb5f5cdf 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -351,7 +351,11 @@ export class PrometheusDatasource extends DataSourceApi templateSrv.replace(target.interval || options.interval, options.scopedVars) ); // Scrape interval as specified for the query ("Min step") or otherwise taken from the datasource. - const scrapeInterval = rangeUtil.intervalToSeconds(target.interval || this.interval); + // Min step field can have template variables in it, make sure to replace it. + const scrapeInterval = target.interval + ? rangeUtil.intervalToSeconds(templateSrv.replace(target.interval, options.scopedVars)) + : rangeUtil.intervalToSeconds(this.interval); + const intervalFactor = target.intervalFactor || 1; // Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits const adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor); diff --git a/yarn.lock b/yarn.lock index 60c594fe2b8..cf5ebe2fc44 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19633,6 +19633,11 @@ pako@~1.0.5: resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.10.tgz#4328badb5086a426aa90f541977d4955da5c9732" integrity sha512-0DTvPVU3ed8+HNXOu5Bs+o//Mbdj9VNQMUOe9oKCwh8l0GNwpTDMKCWbRjgtD291AWnkAgkqA/LOnQS8AmS1tw== +papaparse@4.6.3: + version "4.6.3" + resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-4.6.3.tgz#742e5eaaa97fa6c7e1358d2934d8f18f44aee781" + integrity sha512-LRq7BrHC2kHPBYSD50aKuw/B/dGcg29omyJbKWY3KsYUZU69RKwaBHu13jGmCYBtOc4odsLCrFyk6imfyNubJQ== + papaparse@5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.3.0.tgz#ab1702feb96e79ab4309652f36db9536563ad05a"