diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index be1f54f7a86..f1c58f6a61d 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -10,7 +10,7 @@ import PrometheusMetricFindQuery from './metric_find_query'; var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/; /** @ngInject */ -export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv) { +export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) { this.type = 'prometheus'; this.editorSrc = 'app/features/prometheus/partials/query.editor.html'; this.name = instanceSettings.name; @@ -145,7 +145,7 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS return $q.reject(err); } - var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated); + var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, timeSrv); return metricFindQuery.process(); }; diff --git a/public/app/plugins/datasource/prometheus/metric_find_query.js b/public/app/plugins/datasource/prometheus/metric_find_query.js index 25704df1e2c..f449978be81 100644 --- a/public/app/plugins/datasource/prometheus/metric_find_query.js +++ b/public/app/plugins/datasource/prometheus/metric_find_query.js @@ -1,13 +1,13 @@ define([ - 'lodash', - 'moment', + 'lodash' ], -function (_, moment) { +function (_) { 'use strict'; - function PrometheusMetricFindQuery(datasource, query) { + function PrometheusMetricFindQuery(datasource, query, timeSrv) { this.datasource = datasource; this.query = query; + this.range = timeSrv.timeRange(); } PrometheusMetricFindQuery.prototype.process = function() { @@ -51,7 +51,9 @@ function (_, moment) { }); }); } else { - url = '/api/v1/series?match[]=' + encodeURIComponent(metric); + url = '/api/v1/series?match[]=' + encodeURIComponent(metric) + + '&start=' + (this.range.from.valueOf() / 1000) + + '&end=' + (this.range.to.valueOf() / 1000); return this.datasource._request('GET', url) .then(function(result) { @@ -86,7 +88,7 @@ function (_, moment) { }; PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) { - var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (moment().valueOf() / 1000); + var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (this.range.to.valueOf() / 1000); return this.datasource._request('GET', url) .then(function(result) { @@ -107,7 +109,9 @@ function (_, moment) { }; PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) { - var url = '/api/v1/series?match[]=' + encodeURIComponent(query); + var url = '/api/v1/series?match[]=' + encodeURIComponent(query) + + '&start=' + (this.range.from.valueOf() / 1000) + + '&end=' + (this.range.to.valueOf() / 1000); var self = this; return this.datasource._request('GET', url) diff --git a/public/app/plugins/datasource/prometheus/specs/metric_find_query_specs.ts b/public/app/plugins/datasource/prometheus/specs/metric_find_query_specs.ts index d328f4cb25b..38b50d2ce79 100644 --- a/public/app/plugins/datasource/prometheus/specs/metric_find_query_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/metric_find_query_specs.ts @@ -28,7 +28,7 @@ describe('PrometheusMetricFindQuery', function() { data: ["value1", "value2", "value3"] }; ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/resource/values').respond(response); - var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)'); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)', ctx.timeSrv); pm.process().then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); @@ -43,13 +43,22 @@ describe('PrometheusMetricFindQuery', function() { {__name__: "metric", resource: "value3"} ] }; - ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response); - var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)'); + ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv); pm.process().then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); expect(results.length).to.be(3); }); + it('label_values(metric, resource) should pass correct time', function() { + ctx.timeSrv.setTime({ from: moment.utc('2011-01-01'), to: moment.utc('2015-01-01') }); + ctx.$httpBackend.expect('GET', + /proxied\/api\/v1\/series\?match\[\]=metric&start=1293840000&end=1420070400/).respond(response); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv); + pm.process().then(function(data) { results = data; }); + ctx.$httpBackend.flush(); + ctx.$rootScope.$apply(); + }); it('label_values(metric{label1="foo", label2="bar", label3="baz"}, resource) should generate series query', function() { response = { status: "success", @@ -59,8 +68,8 @@ describe('PrometheusMetricFindQuery', function() { {__name__: "metric", resource: "value3"} ] }; - ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response); - var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)'); + ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv); pm.process().then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); @@ -72,7 +81,7 @@ describe('PrometheusMetricFindQuery', function() { data: ["metric1","metric2","metric3","nomatch"] }; ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/__name__/values').respond(response); - var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)'); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)', ctx.timeSrv); pm.process().then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); @@ -90,7 +99,7 @@ describe('PrometheusMetricFindQuery', function() { } }; ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/query\?query=metric&time=.*/).respond(response); - var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)'); + var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)', ctx.timeSrv); pm.process().then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); diff --git a/public/test/specs/helpers.js b/public/test/specs/helpers.js index ba75a4ebf51..7b64e18175d 100644 --- a/public/test/specs/helpers.js +++ b/public/test/specs/helpers.js @@ -138,6 +138,10 @@ define([ this.replace = function(target) { return target; }; + + this.setTime = function(time) { + this.time = time; + }; } function ContextSrvStub() {