8

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.

Bowen Jin
  • 81
  • 1
  • 2
  • 2
  • input details clearly for undesrtanding. what is the sample query that you had, and what is the expected out of it ? – Raady Apr 09 '20 at 13:18

1 Answers1

6

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})

stephenlechner
  • 1,836
  • 11
  • 11
  • Thanks for the answer Stephen! Is there a way to list out all the distinct file names? – Bowen Jin Apr 14 '20 at 16:14
  • 2
    To get a list of the filenames, in a dashboard you could add a toplist widget that queries without the `count_not_null/count_nonzero` functions, and grouped by filename (so something like `sum:your.metric.name{*} by {file_name}` ). Unless you have very-many files, juxtaposing this widget next to one that shows the overall count may get you where you want. If you have more files than the toplist option allows, you can switch the widget to JSON edit mode and input a higher number. – stephenlechner Apr 15 '20 at 18:51
  • In the same context as this question, how can I get a graph with the number of files per directory? To know which are the directories with the largest number of files? – Joaquín Fernández Aug 14 '23 at 15:10