From 15dc30edf67dff8d6f83516988eb7b7d80feba04 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Fri, 13 Nov 2015 21:45:15 +0900 Subject: [PATCH 1/2] if there isn't enough datapoint, add null data point --- .../datasource/prometheus/datasource.js | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.js b/public/app/plugins/datasource/prometheus/datasource.js index c86fa2806d3..7e45c10246c 100644 --- a/public/app/plugins/datasource/prometheus/datasource.js +++ b/public/app/plugins/datasource/prometheus/datasource.js @@ -48,6 +48,7 @@ function (angular, _, moment, dateMath) { var end = getPrometheusTime(options.range.to, true); var queries = []; + options = _.clone(options); _.each(options.targets, _.bind(function(target) { if (!target.expr || target.hide) { return; @@ -58,7 +59,13 @@ function (angular, _, moment, dateMath) { var interval = target.interval || options.interval; var intervalFactor = target.intervalFactor || 1; - query.step = this.calculateInterval(interval, intervalFactor); + target.step = query.step = this.calculateInterval(interval, intervalFactor); + var range = Math.ceil(end - start); + // Prometheus drop query if range/step > 11000 + // calibrate step if it is too big + if (query.step !== 0 && range / query.step > 11000) { + target.step = query.step = Math.ceil(range / 11000); + } queries.push(query); }, this)); @@ -96,17 +103,7 @@ function (angular, _, moment, dateMath) { }; PrometheusDatasource.prototype.performTimeSeriesQuery = function(query, start, end) { - var url = '/api/v1/query_range?query=' + encodeURIComponent(query.expr) + '&start=' + start + '&end=' + end; - - var step = query.step; - var range = Math.ceil(end - start); - // Prometheus drop query if range/step > 11000 - // calibrate step if it is too big - if (step !== 0 && range / step > 11000) { - step = Math.ceil(range / 11000); - } - url += '&step=' + step; - + var url = '/api/v1/query_range?query=' + encodeURIComponent(query.expr) + '&start=' + start + '&end=' + end + '&step=' + query.step; return this._request('GET', url); }; @@ -221,8 +218,20 @@ function (angular, _, moment, dateMath) { metricLabel = createMetricLabel(md.metric, options); - dps = _.map(md.values, function(value) { - return [parseFloat(value[1]), value[0] * 1000]; + var stepMs = parseInt(options.step.slice(0, -1)) * 1000; + var lastTimestamp = null; + _.each(md.values, function(value) { + var dp_value = parseFloat(value[1]); + if (_.isNaN(dp_value)) { + dp_value = null; + } + + var timestamp = value[0] * 1000; + if (lastTimestamp && (timestamp - lastTimestamp) > stepMs) { + dps.push([null, lastTimestamp + stepMs]); + } + lastTimestamp = timestamp; + dps.push([dp_value, timestamp]); }); return { target: metricLabel, datapoints: dps }; From a1fcd3c5b6247db96e10e3d138aea794ba945eb2 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Fri, 20 Nov 2015 15:33:52 +0900 Subject: [PATCH 2/2] import fix step calibration --- public/app/plugins/datasource/prometheus/datasource.js | 4 ++-- .../plugins/datasource/prometheus/specs/datasource_specs.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.js b/public/app/plugins/datasource/prometheus/datasource.js index 7e45c10246c..2c9279cee96 100644 --- a/public/app/plugins/datasource/prometheus/datasource.js +++ b/public/app/plugins/datasource/prometheus/datasource.js @@ -209,7 +209,7 @@ function (angular, _, moment, dateMath) { sec = 1; } - return Math.ceil(sec * intervalFactor) + 's'; + return Math.ceil(sec * intervalFactor); }; function transformMetricData(md, options) { @@ -218,7 +218,7 @@ function (angular, _, moment, dateMath) { metricLabel = createMetricLabel(md.metric, options); - var stepMs = parseInt(options.step.slice(0, -1)) * 1000; + var stepMs = parseInt(options.step) * 1000; var lastTimestamp = null; _.each(md.values, function(value) { var dp_value = parseFloat(value[1]); diff --git a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts index 2ac4992edef..58034d09cac 100644 --- a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts @@ -19,7 +19,7 @@ describe('PrometheusDatasource', function() { var results; var urlExpected = 'proxied/api/v1/query_range?query=' + encodeURIComponent('test{job="testjob"}') + - '&start=1443438675&end=1443460275&step=60s'; + '&start=1443438675&end=1443460275&step=60'; var query = { range: { from: moment(1443438674760), to: moment(1443460274760) }, targets: [{ expr: 'test{job="testjob"}' }],