Prometheus: Interpolate template variables in interval (#42637)

* Interpolate interval

* Add test
This commit is contained in:
Ivana Huckova
2021-12-02 13:32:02 +01:00
committed by GitHub
parent d1c7db5365
commit 5cbb4a0a2d
2 changed files with 14 additions and 13 deletions
@@ -591,6 +591,19 @@ describe('PrometheusDatasource', () => {
expect(interpolatedQuery.legendFormat).toBe(legend);
});
it('should call replace function for interval', () => {
const query = {
expr: 'test{job="bar"}',
interval: '$step',
refId: 'A',
};
const step = '5s';
templateSrvStub.replace.mockReturnValue(step);
const interpolatedQuery = ds.applyTemplateVariables(query, { step: { text: step, value: step } });
expect(interpolatedQuery.interval).toBe(step);
});
it('should call replace function for expr', () => {
const query = {
expr: 'test{job="$job"}',
@@ -603,19 +616,6 @@ describe('PrometheusDatasource', () => {
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);
});
it('should add ad-hoc filters to expr', () => {
templateSrvStub.replace = jest.fn((a: string) => a);
templateSrvStub.getAdhocFilters.mockReturnValue([
@@ -961,6 +961,7 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
...target,
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
expr: this.templateSrv.replace(expr, variables, this.interpolateQueryExpr),
interval: this.templateSrv.replace(target.interval, variables),
};
}
}