Count all TagValues for each TagKey in all Measurements of a Bucket

Hi Flux-Forum,

While creating a selfmonitoring-dashboard for the influx-instances of a customer, I stumbled across a challenging task.

Because the customer can´t control how much tagValues are written into a measurement-tag, we have to setup some kind of a alerting system, so he gets notified, if someone writes IDs or something like this into the database.

Example Output:

| Measurement     | Tag       | TagValue Count |   |   |
|-----------------|-----------|----------------|---|---|
| method_duration | http_path | 32             |   |   |
| method_duration | message   | 30             |   |   |
| jvm_memory_sum  | maschine  | 8              |   |   |

I tryed to play around with the schema-functions, like measurementTagValues(), measurementTagKeys(), and map the output of them down, but I did not get my expected result yet.

Maybe someone could help with this?

@levinkerschberger If you’re using a relatively recent version of InfluxDB or InfluxDB Cloud, the following query should do it for you:

import "array"
import "influxdata/influxdb/schema"

bucket = "example-bucket"
measurement = "example-measurement"

// Return an array of tag keys
tags = schema.measurementTagKeys(bucket: bucket, measurement: measurement)
    |> findColumn(fn: (key) => true, column: "_value")
    |> array.filter(fn: (x) => x !~ /^_/)

// Iterate through the array of tag keys and return an array of streams
// each containing the tag values for a tag key
tagValuesArr = tags
    |> array.map(fn: (x) =>
        schema.measurementTagValues(
            tag: x,
            bucket: bucket,
            measurement: measurement
        )   
            |> map(fn: (r) => ({r with tag: x }))
    )

// Union all tag key-value streams into a single stream
// Return a count for each tag
union(tables: tagValuesArr)
    |> group(columns: ["tag"])
    |> count()
1 Like