8

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.

Scott Smith
  • 221
  • 2
  • 7

2 Answers2

14

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

Scott Smith
  • 221
  • 2
  • 7
1

If you know the interval between samples (aka scrape_interval in Prometheus ecosystem), then the following query should return the number of unique labelsets (aka unique time series - see this article for more details about commonly used technical terms in Prometheus) with values >=1 over the last 7 days if scrape_interval=30s:

count(count_over_time((metric >= 1)[7d:30s])

This query uses subquery feature from PromQL.

If scrape_interval is unknown beforehand, then the task cannot be solved with PromQL. But it can be solved with count_gt_over_time and count_eq_over_time functions in MetricsQL:

count(count_gt_over_time(metric[7d], 1) or count_eq_over_time(metric[7d], 1))
valyala
  • 11,669
  • 1
  • 59
  • 62