diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index ee3b164db4b..ea008bd2c6c 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -18,6 +18,7 @@ import { TimeRange, ToggleFilterAction, DataQueryRequest, + ScopedVars, } from '@grafana/data'; import { BackendSrv, @@ -1652,6 +1653,19 @@ describe('LokiDatasource', () => { }).rejects.toThrow('invalid metadata request url: /index'); }); }); + + describe('live tailing', () => { + it('interpolates variables with scopedVars and filters', () => { + const ds = createLokiDatasource(); + const query: LokiQuery = { expr: '{app=$app}', refId: 'A' }; + const scopedVars: ScopedVars = { app: { text: 'interpolated', value: 'interpolated' } }; + const filters: AdHocFilter[] = []; + + jest.spyOn(ds, 'applyTemplateVariables').mockImplementation((query) => query); + ds.query({ targets: [query], scopedVars, filters, liveStreaming: true } as DataQueryRequest); + expect(ds.applyTemplateVariables).toHaveBeenCalledWith(expect.objectContaining(query), scopedVars, filters); + }); + }); }); describe('applyTemplateVariables', () => { diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 8277285cd5c..90380a9a883 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -300,7 +300,7 @@ export class LokiDatasource return merge( ...streamQueries.map((q) => doLokiChannelStream( - this.applyTemplateVariables(q, request.scopedVars), + this.applyTemplateVariables(q, request.scopedVars, request.filters), this, // the datasource streamRequest ) @@ -340,15 +340,13 @@ export class LokiDatasource /** * Used within the `query` to execute live queries. - * It is intended for explore-mode and logs-queries, not metric queries. + * It is intended for logs-queries, not metric queries. * @returns An Observable of DataQueryResponse with live query results or an empty response if no suitable queries are found. * @todo: The name says "backend" but it's actually running the query through the frontend. We should fix this. */ private runLiveQueryThroughBackend(request: DataQueryRequest): Observable { - // this only works in explore-mode so variables don't need to be handled, // and only for logs-queries, not metric queries const logsQueries = request.targets.filter((query) => query.expr !== '' && isLogsQuery(query.expr)); - if (logsQueries.length === 0) { return of({ data: [], @@ -357,9 +355,10 @@ export class LokiDatasource } const subQueries = logsQueries.map((query) => { - const maxDataPoints = query.maxLines || this.maxLines; + const interpolatedQuery = this.applyTemplateVariables(query, request.scopedVars, request.filters); + const maxDataPoints = interpolatedQuery.maxLines || this.maxLines; // FIXME: currently we are running it through the frontend still. - return this.runLiveQuery(query, maxDataPoints); + return this.runLiveQuery(interpolatedQuery, maxDataPoints); }); return merge(...subQueries);