Search code examples
kubernetesprometheusgrafanametricspromql

PromQL: reuse group label in another metric


I have multiple pods controlled by a workflow. To retrieve the controlling workflow name I have the following query:
group by(owner_name)(kube_pod_owner{owner_kind="Workflow"}).

Now I want to reuse the owner_name field to measure the CPU usage of the pods controlled by the grouped workflows.
For this I think about something like this: container_cpu_usage_seconds_total{pod=~"${owner_name}-.*"}.

How do I combine those two statements? Is that even possible?


Solution

  • According to: How do I group pod metrics by deployment in Prometheus?
    Try something like this:

    (
        sum by (pod) (label_replace(
            (
                rate(
                    container_cpu_usage_seconds_total{image!=""}[2m]) * on(pod) group_left(owner_name) 
                    (sum without (instance) (kube_pod_owner{owner_kind="Workflow"}))
                ), 
                "pod", "$1", "owner_name", "(.*)"
            )
        )
    )