Prometheus: Fix interpolation of legendFormat (#40635)

* Prometheus: Fix interpolation of legendFormat

* Fix test
pull/39183/head
Ivana Huckova 4 years ago committed by GitHub
parent c70cfe9125
commit 998ba06f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      public/app/plugins/datasource/prometheus/datasource.test.ts
  2. 1
      public/app/plugins/datasource/prometheus/datasource.ts

@ -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', () => {
beforeEach(() => {
const query = 'query_result(topk(5,rate(http_request_duration_microseconds_count[$__interval])))';

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

Loading…
Cancel
Save