Addition and division in metrics

Hi!

Have a simple math expression:
(metric_name1{env="prod", status="1"} + metric_name1{env="prod", status="2"}) / metric_name1{env="prod", status="3"}

But all what I have as result is n/a
Is it possible to do some math expression with the same metric in prometheus?
or I’m doing something illigal

Hello akoto and welcome to Grafana community! :tada:

Regarding your question - your arithmetic operation would work, if all labels were exactly the same. Check out my example on play.grafana (probably not the best example, but you get the point) :slightly_smiling_face:. In my case it works, because I’ve used labels that are exactly same job="alertmanager", instance="demo.robustperception.io:9093". However, your labels aren’t the same, therefore you need to decide which labels are going to be considered. You can either use ignoring clause to ignore certain labels (example) or on clause to consider only labels that you specify (example). However, keep in mind that all labels that are not used for the comparison are going to be thrown away. If you would like to keep all labels from the left, you can do that by adding group_left after the on/ ignoring clause. In this case, the value of the right side will be applied on each of the left side labels that match the labels from on . There is also a group_right.
For more info/context, definitely check out this amazing Introduction to PromQL blog or Prometheus docs. :slightly_smiling_face:

1 Like

thanks for answering!
I found another resolution:
sum(metric_name1{env=“prod”, status=“1|2”}) / metric_name1{env=“prod”, status=“3”}

1 Like

I found another resolution:
sum(metric_name1{env=“prod”, status=“1|2”}) / metric_name1{env=“prod”, status=“3”}

This query won’t work because the result on the left side of / doesn’t contain any labels, while the result on the right side contains at least env and status labels. This should prevent from proper matching of time series according to vector matching rules. The fix is to add on() after /:

sum(metric_name1{env=“prod”, status=“1|2”}) / on() metric_name1{env=“prod”, status=“3”}