Commit 2f55c18d by Torkel Ödegaard

Merge pull request #2847 from jimmidyson/prometheus-datasource

Add Prometheus datasource
parents 866ea7f9 cf074889
define([ define([
'angular', 'angular',
'lodash', 'lodash',
'kbn',
'moment', 'moment',
'app/core/utils/datemath', 'app/core/utils/datemath',
'./directives', './directives',
'./queryCtrl', './queryCtrl',
], ],
function (angular, _, kbn, dateMath) { function (angular, _, moment, dateMath) {
'use strict'; 'use strict';
var module = angular.module('grafana.services'); var module = angular.module('grafana.services');
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
module.factory('PrometheusDatasource', function($q, backendSrv, templateSrv) { module.factory('PrometheusDatasource', function($q, backendSrv, templateSrv) {
function PrometheusDatasource(datasource) { function PrometheusDatasource(datasource) {
...@@ -129,8 +130,16 @@ function (angular, _, kbn, dateMath) { ...@@ -129,8 +130,16 @@ function (angular, _, kbn, dateMath) {
PrometheusDatasource.prototype.metricFindQuery = function(query) { PrometheusDatasource.prototype.metricFindQuery = function(query) {
var url; var url;
var metricsQuery = query.match(/^[a-zA-Z_:*][a-zA-Z0-9_:*]*/); var interpolated;
var labelValuesQuery = query.match(/^label_values\((.+)\)/); try {
interpolated = templateSrv.replace(query);
}
catch (err) {
return $q.reject(err);
}
var metricsQuery = interpolated.match(/^[a-zA-Z_:*][a-zA-Z0-9_:*]*/);
var labelValuesQuery = interpolated.match(/^label_values\((.+)\)/);
if (labelValuesQuery) { if (labelValuesQuery) {
// return label values // return label values
...@@ -162,11 +171,12 @@ function (angular, _, kbn, dateMath) { ...@@ -162,11 +171,12 @@ function (angular, _, kbn, dateMath) {
}); });
} else { } else {
// if query contains full metric name, return metric name and label list // if query contains full metric name, return metric name and label list
url = '/api/v1/query?query=' + encodeURIComponent(query); url = '/api/v1/query?query=' + encodeURIComponent(interpolated) +
'&time=' + (moment().valueOf() / 1000);
return this._request('GET', url) return this._request('GET', url)
.then(function(result) { .then(function(result) {
return _.map(result.data.result, function(metricData) { return _.map(result.data.data.result, function(metricData) {
return { return {
text: getOriginalMetricName(metricData.metric), text: getOriginalMetricName(metricData.metric),
expandable: true expandable: true
...@@ -183,13 +193,14 @@ function (angular, _, kbn, dateMath) { ...@@ -183,13 +193,14 @@ function (angular, _, kbn, dateMath) {
}; };
PrometheusDatasource.prototype.calculateInterval = function(interval, intervalFactor) { PrometheusDatasource.prototype.calculateInterval = function(interval, intervalFactor) {
var sec = kbn.interval_to_seconds(interval); var m = interval.match(durationSplitRegexp);
var dur = moment.duration(parseInt(m[1]), m[2]);
var sec = dur.asSeconds();
if (sec < 1) { if (sec < 1) {
sec = 1; sec = 1;
} }
return sec * intervalFactor; return Math.floor(sec * intervalFactor) + 's';
}; };
function transformMetricData(md, options) { function transformMetricData(md, options) {
......
...@@ -117,7 +117,7 @@ function (angular, _, kbn, dateMath) { ...@@ -117,7 +117,7 @@ function (angular, _, kbn, dateMath) {
$scope.calculateInterval = function() { $scope.calculateInterval = function() {
var interval = $scope.target.interval || $scope.interval; var interval = $scope.target.interval || $scope.interval;
var calculatedInterval = $scope.datasource.calculateInterval(interval, $scope.target.intervalFactor); var calculatedInterval = $scope.datasource.calculateInterval(interval, $scope.target.intervalFactor);
$scope.target.calculatedInterval = kbn.secondsToHms(calculatedInterval); $scope.target.calculatedInterval = calculatedInterval;
}; };
// TODO: validate target // TODO: validate target
......
...@@ -21,7 +21,7 @@ define([ ...@@ -21,7 +21,7 @@ define([
var results; var results;
var urlExpected = '/api/v1/query_range?query=' + var urlExpected = '/api/v1/query_range?query=' +
encodeURIComponent('test{job="testjob"}') + encodeURIComponent('test{job="testjob"}') +
'&start=1443438675&end=1443460275&step=60'; '&start=1443438675&end=1443460275&step=60s';
var query = { var query = {
range: { from: moment(1443438674760), to: moment(1443460274760) }, range: { from: moment(1443438674760), to: moment(1443460274760) },
targets: [{ expr: 'test{job="testjob"}' }], targets: [{ expr: 'test{job="testjob"}' }],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment