Search code examples
prometheuspromql

Is there a way to multiply Prometheus metrics, but persist label when one of the metrics misses a label?


Let's say I have 2 following metrics:

metric1{identifier="test", name="first"} 100
metric1{identifier="test", name="second"} 200

metric2{identifier="test", othername="third"} 2

I want to write a PromQL that will multiply the first metric with the second one on a specific label (identifier here), while persisting all the other labels in the resulting metric (name and othername here), so the result would be:

resultmetric{identifier="test", name="first", othername="third"} 200
resultmetric{identifier="test", name="second", othername="third"} 400

Simply multiplying it won't work, as these metrics have different set of labels.

How can I make this possible, if there's a way?


Solution

  • Try the following query:

    metric1 * on(identifier) group_left(othername) metric2
    

    It will multiply every metric1 metric by the metric2 metric with the same identifier label, while adding othername label from metric2 to the result.

    See these docs for more details.