Prometheus: Fix interpolation of variables in Annotation queries (#43459)

This commit is contained in:
Ivana Huckova 2021-12-22 16:50:58 +01:00 committed by GitHub
parent cd93c6cdc0
commit f63c2e4bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -1062,6 +1062,31 @@ describe('PrometheusDatasource', () => {
expect(results.map((result) => [result.time, result.timeEnd])).toEqual([[120000, 120000]]);
});
});
describe('with template variables', () => {
const originalReplaceMock = jest.fn((a: string, ...rest: any) => a);
afterAll(() => {
templateSrvStub.replace = originalReplaceMock;
});
it('should interpolate variables in query expr', () => {
const query = {
...options,
annotation: {
...options.annotation,
expr: '$variable',
},
range: {
from: time({ seconds: 1 }),
to: time({ seconds: 2 }),
},
};
const interpolated = 'interpolated_expr';
templateSrvStub.replace.mockReturnValue(interpolated);
ds.annotationQuery(query);
const req = fetchMock.mock.calls[0][0];
expect(req.data.queries[0].expr).toBe(interpolated);
});
});
});
describe('When resultFormat is table and instant = true', () => {

View File

@ -688,7 +688,7 @@ export class PrometheusDatasource
data: {
from: (this.getPrometheusTime(options.range.from, false) * 1000).toString(),
to: (this.getPrometheusTime(options.range.to, true) * 1000).toString(),
queries: [queryModel],
queries: [this.applyTemplateVariables(queryModel, {})],
},
requestId: `prom-query-${annotation.name}`,
})