Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions public/app/plugins/datasource/loki/querySplitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ describe('runSplitQuery()', () => {
});
});

test('Interpolates queries before execution', async () => {
const request = createRequest([{ expr: 'count_over_time({a="b"}[$__auto])', refId: 'A', step: '$step' }]);
datasource = createLokiDatasource({
replace: (input = '') => {
return input.replace('$__auto', '5m').replace('$step', '5m');
},
getVariables: () => [],
});
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ data: [] }));
await expect(runSplitQuery(datasource, request)).toEmitValuesWith(() => {
expect(jest.mocked(datasource.runQuery).mock.calls[0][0].targets[0].expr).toBe('count_over_time({a="b"}[5m])');
expect(jest.mocked(datasource.runQuery).mock.calls[0][0].targets[0].step).toBe('5m');
});
});

test('Skips partial updates as an option', async () => {
await expect(runSplitQuery(datasource, request, { skipPartialUpdates: true })).toEmitValuesWith((emitted) => {
// 3 days, 3 chunks, 3 requests.
Expand Down
5 changes: 4 additions & 1 deletion public/app/plugins/datasource/loki/querySplitting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ export function runSplitQuery(
request: DataQueryRequest<LokiQuery>,
options: QuerySplittingOptions = {}
) {
const queries = request.targets.filter((query) => !query.hide).filter((query) => query.expr);
const queries = request.targets
.filter((query) => !query.hide)
.filter((query) => query.expr)
.map((query) => datasource.applyTemplateVariables(query, request.scopedVars, request.filters));
Comment on lines +296 to +299
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

LGTM! Per-query template variable application implemented correctly.

The refactoring from batch interpolation to per-query template variable application is correct. The filtering logic properly removes hidden queries and those without expressions before applying template variables with the appropriate scopedVars and filters context.

However, note a minor filter ordering inconsistency with shardQuerySplitting.ts (line 49-52), which filters query.expr before !query.hide. While functionally equivalent, consider aligning the filter order for consistency:


🏁 Script executed:

#!/bin/bash
# Verify filter ordering in both splitting implementations
rg -n -A2 'request\.targets' public/app/plugins/datasource/loki/querySplitting.ts public/app/plugins/datasource/loki/shardQuerySplitting.ts

Length of output: 1104


Align filter order with shardQuerySplitting.ts for consistency.

The per-query template variable application is correct, but the filter order differs from the sibling implementation in shardQuerySplitting.ts (lines 49-51). While functionally equivalent, reorder to match:

.filter((query) => query.expr)
.filter((query) => !query.hide)
🤖 Prompt for AI Agents
In public/app/plugins/datasource/loki/querySplitting.ts around lines 296 to 299
the two .filter calls are ordered differently than shardQuerySplitting.ts;
reorder the filters to first filter for query.expr then for !query.hide so the
chain becomes .filter((query) => query.expr).filter((query) => !query.hide)
before mapping, matching the sibling implementation for consistency.

const [nonSplittingQueries, normalQueries] = partition(queries, (query) => !querySupportsSplitting(query));
const [logQueries, metricQueries] = partition(normalQueries, (query) => isLogsQuery(query.expr));

Expand Down
37 changes: 26 additions & 11 deletions public/app/plugins/datasource/loki/shardQuerySplitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ describe('runShardSplitQuery()', () => {
request = createRequest([{ expr: '$SELECTOR', refId: 'A', direction: LokiQueryDirection.Scan }]);
datasource = createLokiDatasource();
datasource.languageProvider.fetchLabelValues = jest.fn();
datasource.interpolateVariablesInQueries = jest.fn().mockImplementation((queries: LokiQuery[]) => {
return queries.map((query) => {
query.expr = query.expr.replace('$SELECTOR', '{a="b"}');
return query;
});
datasource.applyTemplateVariables = jest.fn().mockImplementation((query: LokiQuery) => {
query.expr = query.expr.replace('$SELECTOR', '{a="b"}');
return query;
});
jest.mocked(datasource.languageProvider.fetchLabelValues).mockResolvedValue(['1', '10', '2', '20', '3']);
const { metricFrameA } = getMockFrames();
Expand All @@ -83,6 +81,25 @@ describe('runShardSplitQuery()', () => {
});
});

test('Interpolates queries before execution', async () => {
const request = createRequest([{ expr: 'count_over_time({a="b"}[$__auto])', refId: 'A', step: '$step' }]);
datasource = createLokiDatasource({
replace: (input = '') => {
return input.replace('$__auto', '5m').replace('$step', '5m');
},
getVariables: () => [],
});
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ data: [] }));
datasource.languageProvider.fetchLabelValues = jest.fn();
jest.mocked(datasource.languageProvider.fetchLabelValues).mockResolvedValue(['1', '10', '2', '20', '3']);
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
expect(jest.mocked(datasource.runQuery).mock.calls[0][0].targets[0].expr).toBe(
'count_over_time({a="b", __stream_shard__=~"20|10"} | drop __stream_shard__[5m])'
);
expect(jest.mocked(datasource.runQuery).mock.calls[0][0].targets[0].step).toBe('5m');
});
});

test('Users query splitting for querying over a day', async () => {
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
// 5 shards, 3 groups + empty shard group, 4 requests
Expand All @@ -92,7 +109,7 @@ describe('runShardSplitQuery()', () => {

test('Interpolates queries before running', async () => {
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
expect(datasource.interpolateVariablesInQueries).toHaveBeenCalledTimes(1);
expect(datasource.applyTemplateVariables).toHaveBeenCalledTimes(5);

expect(datasource.runQuery).toHaveBeenCalledWith({
intervalMs: expect.any(Number),
Expand Down Expand Up @@ -153,11 +170,9 @@ describe('runShardSplitQuery()', () => {
});

test('Sends the whole stream selector to fetch values', async () => {
datasource.interpolateVariablesInQueries = jest.fn().mockImplementation((queries: LokiQuery[]) => {
return queries.map((query) => {
query.expr = query.expr.replace('$SELECTOR', '{service_name="test", filter="true"}');
return query;
});
datasource.applyTemplateVariables = jest.fn().mockImplementation((query: LokiQuery) => {
query.expr = query.expr.replace('$SELECTOR', '{service_name="test", filter="true"}');
return query;
});

await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
Expand Down
6 changes: 3 additions & 3 deletions public/app/plugins/datasource/loki/shardQuerySplitting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ import { LokiQuery } from './types';
*/

export function runShardSplitQuery(datasource: LokiDatasource, request: DataQueryRequest<LokiQuery>) {
const queries = datasource
.interpolateVariablesInQueries(request.targets, request.scopedVars)
const queries = request.targets
.filter((query) => query.expr)
.filter((query) => !query.hide);
.filter((query) => !query.hide)
.map((query) => datasource.applyTemplateVariables(query, request.scopedVars, request.filters));

return splitQueriesByStreamShard(datasource, request, queries);
}
Expand Down
Loading