Loki: Also replace step with vars (#91031)

This commit is contained in:
Sven Grossmann 2024-07-26 11:55:04 +02:00 committed by GitHub
parent f66149807d
commit f5b4fc58e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 3 deletions

View File

@ -1875,7 +1875,7 @@ describe('applyTemplateVariables', () => {
{ expr: 'rate({job="grafana"}[$__interval]) + rate({job="grafana"}[$__interval_ms])', refId: 'A' }, { expr: 'rate({job="grafana"}[$__interval]) + rate({job="grafana"}[$__interval_ms])', refId: 'A' },
scopedVars scopedVars
); );
expect(templateSrvMock.replace).toHaveBeenCalledTimes(2); expect(templateSrvMock.replace).toHaveBeenCalledTimes(3);
// Interpolated legend // Interpolated legend
expect(templateSrvMock.replace).toHaveBeenCalledWith( expect(templateSrvMock.replace).toHaveBeenCalledWith(
undefined, undefined,
@ -1910,7 +1910,7 @@ describe('applyTemplateVariables', () => {
}, },
scopedVars scopedVars
); );
expect(templateSrvMock.replace).toHaveBeenCalledTimes(2); expect(templateSrvMock.replace).toHaveBeenCalledTimes(3);
// Interpolated legend // Interpolated legend
expect(templateSrvMock.replace).toHaveBeenCalledWith( expect(templateSrvMock.replace).toHaveBeenCalledWith(
undefined, undefined,
@ -1931,6 +1931,52 @@ describe('applyTemplateVariables', () => {
expect.any(Function) expect.any(Function)
); );
}); });
it('should replace step', () => {
const templateSrvMock = {
getAdhocFilters: jest.fn().mockImplementation((query: string) => query),
replace: jest.fn((a: string | undefined, ...rest: unknown[]) => a?.replace('$testVariable', 'foo')),
} as unknown as TemplateSrv;
const ds = createLokiDatasource(templateSrvMock);
ds.addAdHocFilters = jest.fn().mockImplementation((query: string) => query);
const replacedQuery = ds.applyTemplateVariables(
{
expr: 'rate({job="grafana"}[$__range]) + rate({job="grafana"}[$__range_ms]) + rate({job="grafana"}[$__range_s])',
refId: 'A',
step: '$testVariable',
},
scopedVars
);
expect(replacedQuery).toEqual(
expect.objectContaining({
step: 'foo',
})
);
});
it('should replace legendFormat', () => {
const templateSrvMock = {
getAdhocFilters: jest.fn().mockImplementation((query: string) => query),
replace: jest.fn((a: string | undefined, ...rest: unknown[]) => a?.replace('$testVariable', 'foo')),
} as unknown as TemplateSrv;
const ds = createLokiDatasource(templateSrvMock);
ds.addAdHocFilters = jest.fn().mockImplementation((query: string) => query);
const replacedQuery = ds.applyTemplateVariables(
{
expr: 'rate({job="grafana"}[$__range]) + rate({job="grafana"}[$__range_ms]) + rate({job="grafana"}[$__range_s])',
refId: 'A',
legendFormat: '$testVariable',
},
scopedVars
);
expect(replacedQuery).toEqual(
expect.objectContaining({
legendFormat: 'foo',
})
);
});
}); });
describe('getStatsTimeRange', () => { describe('getStatsTimeRange', () => {

View File

@ -1106,10 +1106,14 @@ export class LokiDatasource
adhocFilters adhocFilters
); );
const step = this.templateSrv.replace(target.step, variables);
const legendFormat = this.templateSrv.replace(target.legendFormat, variables);
return { return {
...target, ...target,
legendFormat: this.templateSrv.replace(target.legendFormat, rest),
expr: exprWithAdHoc, expr: exprWithAdHoc,
step,
legendFormat,
}; };
} }