Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prometheus query to count unique labels over a timeframe

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.

like image 975
Scott Smith Avatar asked Aug 09 '19 17:08

Scott Smith


People also ask

What is PromQL in Prometheus?

Prometheus provides a functional query language called PromQL (Prometheus Query Language) that lets the user select and aggregate time series data in real time.

How do you use the wildcard in Prometheus query?

Prometheus uses the tilde character ~ to indicate a query contains a wildcard. Inside the label-query the "dot plus" ( . + ) character combination is used where all characters are accepted.

Can Prometheus have multiple time series for the same metrics?

1 Answer. Show activity on this post. Prometheus calls this Vector Matching, you can perform arithmetic binary operations (+, -, *, / etc) across different time series if their labels match.


1 Answers

Figured it out. count(count_over_time(metric[range])) gives the value I want.

like image 161
Scott Smith Avatar answered Oct 16 '22 12:10

Scott Smith