[英]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."
例如,“在过去 7 天内的某个时间点,有多少个唯一标签集的值为 1”。
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
和count_over_time
进行了调查,但count
仅对即时向量进行操作,这意味着我可以及时获取一个实例的唯一标签集的数量,但不能在一个时间范围内汇总。 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. count_over_time
返回没有用的值的数量,因为我需要知道标签集的数量而不是每个标签集有多少个值。
Basically I want something like count((metric_name >= 1)[7d])
.基本上我想要像
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.这是一个非常容易在 PromQL 之外解决的问题,只需在 7 天内进行范围查询
metric_name >= 1
,然后计算响应结果字段中的系列数,但如果可能,我想在 PromQL 中执行此查询.
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
:如果您知道样本之间的间隔(又名 Prometheus 生态系统中
unique time series
scrape_interval
),那么以下查询应该返回值>=1
如果scrape_interval=30s
则过去 7 天为>=1
次:
count(count_over_time((metric >= 1)[7d:30s])
This query uses subquery feature from PromQL.此查询使用 PromQL 的子查询功能。
If scrape_interval
is unknown beforehand, then the task cannot be solved with PromQL.如果事先不知道
scrape_interval
,则无法使用 PromQL 解决该任务。 But it can be solved with count_gt_over_time and count_eq_over_time functions in MetricsQL :但它可以通过 MetricsQL 中的count_gt_over_time和count_eq_over_time函数来解决:
count(count_gt_over_time(metric[7d], 1) or count_eq_over_time(metric[7d], 1))
Figured it out. 弄清楚了。
count(count_over_time(metric[range]))
gives the value I want. count(count_over_time(metric[range]))
给出我想要的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.