问题
I need to count the number of unique labelsets for a prometheus metric over a given timeframe. For example, "How many unique labelsets have a value of 1 at some point during the past 7 days."
I've investigated using count
and count_over_time
but count
only operates on instant vectors meaning I can get the number of unique labelsets for an instance in time, but not in aggregate over a timeframe. count_over_time
returns the number of values which isn't useful since I need to know the number of labelsets and not how many values each has.
Basically I want something like count((metric_name >= 1)[7d])
. This is a very easy problem to solve outside of PromQL by just making the range query metric_name >= 1
over 7 days and then counting the number of series in the result field of the response, but I want to perform this query in PromQL if possible.
回答1:
Figured it out. count(count_over_time(metric[range]))
gives the value I want.
来源:https://stackoverflow.com/questions/57434543/prometheus-query-to-count-unique-labels-over-a-timeframe