Skip to content

Commit f842e28

Browse files
authored
Merge pull request grafana#5343 from utkarshcmu/opentsdb-template
Implemented nested template variables functionality for Opentsdb
2 parents 22407fc + 33aa3f0 commit f842e28

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* **Scripts**: Use restart instead of start for deb package script, closes [#5282](https://github.com/grafana/grafana/pull/5282)
1212
* **Logging**: Moved to structured logging lib, and moved to component specific level filters via config file, closes [#4590](https://github.com/grafana/grafana/issues/4590)
1313
* **Search**: Add search limit query parameter, closes [#5292](https://github.com/grafana/grafana/pull/5292)
14+
* **OpenTSDB**: Support nested template variables in tag_values function, closes [4398](https://github.com/grafana/grafana/issues/4398)
1415

1516
## Breaking changes
1617
* **Logging** : Changed default logging output format (now structured into message, and key value pairs, with logger key acting as component). You can also no change in config to json log ouput.

docs/sources/datasources/opentsdb.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ When using OpenTSDB with a template variable of `query` type you can use followi
5151

5252
If you do not see template variables being populated in `Preview of values` section, you need to enable `tsd.core.meta.enable_realtime_ts` in the OpenTSDB server settings. Also, to populate metadata of the existing time series data in OpenTSDB, you need to run `tsdb uid metasync` on the OpenTSDB server.
5353

54+
### Nested Templating
55+
56+
One template variable can be used to filter tag values for another template varible. Very importantly, the order of the parameters matter in tag_values function. First parameter is the metric name, second parameter is the tag key for which you need to find tag values, and after that all other dependent template variables. Some examples are mentioned below to make nested template queries work successfully.
57+
58+
tag_values(cpu, hostname, env=$env) // return tag values for cpu metric, selected env tag value and tag key hostname
59+
tag_values(cpu, hostanme, env=$env, region=$region) // return tag values for cpu metric, selected env tag value, selected region tag value and tag key hostname
60+
5461
> Note: This is required for the OpenTSDB `lookup` api to work.
5562
5663
For details on opentsdb metric queries checkout the official [OpenTSDB documentation](http://opentsdb.net/docs/build/html/index.html)

public/app/plugins/datasource/opentsdb/datasource.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,23 @@ function (angular, _, dateMath) {
162162
});
163163
};
164164

165-
this._performMetricKeyValueLookup = function(metric, key) {
166-
if(!metric || !key) {
165+
this._performMetricKeyValueLookup = function(metric, keys) {
166+
167+
if(!metric || !keys) {
167168
return $q.when([]);
168169
}
169170

170-
var m = metric + "{" + key + "=*}";
171+
var keysArray = keys.split(",").map(function(key) {
172+
return key.trim();
173+
});
174+
var key = keysArray[0];
175+
var keysQuery = key + "=*";
176+
177+
if (keysArray.length > 1) {
178+
keysQuery += "," + keysArray.splice(1).join(",");
179+
}
180+
181+
var m = metric + "{" + keysQuery + "}";
171182

172183
return this._get('/api/search/lookup', {m: m, limit: 3000}).then(function(result) {
173184
result = result.data.results;
@@ -225,7 +236,7 @@ function (angular, _, dateMath) {
225236

226237
var metrics_regex = /metrics\((.*)\)/;
227238
var tag_names_regex = /tag_names\((.*)\)/;
228-
var tag_values_regex = /tag_values\((.*),\s?(.*)\)/;
239+
var tag_values_regex = /tag_values\((.*?),\s?(.*)\)/;
229240
var tag_names_suggest_regex = /suggest_tagk\((.*)\)/;
230241
var tag_values_suggest_regex = /suggest_tagv\((.*)\)/;
231242

public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ describe('opentsdb', function() {
5151
expect(requestOptions.params.m).to.be('cpu{hostname=*}');
5252
});
5353

54+
it('tag_values(cpu, test) should generate lookup query', function() {
55+
ctx.ds.metricFindQuery('tag_values(cpu, hostname, env=$env)').then(function(data) { results = data; });
56+
ctx.$rootScope.$apply();
57+
expect(requestOptions.url).to.be('/api/search/lookup');
58+
expect(requestOptions.params.m).to.be('cpu{hostname=*,env=$env}');
59+
});
60+
61+
it('tag_values(cpu, test) should generate lookup query', function() {
62+
ctx.ds.metricFindQuery('tag_values(cpu, hostname, env=$env, region=$region)').then(function(data) { results = data; });
63+
ctx.$rootScope.$apply();
64+
expect(requestOptions.url).to.be('/api/search/lookup');
65+
expect(requestOptions.params.m).to.be('cpu{hostname=*,env=$env,region=$region}');
66+
});
67+
5468
it('suggest_tagk() should generate api suggest query', function() {
5569
ctx.ds.metricFindQuery('suggest_tagk(foo)').then(function(data) { results = data; });
5670
ctx.$rootScope.$apply();

0 commit comments

Comments
 (0)