简体   繁体   中英

Prometheus Query to aggregate metrics by certain label values

I have my metrics exposed by Prometheus as:

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" .

I expect the result after querying to be something on lines of 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"

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 . Note that the required values for the label1 are enumerated in the regexp filter with | delimiter.
  2. It sums values for the selected time series individually per each requested timestamp (aka point on the graph) via sum aggregate function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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