Search code examples
grafanainfluxdbinfluxdb-2flux-influxdb

InfluxDB2 / Grafana : how can we filter a list of tag values with flux


InfluxDB2 flux language provides a convenient way to get all the tags values for a specific bucket/measurement combination, using the schema.measurementTagValues function. The problem is that the documentation doesn't mention how to filter this list to keep only the tag values that match a certain criteria.

Example :
With the following query, I can get all the transaction tag values :

import "influxdata/influxdb/schema"

schema.measurementTagValues(
    bucket: "jmeter",
    measurement: "jmeter",
    tag: "transaction",
)

The schema contains another tag named "application". I want to get all the transactions for a specific application, not all of them.
How can we achieve this with flux?
The same request in InfluxQL would be pretty straightforward :
SHOW TAG VALUES FROM "jmeter" WITH KEY = "transaction" WHERE "application" = $application

The goal is to create Grafana dynamic dropdown lists like this one : enter image description here


Solution

  • If you use schema.tagValues() instead of schema.measurementTagValues() you can define a predicate function that filters you results. In your example:

    import "influxdata/influxdb/schema"
    
    schema.tagValues(
        bucket: "jmeter",
        tag: "transaction",
        predicate: (r) => r._measurement == "jmeter" and r.application == ${application:doublequote},
        start: -3000d
    )
    

    As you see you need to define a time range for this function. You can use the timerange variables of the grafana dashboard here (v.timeRangeStart and v.timeRangeStop) if suitable.