Commit daee7970 by Jimmi Dyson

Add label_values query to get labels on a particular metric

parent 2f55c18d
...@@ -128,7 +128,7 @@ function (angular, _, moment, dateMath) { ...@@ -128,7 +128,7 @@ function (angular, _, moment, dateMath) {
}; };
PrometheusDatasource.prototype.metricFindQuery = function(query) { PrometheusDatasource.prototype.metricFindQuery = function(query) {
var url; if (!query) { return $q.when([]); }
var interpolated; var interpolated;
try { try {
...@@ -138,27 +138,51 @@ function (angular, _, moment, dateMath) { ...@@ -138,27 +138,51 @@ function (angular, _, moment, dateMath) {
return $q.reject(err); return $q.reject(err);
} }
var metricsQuery = interpolated.match(/^[a-zA-Z_:*][a-zA-Z0-9_:*]*/); var label_values_regex = /^label_values\(([^,]+)(?:,\s*(.+))?\)$/;
var labelValuesQuery = interpolated.match(/^label_values\((.+)\)/); var metric_names_regex = /^metrics\((.+)\)$/;
if (labelValuesQuery) { var label_values_query = interpolated.match(label_values_regex);
// return label values if (label_values_query) {
url = '/api/v1/label/' + labelValuesQuery[1] + '/values'; if (!label_values_query[2]) {
// return label values globally
var url = '/api/v1/label/' + label_values_query[1] + '/values';
return this._request('GET', url).then(function(result) { return this._request('GET', url).then(function(result) {
return _.map(result.data.data, function(value) { return _.map(result.data.data, function(value) {
return {text: value}; return {text: value};
}); });
}); });
} else if (metricsQuery != null && metricsQuery[0].indexOf('*') >= 0) { } else {
// if query has wildcard character, return metric name list var metric_query = 'count(' + label_values_query[1] + ') by (' +
url = '/api/v1/label/__name__/values'; label_values_query[2] + ')';
var url = '/api/v1/query?query=' + encodeURIComponent(metric_query) +
'&time=' + (moment().valueOf() / 1000);
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 {
text: metricValue.metric[label_values_query[2]],
expandable: true
};
});
});
}
}
var metric_names_query = interpolated.match(metric_names_regex);
if (metric_names_query) {
var url = '/api/v1/label/__name__/values';
return this._request('GET', url) return this._request('GET', url)
.then(function(result) { .then(function(result) {
return _.chain(result.data.data) return _.chain(result.data.data)
.filter(function(metricName) { .filter(function(metricName) {
var r = new RegExp(metricsQuery[0].replace(/\*/g, '.*')); var r = new RegExp(metric_names_query[1]);
return r.test(metricName); return r.test(metricName);
}) })
.map(function(matchedMetricName) { .map(function(matchedMetricName) {
...@@ -171,7 +195,7 @@ function (angular, _, moment, dateMath) { ...@@ -171,7 +195,7 @@ function (angular, _, moment, 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(interpolated) + var url = '/api/v1/query?query=' + encodeURIComponent(interpolated) +
'&time=' + (moment().valueOf() / 1000); '&time=' + (moment().valueOf() / 1000);
return this._request('GET', url) return this._request('GET', url)
......
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