[英]Prometheus query to average over time by a specific label
I need to query a metric and find out the average value of the metric over a period of 24hrs.我需要查询一个指标并找出该指标在 24 小时内的平均值。 But using using
avg_over_time
directly on the metric won't work.但是直接在指标上使用 using
avg_over_time
是行不通的。 There is a specific ipaddr
label. The average has to be grouped by each ipaddr
.有一个特定的
ipaddr
label。平均值必须按每个ipaddr
分组。 Now, grouping is not allowed in avg_over_time
.现在,
avg_over_time
不允许分组。 In such case, how can I find out the average of the metric over 24 hrs for each ipaddr
?在这种情况下,我如何才能找出每个
ipaddr
24 小时内指标的平均值?
The metric and its values are like this指标及其值是这样的
K_utilization{ifName="Ds12:1/0/30",ipaddr="10.1.109.54",node="worker"} 3.5
K_utilization{ifName="Ds65:1/0/4",ipaddr="10.1.5.50",node="worker"} 13.2
K_utilization{ifName="Ds26:1/0/8",ipaddr="10.1.123.58",node="worker"} 3.2
K_utilization{ifName="Ds69:0/0/10",ipaddr="10.1.115.55",node="worker"} 6.2
K_utilization{ifName="Ds71:0/0/21",ipaddr="10.1.25.51",node="worker"} 13.5
The avg_over_time
function expects a range vector, which means that you could (if I understood correctly) use subquery like: avg_over_time
函数需要一个范围向量,这意味着您可以(如果我理解正确)使用子查询,例如:
avg_over_time(K_utilization[1h:5m])
This will look at the K_utilization
metric for the last 1h at a 5m resolution, the result should contain all labels from the metric.这将以 5m 的分辨率查看过去 1h 的
K_utilization
指标,结果应包含指标中的所有标签。
You could also aggregate the metric in the subquery by the ipaddr
label with a sum
subquery and then calculate the avg_over_time
:您还可以通过带有
sum
子查询的ipaddr
标签聚合子查询中的指标,然后计算avg_over_time
:
avg_over_time(sum by (ipaddr) (K_utilization)[1h:5m])
More info about Prometheus subqueries 🔖有关Prometheus 子查询的更多信息🔖
The following PromQL query returns the average K_utilization
over the last 24 hours grouped by ipaddr
:以下 PromQL 查询返回按
ipaddr
分组的过去 24 小时内的平均K_utilization
:
sum(sum_over_time(K_utilization[24h])) by (ipaddr)
/
sum(count_over_time(K_utilization[24h])) by (ipaddr)
It uses sum_over_time andcount_over_time functions for calculating the average value.它使用sum_over_time和count_over_time函数来计算平均值。
This query is roughly equivalent to the following SQL:这个查询大致相当于下面的 SQL:
SELECT ipaddr, avg(value)
FROM K_utilization
WHERE timestamp > now() - interval '24 hours'
GROUP BY ipaddr
It is assumed that the K_utilization
table contains the following fields:假设
K_utilization
表包含以下字段:
ipaddr string
timestamp int
value float
The following PromQL query returns the average K_utilization over the last 24 hours grouped by ipaddr:
以下 PromQL 查询返回按 ipaddr 分组的过去 24 小时内的平均 K_utilization:
sum(sum_over_time(K_utilization[24h])) by (ipaddr) / sum(count_over_time(K_utilization[24h])) by (ipaddr)
sum(sum_over_time(K_utilization[24h])) by (ipaddr) / sum(count_over_time(K_utilization[24h])) by (ipaddr)
This works for me.这对我有用。 I have the same question as OP on how to group the queries with avg_over_time promql function.
关于如何使用 avg_over_time promql 函数对查询进行分组,我有与 OP 相同的问题。 Applying the sum by after aws_over_time did it.
在 aws_over_time 之后应用总和。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.