Skip to content

[BUGFIX] Loki query migration incorrectly matching non-Loki datasources#682

Open
gfierkasap wants to merge 6 commits into
perses:mainfrom
gfierkasap:issue-4134-migration-bug
Open

[BUGFIX] Loki query migration incorrectly matching non-Loki datasources#682
gfierkasap wants to merge 6 commits into
perses:mainfrom
gfierkasap:issue-4134-migration-bug

Conversation

@gfierkasap

@gfierkasap gfierkasap commented Jun 16, 2026

Copy link
Copy Markdown

Description

This PR is one part of the solution for perses/perses#4134

When migrating a Grafana dashboard to Perses, queries belonging to a Prometheus datasource were incorrectly migrated as LokiLogQuery with LokiDatasource.

This happened because the Loki migration script used a #target constraint to match targets with datasource.type: "loki". However, due to how the migration engine works — it fills #target with the Grafana JSON via CUE's FillPath before unifying with the script — the type: "loki" constraint is not enforced as a guard. Instead, CUE merges it into the open struct, so the script matches any target that has an expr field and a datasource.uid, regardless of whether datasource.type is present or correct.

This particularly affected dashboards where datasource.type is absent from the Grafana JSON (only uid is set), which is valid Grafana syntax. In those cases the Loki script would match and win the migration — non-deterministically, since migrateQuery iterates over a Go map.

Signed-off-by: gfierksap <gabor.fierka@sap.com>
Signed-off-by: gfierksap <gabor.fierka@sap.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Not ready to approve

The updated #target.datasource schema likely stops matching typical Grafana targets that include datasource.uid, which can prevent the Loki migration from applying even for valid Loki queries.

Pull request overview

This PR adjusts the Loki LogQL query migration CUE script to ensure it only migrates targets when the Grafana datasource explicitly declares datasource.type: "loki", preventing Prometheus (and other) targets from being incorrectly migrated as LokiLogQuery.

Changes:

  • Removes the previous datasource.type: "loki" constraint from #target and replaces it with a runtime guard that requires datasource.type to be present and equal to "loki".
  • Keeps datasource selection by UID when present, mapping it to a LokiDatasource selector.
File summaries
File Description
loki/schemas/queries/loki-log-query/migrate/migrate.cue Adds a strict datasource.type == "loki" guard to prevent non-Loki targets from being migrated as Loki log queries.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

kind: "LokiDatasource"
name: #target.datasource.uid
if #target.datasource.type != _|_ if #target.datasource.type == "loki" {
kind: "LokiLogQuery"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines 17 to 19
datasource: {
type: "loki"
...
type?: string
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Signed-off-by: gfierksap <gabor.fierka@sap.com>
Signed-off-by: gfierksap <gabor.fierka@sap.com>
@jgbernalp

jgbernalp commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

@gfierkasap thnx for your contribution. Would you mind adding tests for this case? We have test for these cases in other plugins in the form of JSON files that validate input and output. e.g. https://github.com/perses/plugins/tree/main/prometheus/schemas/prometheus-time-series-query/migrate/tests/basic

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Not ready to approve

As implemented, the guard drops valid Loki targets that omit datasource.type (a pattern present in existing fixtures), which can lead to missed or incorrect query migrations.

Copilot's findings
  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.

datasource: {
kind: "LokiDatasource"
name: #target.datasource.uid
if #target.datasource.type != _|_ if #target.datasource.type == "loki" {
Comment on lines +25 to +26
if #target.datasource.type != _|_ if #target.datasource.type == "loki" {
kind: "LokiLogQuery"
#target: {
datasource: {
type: "loki"
type?: string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
type?: string
type?: "loki"
uid: string
...

That sounds more accurate

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Signed-off-by: gfierksap <gabor.fierka@sap.com>
@gfierkasap

Copy link
Copy Markdown
Author

@gfierkasap thnx for your contribution. Would you mind adding tests for this case? We have test for these cases in other plugins in the form of JSON files that validate input and output. e.g. https://github.com/perses/plugins/tree/main/prometheus/schemas/prometheus-time-series-query/migrate/tests/basic

@jgbernalp I tried adding a test case covering this scenario, but I find adding a negative case (like this one) a bit problematic, since I assume the expected.json should be empty. I can keep working on it, so far all I get is an error on the test case with the message Failed to parse expected.json: kind cannot be empty

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.

4 participants