Loki: Interpolate variables in live queries (#83831)

* Loki: Interpolate variables in live queries

* Update to not have to rempve private

* Update comment
This commit is contained in:
Ivana Huckova
2024-03-04 16:26:46 +02:00
committed by GitHub
parent 59fb26443f
commit b63866baaf
2 changed files with 19 additions and 6 deletions
@@ -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<LokiQuery>);
expect(ds.applyTemplateVariables).toHaveBeenCalledWith(expect.objectContaining(query), scopedVars, filters);
});
});
});
describe('applyTemplateVariables', () => {
@@ -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<LokiQuery>): Observable<DataQueryResponse> {
// 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);