I want to multiply a metric with the outcome of the sum of another metric
What I want to try to do:
MeticOne * SUM(MetricTwo)
Thanks
I want to multiply a metric with the outcome of the sum of another metric
What I want to try to do:
MeticOne * SUM(MetricTwo)
Thanks
Suppose you have MetricOne with labels id and name. And you have MetricTwo with labels also id and name. Then you have the sum of values from MetricTwo aggregated by id using the metric:
sum without (name) (MetricTwo)
You can do a "join" in prometheus using the on(id) together with the group_left:
MetricOne
* on (id) group_left()
(sum without (name) (MetricTwo))
This is only possible if the label id from MetricOne is also id on the MetricTwo that you are joining. So, if they are different you may see an empty result query although there are rows to match!
Improvement:
Then it comes the time when you need to use label_replace to rename the label of one metric and make it match with the label of the other metric. Let's say that MetricOne with labels metric_one_id and name and we want to relabel it to id. You have to do this:
label_replace(MetricOne,"id","$1","metric_one_id", "(.+)")
Then you can use the same previous operator of before:
label_replace(MetricOne,"id","$1","metric_one_id", "(.+)")
* on (metric_one_id) group_left()
(sum without (name) (MetricTwo))
Regarding the group_left(), everything inside the group_left() will be excluded on the result. So in case you have more labels you might add them in the group_left() or on the without(). The Prometheus documentation explains the group_left operator here.