prometheusgrafanapromql

PromQL aggregate with the max value in another metric


I would like aggregate a metric with the maximum number of another metric.

Metric 1:

0:00 0:01 0:02
Instance 1 1 1 1
Instance 2 2 2 2
Instance 3 3 3 3

Metric 2:

0:00 0:01 0:02
Instance 1 1 12 23
Instance 2 1 14 14
Instance 3 3 3 3

For example, the aggregated result from Metric 1 is (3,2,1).

The result using

max(metric2) 

is (3,14,23) and it can be mapping to (instance3, instance2, instance1). I would like to apply this result's instance back to metric1.


Solution

  • Query

    metric1 and on(instance) topk(1, metric2)
    

    will return data as you want it.

    Here: