Plugins: opentsdb ds: interpolate filter variables in expressions (#71232)

Use the same code path to interpolate both metric variables and
filter/tag variables when the data source is accessed either directly or
via the expressions datasource.

partial fix for #48141 (opentsdb datasource only)

Signed-off-by: Uri Okrent <uokrent@gmail.com>
This commit is contained in:
Uri Okrent 2023-08-07 12:05:13 -04:00 committed by GitHub
parent ddc3784213
commit 27a8328d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 39 deletions

View File

@ -4219,8 +4219,8 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "49"],
[0, 0, 0, "Unexpected any. Specify a different type.", "50"],
[0, 0, 0, "Unexpected any. Specify a different type.", "51"],
[0, 0, 0, "Unexpected any. Specify a different type.", "52"],
[0, 0, 0, "Do not use any type assertions.", "53"],
[0, 0, 0, "Do not use any type assertions.", "52"],
[0, 0, 0, "Unexpected any. Specify a different type.", "53"],
[0, 0, 0, "Unexpected any. Specify a different type.", "54"],
[0, 0, 0, "Unexpected any. Specify a different type.", "55"],
[0, 0, 0, "Unexpected any. Specify a different type.", "56"],

View File

@ -497,14 +497,7 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
return null;
}
const query: any = {
metric: this.templateSrv.replace(target.metric, options.scopedVars, 'pipe'),
aggregator: 'avg',
};
if (target.aggregator) {
query.aggregator = this.templateSrv.replace(target.aggregator);
}
const query = this.interpolateVariablesInQuery(target, options.scopedVars);
if (target.shouldComputeRate) {
query.rate = true;
@ -540,22 +533,6 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
}
}
if (target.filters && target.filters.length > 0) {
query.filters = cloneDeep(target.filters);
if (query.filters) {
this.interpolateVariablesInFilters(query, options);
}
} else {
query.tags = cloneDeep(target.tags);
if (query.tags) {
for (const tagKey in query.tags) {
query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], options.scopedVars, 'pipe');
}
}
}
if (target.explicitTags) {
query.explicitTags = true;
}
@ -563,11 +540,11 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
return query;
}
interpolateVariablesInFilters(query: OpenTsdbQuery, options: DataQueryRequest<OpenTsdbQuery>) {
interpolateVariablesInFilters(query: OpenTsdbQuery, scopedVars: ScopedVars) {
query.filters = query.filters?.map((filter: OpenTsdbFilter): OpenTsdbFilter => {
filter.tagk = this.templateSrv.replace(filter.tagk, options.scopedVars, 'pipe');
filter.tagk = this.templateSrv.replace(filter.tagk, scopedVars, 'pipe');
filter.filter = this.templateSrv.replace(filter.filter, options.scopedVars, 'pipe');
filter.filter = this.templateSrv.replace(filter.filter, scopedVars, 'pipe');
return filter;
});
@ -602,10 +579,30 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
return queries;
}
return queries.map((query) => ({
...query,
metric: this.templateSrv.replace(query.metric, scopedVars),
}));
return queries.map((query) => this.interpolateVariablesInQuery(query, scopedVars));
}
interpolateVariablesInQuery(target: OpenTsdbQuery, scopedVars: ScopedVars): any {
const query = cloneDeep(target);
query.metric = this.templateSrv.replace(target.metric, scopedVars, 'pipe');
query.aggregator = 'avg';
if (target.aggregator) {
query.aggregator = this.templateSrv.replace(target.aggregator);
}
if (query.filters && query.filters.length > 0) {
this.interpolateVariablesInFilters(query, scopedVars);
} else {
if (query.tags) {
for (const tagKey in query.tags) {
query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], scopedVars, 'pipe');
}
}
}
return query;
}
convertToTSDBTime(date: any, roundUp: any, timezone: any) {

View File

@ -128,7 +128,7 @@ describe('opentsdb', () => {
expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0);
});
it('should replace metric variable', () => {
it('should replace metric and filter variable', () => {
const { ds, templateSrv } = getTestcontext();
const logQuery: OpenTsdbQuery = {
refId: 'someRefId',
@ -136,8 +136,8 @@ describe('opentsdb', () => {
filters: [
{
type: 'type',
tagk: 'someTagk',
filter: 'someTagv',
tagk: '$someTagk',
filter: '$someTagv',
groupBy: true,
},
],
@ -145,8 +145,10 @@ describe('opentsdb', () => {
ds.interpolateVariablesInQueries([logQuery], {});
expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {});
expect(templateSrv.replace).toHaveBeenCalledTimes(1);
expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {}, 'pipe');
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', {}, 'pipe');
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', {}, 'pipe');
expect(templateSrv.replace).toHaveBeenCalledTimes(3);
});
it('should replace filter tag key and value', () => {
@ -210,7 +212,7 @@ describe('opentsdb', () => {
},
};
ds.interpolateVariablesInFilters(logQuery, dataQR);
ds.interpolateVariablesInFilters(logQuery, dataQR.scopedVars);
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', scopedVars, 'pipe');
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', scopedVars, 'pipe');