loki: handle ad hoc filters in backend mode (#50135)

* loki: handle ad hoc filters in backend mode

* devenv: better loki fake data

* added test
This commit is contained in:
Gábor Farkas 2022-06-03 10:53:03 +02:00 committed by GitHub
parent 468ed68d64
commit 6011c373ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View File

@ -125,8 +125,8 @@ async function main() {
await sleep(getNextSineWaveSleepDuration());
const timestampMs = new Date().getTime();
const item = getRandomLogItem(step + 1)
lokiSendLogLine(timestampMs, JSON.stringify(item), {place:'moon'});
lokiSendLogLine(timestampMs, logFmtLine(item), {place:'luna'});
lokiSendLogLine(timestampMs, JSON.stringify(item), {place:'moon', source: 'data'});
lokiSendLogLine(timestampMs, logFmtLine(item), {place:'luna', source: 'data'});
}
}

View File

@ -1058,6 +1058,15 @@ describe('isMetricsQuery', () => {
});
});
describe('applyTemplateVariables', () => {
it('should add the adhoc filter to the query', () => {
const ds = createLokiDSForTests();
const spy = jest.spyOn(ds, 'addAdHocFilters');
ds.applyTemplateVariables({ expr: '{test}', refId: 'A' }, {});
expect(spy).toHaveBeenCalledWith('{test}');
});
});
function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) {
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
const result = ds.addAdHocFilters(lokiQuery.expr);

View File

@ -887,10 +887,12 @@ export class LokiDatasource
// We want to interpolate these variables on backend
const { __interval, __interval_ms, ...rest } = scopedVars;
const exprWithAdHoc = this.addAdHocFilters(target.expr);
return {
...target,
legendFormat: this.templateSrv.replace(target.legendFormat, rest),
expr: this.templateSrv.replace(target.expr, rest, this.interpolateQueryExpr),
expr: this.templateSrv.replace(exprWithAdHoc, rest, this.interpolateQueryExpr),
};
}