Skip to content

[BUGFIX] Loki: only migrate the Grafana queries of a Loki datasource - #990

Open
NopeCharon wants to merge 1 commit into
perses:mainfrom
NopeCharon:fix/loki-migration-datasource
Open

NopeCharon wants to merge 1 commit into
perses:mainfrom
NopeCharon:fix/loki-migration-datasource

Conversation

@NopeCharon

@NopeCharon NopeCharon commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

#682 aimed to stop the Loki migration script from matching the Grafana targets of other datasources
(perses/perses#4134), but the script still matches every target whose datasource has no type, and every target
without datasource. The migration engine unifies #target with the Grafana target, so the constraint
datasource: type: "loki" in #target doesn't reject these targets: it gives them the type loki, and the condition
#target.datasource.type == "loki" that follows is then true.

As the Prometheus script deliberately matches the targets without datasource type, these targets are matched by two
plugins, so since perses/perses#4139 they are replaced by the placeholder query migration_from_grafana_not_supported.
For example, with the current release of the plugins, the 301 queries of
Node Exporter Full end up as placeholders, and the migration logs
ambiguous query migration: 2 plugins matched the same target: LokiLogQuery, PrometheusTimeSeriesQuery for each of
them.

This PR checks the type of the datasource in the condition only, with the idiom of the
documentation of the query migration scripts:
(*#target.datasource.type | null) == "loki". #target no longer constrains the type, and the rest of the script is
unchanged.

Grafana target Before After
"datasource": {"type": "loki", "uid": "lk"} LokiLogQuery LokiLogQuery (unchanged)
"datasource": {"type": "loki"} LokiLogQuery LokiLogQuery (unchanged)
"datasource": {"type": "prometheus", "uid": "prom"} PrometheusTimeSeriesQuery PrometheusTimeSeriesQuery (unchanged)
"datasource": {"uid": "prom"} placeholder (ambiguous) PrometheusTimeSeriesQuery
no datasource placeholder (ambiguous) PrometheusTimeSeriesQuery
Loki target without expr, or with a null uid placeholder placeholder (unchanged)

The targets without datasource type are thus migrated by the Prometheus script again, as before the Loki script was
added. This also applies to a Loki target without datasource in a panel using a Loki datasource:
perses/perses#4817 gives such a target the datasource of its panel before the query scripts run, as Grafana does, so it is
migrated as a LokiLogQuery.

Tests

  • percli plugin test-schemas --plugin.path=loki passes, including the existing migration tests. These tests can only
    check a migration that produces a result, so I checked the targets that must not match by migrating dashboards with
    percli migrate --plugin.path and the plugins of the current release, with only this script replaced: this gives the
    table above.
  • On 19 dashboards from grafana.com, every query that changed was a placeholder before and is a migrated query now:
    594 placeholders in 11 dashboards, e.g. 301 in Node Exporter Full. Nothing else changed in the dashboards. With
    [BUGFIX] Grafana migration: use the datasource of the panel for the queries without one perses#4817, the migrated queries also keep the datasource of their panel, e.g.
    "datasource": "${ds_prometheus}" in Node Exporter Full.
  • Two of these dashboards are Loki dashboards from before Grafana 8.3: their panels give their datasource by name, and
    their targets have no datasource. Their 4 Loki targets were placeholders, and are now migrated by the Prometheus
    script, as before the Loki script was added. Such old dashboards are outside the range supported by the migration
    (Grafana 9 and later), and the type of a datasource given by name isn't known.
  • The file is formatted as cue fmt v0.17.1 expects.

Screenshots

No UI change.

Checklist

  • Pull request has a descriptive title and context useful to a reviewer.
  • Pull request title follows the [<catalog_entry>] <commit message> naming convention using one of the
    following catalog_entry values: FEATURE, ENHANCEMENT, BUGFIX, BREAKINGCHANGE, DOC,IGNORE.
  • All commits have DCO signoffs.

UI Changes

  • Changes that impact the UI include screenshots and/or screencasts of the relevant changes.
  • Code follows the UI guidelines.

The migration script of LokiLogQuery constrained the type of the
datasource in #target. As the migration engine unifies #target with the
Grafana target, this constraint doesn't only reject the targets of
another datasource: it also gives the type "loki" to the targets whose
datasource has no type, or which have no datasource at all. The check
that follows is then always true for them.

So the Loki script matched every target with an expression and without
datasource type, like the Prometheus script does on purpose. As the
migration of a target matched by several plugins is abandoned, these
targets were all replaced by the placeholder query
"migration_from_grafana_not_supported". For example, it is the case of
the 301 queries of the dashboard Node Exporter Full, whose targets have
no datasource.

The type is now checked by the condition only, as in the documentation
of the migration scripts, so the Loki script matches the targets of a
Loki datasource only. The other targets are migrated by the Prometheus
script again, as before the Loki script was added.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
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.

1 participant