Search code examples
datadog

How to get the number of different values of a metric's tag in Datadog


I have a metric which has a tag with lots of different values (the value is a file name). How can I create a query that determines the number of different values of that tag exist on a metric?

For example if 4 metrics are received during a time frame, with the following tags "file_name:dir/file1", "file_name:dir/file2", "file_name:dir/file3", "file_name:dir/file1"

I want the query to return the value 3, since of all the metrics received during this timeframe there were 3 distinct values for the file_name tag.


Solution

  • Either of the count_not_null() or count_nonzero() functions should get you where you want.

    If graph your metric, grouped by your tag, and then apply one of those functions, it should return the count of unique tag values under that tag key. So in your case:

    count_not_null(sum:your.metric.name{*} by {file_name})

    And it works with multiple group-by tags too, so if you had separate tags for file_name and directory then you could use this same approach to graph the count of unique combinations of these tag values, or the count of unique combinations of directory+file_name:

    count_not_null(your.metric.name{*} by {file_name,directory})