Merge pull request #3088 from utkarshcmu/suggest-opentsdb

Tag suggestions fixed for v2.1.1 & above by using Suggest Api.
This commit is contained in:
Torkel Ödegaard 2015-10-30 10:34:44 +01:00
commit 0a0a0776e4
4 changed files with 40 additions and 8 deletions

View File

@ -40,8 +40,10 @@ Grafana's OpenTSDB data source now supports template variable values queries. Th
When using OpenTSDB with a template variable of `query` type you can use following syntax for lookup. When using OpenTSDB with a template variable of `query` type you can use following syntax for lookup.
metrics() // returns metric names metrics(prefix) // returns metric names with specific prefix (can be empty)
tag_names(cpu) // return tag names (i.e. keys) for a specific cpu metric tag_names(cpu) // return tag names (i.e. keys) for a specific cpu metric
tag_values(cpu, hostname) // return tag values for metric cpu and tag key hostname tag_values(cpu, hostname) // return tag values for metric cpu and tag key hostname
suggest_tagk(prefix) // return tag names (i.e. keys) for all metrics with specific prefix (can be empty)
suggest_tagv(prefix) // return tag values for all metrics with specific prefix (can be empty)
For details on opentsdb metric queries checkout the official [OpenTSDB documentation](http://opentsdb.net/docs/build/html/index.html) For details on opentsdb metric queries checkout the official [OpenTSDB documentation](http://opentsdb.net/docs/build/html/index.html)

View File

@ -80,8 +80,8 @@ function (angular, _, dateMath) {
return backendSrv.datasourceRequest(options); return backendSrv.datasourceRequest(options);
}; };
OpenTSDBDatasource.prototype._performSuggestQuery = function(query) { OpenTSDBDatasource.prototype._performSuggestQuery = function(query, type) {
return this._get('/api/suggest', {type: 'metrics', q: query, max: 1000}).then(function(result) { return this._get('/api/suggest', {type: type, q: query, max: 1000}).then(function(result) {
return result.data; return result.data;
}); });
}; };
@ -150,10 +150,12 @@ function (angular, _, dateMath) {
var metrics_regex = /metrics\((.*)\)/; var metrics_regex = /metrics\((.*)\)/;
var tag_names_regex = /tag_names\((.*)\)/; var tag_names_regex = /tag_names\((.*)\)/;
var tag_values_regex = /tag_values\((.*),\s?(.*)\)/; var tag_values_regex = /tag_values\((.*),\s?(.*)\)/;
var tag_names_suggest_regex = /suggest_tagk\((.*)\)/;
var tag_values_suggest_regex = /suggest_tagv\((.*)\)/;
var metrics_query = interpolated.match(metrics_regex); var metrics_query = interpolated.match(metrics_regex);
if (metrics_query) { if (metrics_query) {
return this._performSuggestQuery(metrics_query[1]).then(responseTransform); return this._performSuggestQuery(metrics_query[1], 'metrics').then(responseTransform);
} }
var tag_names_query = interpolated.match(tag_names_regex); var tag_names_query = interpolated.match(tag_names_regex);
@ -166,6 +168,16 @@ function (angular, _, dateMath) {
return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform); return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform);
} }
var tag_names_suggest_query = interpolated.match(tag_names_suggest_regex);
if (tag_names_suggest_query) {
return this._performSuggestQuery(tag_names_suggest_query[1], 'tagk').then(responseTransform);
}
var tag_values_suggest_query = interpolated.match(tag_values_suggest_regex);
if (tag_values_suggest_query) {
return this._performSuggestQuery(tag_values_suggest_query[1], 'tagv').then(responseTransform);
}
return $q.when([]); return $q.when([]);
}; };

View File

@ -48,13 +48,13 @@ function (angular, _, kbn) {
}; };
$scope.suggestTagKeys = function(query, callback) { $scope.suggestTagKeys = function(query, callback) {
$scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')') $scope.datasource.metricFindQuery('suggest_tagk(' + query + ')')
.then($scope.getTextValues) .then($scope.getTextValues)
.then(callback); .then(callback);
}; };
$scope.suggestTagValues = function(query, callback) { $scope.suggestTagValues = function(query, callback) {
$scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')') $scope.datasource.metricFindQuery('suggest_tagv(' + query + ')')
.then($scope.getTextValues) .then($scope.getTextValues)
.then(callback); .then(callback);
}; };

View File

@ -27,9 +27,11 @@ define([
}); });
it('metrics() should generate api suggest query', function() { it('metrics() should generate api suggest query', function() {
ctx.ds.metricFindQuery('metrics()').then(function(data) { results = data; }); ctx.ds.metricFindQuery('metrics(pew)').then(function(data) { results = data; });
ctx.$rootScope.$apply(); ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest'); expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('metrics');
expect(requestOptions.params.q).to.be('pew');
}); });
it('tag_names(cpu) should generate looku query', function() { it('tag_names(cpu) should generate looku query', function() {
@ -46,6 +48,22 @@ define([
expect(requestOptions.params.m).to.be('cpu{hostname=*}'); expect(requestOptions.params.m).to.be('cpu{hostname=*}');
}); });
it('suggest_tagk() should generate api suggest query', function() {
ctx.ds.metricFindQuery('suggest_tagk(foo)').then(function(data) { results = data; });
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('tagk');
expect(requestOptions.params.q).to.be('foo');
});
it('suggest_tagv() should generate api suggest query', function() {
ctx.ds.metricFindQuery('suggest_tagv(bar)').then(function(data) { results = data; });
ctx.$rootScope.$apply();
expect(requestOptions.url).to.be('/api/suggest');
expect(requestOptions.params.type).to.be('tagv');
expect(requestOptions.params.q).to.be('bar');
});
}); });
}); });
}); });