Best way to visualize number of distinct tags over time?

  1. What is the best query to use to determine the number of distinct tags in a database?
  2. Is it possible to query the number of tags that have reported data within a given time range?
  3. Is it possible to create a chart in Grafana that shows how many distinct tags are reporting data over time?
  1. To show all the existing values for a tag you can use:
    SHOW TAG VALUES WITH KEY = "__YourTagName__"
    To have a count of the values you can use
    SHOW TAG VALUES CARDINALITY WITH KEY = "tag" (estimated, exact is also available, see docs)
    The result returned sometimes is weird and cannot be visualized, so try some combinations and see if it’s actually usable (in the influx-cli the result will be readable so you can check it there for reference)

The other 2 points are a bit tricky as they require subqueries

  • to SELECT points, at least a field must be in the select list. Therefore even if we don’t need any field, at least one must be returned.
    if the field has no value no point will be returned, therefore choose a field that is always valued, or just use SELECT *
  • COUNT DISTINCT cannot be used directly on tags, therefore a subquery is needed.

This sample allows you to have the “tag” count over time

SELECT COUNT(DISTINCT "tag") as "TagCount"
FROM (
	SELECT "time","tag","ValueA"
	FROM "logins.count"
	WHERE $timeFilter
)
GROUP BY time($__interval)

In order to have the total count for a defined time range as a KPI or similar, the query stays the same, ideally, you should remove the GROUP BY… but that can’t be done as that’s required by the COUNT function.
therefore I suggest you put a really big time interval, like “1d” maybe in order to group all the points in the same “range bucket”.
Note that KPIs (“Stats” chart) will force you to also select another aggregation function (Display → Calculation) on top of the returned result like Last, Max, etc.
which is not a problem as long as you just have a one-point result, but might be not accurate if you have more points so pay attention

1 Like