Skip to content

Conversation

@akshayutture-augment
Copy link

@akshayutture-augment akshayutture-augment commented Nov 14, 2025

Test 4

Summary by CodeRabbit

  • Tests

    • Added test to verify template variables are properly interpolated before query execution.
  • Refactor

    • Enhanced Loki query splitting to apply template variables and filters to each query individually during processing.

✏️ Tip: You can customize this high-level summary in your review settings.


Replicated from ai-code-review-evaluation/grafana-coderabbit#4

…7534)

* Loki query splitting: interpolate queries before execution

* Update tests

* Prettier

* shardQuerySplitting: remove unnecessary call
@akshayutture-augment
Copy link
Author

@coderabbitai review

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@akshayutture-augment
Copy link
Author

@coderabbitai review

1 similar comment
@akshayutture-augment
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Nov 22, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link

coderabbitai bot commented Nov 22, 2025

Walkthrough

The Loki datasource template variable interpolation mechanism has been refactored from a batch operation to per-query application. The deprecated interpolateVariablesInQueries method has been replaced with applyTemplateVariables, which now processes individual queries while incorporating scopedVars and filters context.

Changes

Cohort / File(s) Summary
Shard Query Splitting Implementation
public/app/plugins/datasource/loki/shardQuerySplitting.ts
Changed query preparation to apply template variables per-query using datasource.applyTemplateVariables(query, request.scopedVars, request.filters) instead of bulk interpolation via interpolateVariablesInQueries.
Query Splitting Implementation
public/app/plugins/datasource/loki/querySplitting.ts
Added per-query template variable application via datasource.applyTemplateVariables during runSplitQuery processing, before further query preparation and splitting.
Shard Query Splitting Tests
public/app/plugins/datasource/loki/shardQuerySplitting.test.ts
Replaced mocks and assertions for interpolateVariablesInQueries with applyTemplateVariables. Updated test logic to expect per-query invocations rather than batch processing, with assertions reflecting the new single-query API.
Query Splitting Tests
public/app/plugins/datasource/loki/querySplitting.test.ts
Added test case validating template variable interpolation ($__auto, $step) in query expressions before execution, asserting resulting queries contain interpolated values like count_over_time({a="b"}[5m]).

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant SplittingLogic as Query Splitting Logic
    participant DS as LokiDatasource
    participant Shard as Shard Processor

    Client->>SplittingLogic: request with targets
    activate SplittingLogic
    
    SplittingLogic->>SplittingLogic: filter targets (expr, not hidden)
    
    loop For each target query
        SplittingLogic->>DS: applyTemplateVariables(query, scopedVars, filters)
        DS-->>SplittingLogic: query with interpolated vars
    end
    
    SplittingLogic->>Shard: splitQueriesByStreamShard(prepared queries)
    Shard-->>SplittingLogic: split queries
    
    SplittingLogic-->>Client: results
    deactivate SplittingLogic
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • API signature change: The shift from interpolateVariablesInQueries(queries[]) to applyTemplateVariables(query) requires understanding the new per-query invocation pattern and parameter handling (scopedVars, filters).
  • Test alignment: Verify that mock expectations in tests accurately reflect the new call pattern (per-query vs. batch) and that assertions validate correct variable interpolation.
  • Integration across files: Confirm that both shardQuerySplitting and querySplitting implementations correctly apply the new method and that the context (scopedVars, filters) is properly threaded through.

Poem

🐰 From batch to bloom, each query finds its way,
Template variables interpolate, one by one they play,
Scope and filters travel along the Loki stream,
Per-query application—a better, cleaner dream! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'Advanced Query Processing Architecture' is too vague and generic. It does not clearly convey the specific changes: replacing interpolateVariablesInQueries with per-query applyTemplateVariables, or the template variable interpolation improvements in query splitting. Consider a more specific title like 'Replace batch variable interpolation with per-query applyTemplateVariables' to clearly describe the core architectural change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch query-splitting-enhancements

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
public/app/plugins/datasource/loki/shardQuerySplitting.ts (1)

49-52: LGTM! Per-query template variable application implemented correctly.

The refactoring correctly applies template variables per-query with scopedVars and filters. The logic is sound and aligns with the updated interpolation mechanism.

Minor: The filter order here (query.expr then !query.hide) differs from querySplitting.ts line 296-299 (!query.hide then query.expr). While functionally equivalent, consider standardizing the order for consistency:

 const queries = request.targets
-  .filter((query) => query.expr)
   .filter((query) => !query.hide)
+  .filter((query) => query.expr)
   .map((query) => datasource.applyTemplateVariables(query, request.scopedVars, request.filters));
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dcb853d and 3ba2457.

📒 Files selected for processing (4)
  • public/app/plugins/datasource/loki/querySplitting.test.ts (1 hunks)
  • public/app/plugins/datasource/loki/querySplitting.ts (1 hunks)
  • public/app/plugins/datasource/loki/shardQuerySplitting.test.ts (4 hunks)
  • public/app/plugins/datasource/loki/shardQuerySplitting.ts (1 hunks)
🔇 Additional comments (5)
public/app/plugins/datasource/loki/querySplitting.test.ts (1)

75-88: LGTM! Test correctly validates per-query template variable interpolation.

The new test properly verifies that template variables ($__auto and $step) are interpolated before query execution. The test setup with the mocked replace function and the assertions checking the interpolated values in the first runQuery call are correct.

public/app/plugins/datasource/loki/shardQuerySplitting.test.ts (4)

56-59: LGTM! Mock correctly updated for per-query template variable application.

The mock properly implements the new applyTemplateVariables API by replacing $SELECTOR with {a="b"} on individual queries. The mutation and return pattern aligns with the expected behavior.


84-101: LGTM! Test correctly validates template variable interpolation in shard splitting.

The test properly verifies that template variables ($__auto and $step) are interpolated before execution in the shard splitting path. The assertions correctly check that the interpolated values appear in the query expression with the shard selector modification and in the step parameter.


112-112: LGTM! Correct expectation for per-query template variable application.

The expectation of 5 calls to applyTemplateVariables aligns with the per-query interpolation approach. This verifies that each query is processed individually rather than in batch.


173-176: LGTM! Mock correctly handles complex selector replacement.

The mock properly replaces $SELECTOR with a more complex selector containing multiple labels, demonstrating that the per-query template variable application works correctly with various selector patterns.

Comment on lines +296 to +299
const queries = request.targets
.filter((query) => !query.hide)
.filter((query) => query.expr)
.map((query) => datasource.applyTemplateVariables(query, request.scopedVars, request.filters));
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants