<p>I'm trying to calculate the average time a pod stays in a pending state in grafana with prometheus. I can generate a graph to get the number of pods in a pending state over time, with this query</p>

<pre><code>sum(kube_pod_status_phase{phase="Pending"})
</code></pre>

<p>However, I would really like to get an the value of the average time that the pods are staying in this state in the last X hours. How can I do that?</p>

<p>PromQL provides <a href="https://prometheus.io/docs/prometheus/latest/querying/functions/#aggregation_over_time" rel="noreferrer">aggregation functions over time</a>.</p>

<p>You specify the <a href="https://prometheus.io/docs/prometheus/latest/querying/basics/#range-vector-selectors" rel="noreferrer">range of time</a> in the expression ; for X previous hours, it will be:</p>

<pre><code>avg_over_time(kube_pod_status_phase{phase="Pending"}[Xh])
</code></pre>
