From 0dc0e03c4c54663d1be58d40caeb68ebb6c3340d Mon Sep 17 00:00:00 2001 From: Ivan Babrou Date: Sun, 4 Oct 2015 12:54:43 +0100 Subject: [PATCH] Add suggest_tagk() and suggest_tagv() to OpenTSDB datasource (#2840) --- docs/sources/datasources/opentsdb.md | 6 ++++-- .../plugins/datasource/opentsdb/datasource.js | 20 +++++++++++++++---- public/test/specs/opentsdbDatasource-specs.js | 20 ++++++++++++++++++- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/docs/sources/datasources/opentsdb.md b/docs/sources/datasources/opentsdb.md index 3368c39a0ae..bcaeed9056c 100644 --- a/docs/sources/datasources/opentsdb.md +++ b/docs/sources/datasources/opentsdb.md @@ -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. - 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_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) \ No newline at end of file +For details on opentsdb metric queries checkout the official [OpenTSDB documentation](http://opentsdb.net/docs/build/html/index.html) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 124c6597a00..c46b451a64c 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -80,8 +80,8 @@ function (angular, _, dateMath) { return backendSrv.datasourceRequest(options); }; - OpenTSDBDatasource.prototype._performSuggestQuery = function(query) { - return this._get('/api/suggest', {type: 'metrics', q: query, max: 1000}).then(function(result) { + OpenTSDBDatasource.prototype._performSuggestQuery = function(query, type) { + return this._get('/api/suggest', {type: type, q: query, max: 1000}).then(function(result) { return result.data; }); }; @@ -150,10 +150,12 @@ function (angular, _, dateMath) { var metrics_regex = /metrics\((.*)\)/; var tag_names_regex = /tag_names\((.*)\)/; 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); 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); @@ -166,7 +168,17 @@ function (angular, _, dateMath) { return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform); } - return $q.when([]); + 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([{text: "wtf"}]); }; OpenTSDBDatasource.prototype.testDatasource = function() { diff --git a/public/test/specs/opentsdbDatasource-specs.js b/public/test/specs/opentsdbDatasource-specs.js index 57818fdc4fa..ecd2ca598e6 100644 --- a/public/test/specs/opentsdbDatasource-specs.js +++ b/public/test/specs/opentsdbDatasource-specs.js @@ -27,9 +27,11 @@ define([ }); 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(); 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() { @@ -46,6 +48,22 @@ define([ 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'); + }); + }); }); });