3
votes

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

3 Answers

1
votes

Try this:

import "influxdata/influxdb/schema"

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

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"])
0
votes

I'm using the following flux-code to extract all host tag-values for the bucket "telegraf" - just as your posted InfluxQL:

import "influxdata/influxdb/schema"

schema.tagValues(bucket: "telegraf", tag: "host")

InfluxDB has a bit about this in their documentation: https://docs.influxdata.com/influxdb/v2.0/query-data/flux/explore-schema/#list-tag-values