Prometheus 查询以计算唯一标签值
Prometheus query to count unique label values
我想计算唯一标签值的数量。有点像
select count (distinct a) from hello_info
例如,如果我的指标 'hello_info' 有标签 a 和 b。我想计算唯一 a 的数量。在这里,对于 a = "1"、"2"、"3",计数将为 3。
hello_info(a="1", b="ddd")
hello_info(a="2", b="eee")
hello_info(a="1", b="fff")
hello_info(a="3", b="ggg")
count(count by (a) (hello_info))
首先,您需要一个聚合器,每个值的结果为 a
,然后您可以对它们进行计数。
其他例子:
如果你想根据标签的不同值(ex:app)统计一个kubernetes集群中部署的应用数量:
count(count(kube_pod_labels{app=~".*"}) by (app))
count(count(hello_info) by (a))
相当于下面的SQL:
SELECT
time_bucket('5 minutes', timestamp) AS t,
COUNT(DISTINCT a)
FROM hello_info
GROUP BY t
参见time_bucket()函数说明。
例如它 returns 默认情况下每个 5-minute
间隔的 a
标签的不同值的数量 - 有关 5 分钟间隔的详细信息,请参阅 staleness docs。
如果您需要计算 a
标签在自定义时间间隔内(例如,在过去一天内)的唯一值数量,则必须改用以下 PromQL 查询:
count(count(last_over_time(hello_info[1d])) by (a))
自定义间隔 - 在上述情况下为 1d
- 可以更改为任意值 - 请参阅 these docs 了解可能的值,可以在那里使用。
此查询使用 last_over_time() function for selecting all the time series, which were active during the last day. Time series can stop receiving new samples and become inactive at any time. Such time series aren't captured with simple count(...) by (a)
after 5 minutes of inactivity. New deployments in Kubernetes and horizontal pod autoscaling are the most frequent source of big number of inactive time series (aka high churn rate).
我想计算唯一标签值的数量。有点像
select count (distinct a) from hello_info
例如,如果我的指标 'hello_info' 有标签 a 和 b。我想计算唯一 a 的数量。在这里,对于 a = "1"、"2"、"3",计数将为 3。
hello_info(a="1", b="ddd")
hello_info(a="2", b="eee")
hello_info(a="1", b="fff")
hello_info(a="3", b="ggg")
count(count by (a) (hello_info))
首先,您需要一个聚合器,每个值的结果为 a
,然后您可以对它们进行计数。
其他例子: 如果你想根据标签的不同值(ex:app)统计一个kubernetes集群中部署的应用数量:
count(count(kube_pod_labels{app=~".*"}) by (app))
count(count(hello_info) by (a))
相当于下面的SQL:
SELECT
time_bucket('5 minutes', timestamp) AS t,
COUNT(DISTINCT a)
FROM hello_info
GROUP BY t
参见time_bucket()函数说明。
例如它 returns 默认情况下每个 5-minute
间隔的 a
标签的不同值的数量 - 有关 5 分钟间隔的详细信息,请参阅 staleness docs。
如果您需要计算 a
标签在自定义时间间隔内(例如,在过去一天内)的唯一值数量,则必须改用以下 PromQL 查询:
count(count(last_over_time(hello_info[1d])) by (a))
自定义间隔 - 在上述情况下为 1d
- 可以更改为任意值 - 请参阅 these docs 了解可能的值,可以在那里使用。
此查询使用 last_over_time() function for selecting all the time series, which were active during the last day. Time series can stop receiving new samples and become inactive at any time. Such time series aren't captured with simple count(...) by (a)
after 5 minutes of inactivity. New deployments in Kubernetes and horizontal pod autoscaling are the most frequent source of big number of inactive time series (aka high churn rate).