From 0a6a3f9ab7963a509f6b919957884f2a65dbfb28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20D=27Andr=C3=A9a=20Alemar?= Date: Fri, 23 Oct 2015 16:08:22 -0200 Subject: [PATCH] fix(prometheus): use time independent API to list metrics and labels names Using the "/api/v1/query" endpoint to extract information about metrics and labels are limited to the metrics available at the time parameter (that is set to current time), this can lead to labels not showing because they have no value in the current time even when the dashboard is displaying historic data. On the other hand "/api/v1/series" returns results including every metric and label known to Prometheus, independent of time and value. --- .../datasource/prometheus/datasource.js | 20 ++++++------------- .../prometheus/specs/datasource_specs.ts | 18 +++++++---------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.js b/public/app/plugins/datasource/prometheus/datasource.js index 3f62279f6c9..64756f4c49e 100644 --- a/public/app/plugins/datasource/prometheus/datasource.js +++ b/public/app/plugins/datasource/prometheus/datasource.js @@ -148,20 +148,13 @@ function (angular, _, moment, dateMath) { }); }); } else { - var metric_query = 'count(' + label_values_query[1] + ') by (' + - label_values_query[2] + ')'; - url = '/api/v1/query?query=' + encodeURIComponent(metric_query) + - '&time=' + (moment().valueOf() / 1000); + url = '/api/v1/series?match[]=' + encodeURIComponent(label_values_query[1]); return this._request('GET', url) .then(function(result) { - if (result.data.data.result.length === 0 || - _.keys(result.data.data.result[0].metric).length === 0) { - return []; - } - return _.map(result.data.data.result, function(metricValue) { + return _.map(result.data.data, function(metric) { return { - text: metricValue.metric[label_values_query[2]], + text: metric[label_values_query[2]], expandable: true }; }); @@ -190,14 +183,13 @@ function (angular, _, moment, dateMath) { }); } else { // if query contains full metric name, return metric name and label list - url = '/api/v1/query?query=' + encodeURIComponent(interpolated) + - '&time=' + (moment().valueOf() / 1000); + url = '/api/v1/series?match[]=' + encodeURIComponent(interpolated); return this._request('GET', url) .then(function(result) { - return _.map(result.data.data.result, function(metricData) { + return _.map(result.data.data, function(metric) { return { - text: getOriginalMetricName(metricData.metric), + text: getOriginalMetricName(metric), expandable: true }; }); diff --git a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts index fc43da279fc..a6b494ccbf8 100644 --- a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts @@ -59,19 +59,16 @@ describe('PrometheusDatasource', function() { ctx.$rootScope.$apply(); expect(results.length).to.be(3); }); - it('label_values(metric, resource) should generate count metric query', function() { + it('label_values(metric, resource) should generate series query', function() { response = { status: "success", - data: { - resultType: "vector", - result: [ - {metric: {resource: "value1"}, value: []}, - {metric: {resource: "value2"}, value: []}, - {metric: {resource: "value3"}, value: []} - ] - } + data: [ + {__name__: "metric", resource: "value1"}, + {__name__: "metric", resource: "value2"}, + {__name__: "metric", resource: "value3"} + ] }; - ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/query\?query=count\(metric\)%20by%20\(resource\)&time=.*/).respond(response); + ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response); ctx.ds.metricFindQuery('label_values(metric, resource)').then(function(data) { results = data; }); ctx.$httpBackend.flush(); ctx.$rootScope.$apply(); @@ -90,4 +87,3 @@ describe('PrometheusDatasource', function() { }); }); }); -