Convert InfluxQL to FLUX query in Grafana

Viewed 5677

In Grafana I have a dashboard that uses InfluxDB 1.x as data source, I'm migrating it to use InfluxDB 2.0 data source and Flux querys.

In the Grafana dashboard there is a Variable called "Server" which has the following query defined:

SHOW TAG VALUES ON telegraf WITH KEY = "host"

I'm really struggling creating a similar Variable with Flux query.. Any idea how to accomplish this? Thanks

3 Answers

Try this:

import "influxdata/influxdb/schema"

schema.measurementTagValues(
  bucket: "my_bucket",
  tag: "host",
  measurement: "my_measurement"
)

this work for me:

from(bucket: "telegraf")
 |> range(start: -15m)
 |> group(columns: ["host"], mode:"by")
 |> keyValues(keyColumns: ["host"])

Note: if you want more time back (e.g. -30d) the performance will be slow, you can solve it by load this query only once (available in grafana variables) or better add some filters and selectors

for example:

from(bucket: "telegraf")
  |> range(start: -30d)
  |> filter(fn: (r) => r._field == "you field")
  |> filter(fn: (r) => /* more filter*/)
  |> group(columns: ["host"], mode:"by")
  |> first()
  |> keyValues(keyColumns: ["host"])
Related