简体   繁体   English

Prometheus Query 按特定标签值聚合指标

[英]Prometheus Query to aggregate metrics by certain label values

I have my metrics exposed by Prometheus as: Prometheus 将我的指标公开为:

custom_metric{label1="abc", label2="xyz"} num1
custom_metric{label1="def", label2="uvw"} num2
custom_metric{label1="ghi", label2="rst"} num3
custom_metric{label1="jkl", label2="opq"} num4

I want to query the metric such that I get sum of metric for label1="abc" , label1="def" and label1="jkl" .我想查询指标,以便获得label1="abc"label1="def"label1="jkl"的指标总和。

I expect the result after querying to be something on lines of custom_metric_groupped (num1 + num2 + num4) .我希望查询后的结果在custom_metric_groupped (num1 + num2 + num4)行上。

Another thing, in my use case, the number of specific label values can vary.另一件事,在我的用例中,特定标签值的数量可能会有所不同。 So, it might be the case that in future I might want to only take the sum for label1="def" and label1="jkl"因此,将来我可能只想取label1="def"label1="jkl"的总和

Try the following query:尝试以下查询:

sum(custom_metric{label1=~"abc|def|jkl"})

It works in the following way:它的工作方式如下:

  1. It selects time series matching label selector custom_metric{label1=~"abc|def|jkl"} - see these docs .它选择时间序列匹配标签选择器custom_metric{label1=~"abc|def|jkl"} - 请参阅这些文档 Note that the required values for the label1 are enumerated in the regexp filter with |请注意, label1所需的值在正则表达式过滤器中用|枚举。 delimiter.分隔符。
  2. It sums values for the selected time series individually per each requested timestamp (aka point on the graph) via sum aggregate function.它通过sum聚合函数对每个请求的时间戳(也称为图表上的点)分别对所选时间序列的值求和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM