Prometheus: Fix interpolation of legendFormat (#40635)

* Prometheus: Fix interpolation of legendFormat

* Fix test
This commit is contained in:
Ivana Huckova 2021-10-22 09:58:59 +02:00 committed by GitHub
parent c70cfe9125
commit 998ba06f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -570,6 +570,45 @@ describe('PrometheusDatasource', () => {
}); });
}); });
describe('applyTemplateVariables', () => {
it('should call replace function for legendFormat', () => {
const query = {
expr: 'test{job="bar"}',
legendFormat: '$legend',
refId: 'A',
};
const legend = 'baz';
templateSrvStub.replace.mockReturnValue(legend);
const interpolatedQuery = ds.applyTemplateVariables(query, { legend: { text: legend, value: legend } });
expect(interpolatedQuery.legendFormat).toBe(legend);
});
it('should call replace function for expr', () => {
const query = {
expr: 'test{job="$job"}',
refId: 'A',
};
const job = 'bar';
templateSrvStub.replace.mockReturnValue(job);
const interpolatedQuery = ds.applyTemplateVariables(query, { job: { text: job, value: job } });
expect(interpolatedQuery.expr).toBe(job);
});
it('should not call replace function for interval', () => {
const query = {
expr: 'test{job="bar"}',
interval: '$interval',
refId: 'A',
};
const interval = '10s';
templateSrvStub.replace.mockReturnValue(interval);
const interpolatedQuery = ds.applyTemplateVariables(query, { interval: { text: interval, value: interval } });
expect(interpolatedQuery.interval).not.toBe(interval);
});
});
describe('metricFindQuery', () => { describe('metricFindQuery', () => {
beforeEach(() => { beforeEach(() => {
const query = 'query_result(topk(5,rate(http_request_duration_microseconds_count[$__interval])))'; const query = 'query_result(topk(5,rate(http_request_duration_microseconds_count[$__interval])))';

View File

@ -894,6 +894,7 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
return { return {
...target, ...target,
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
expr: this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr), expr: this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr),
}; };
} }