<p>I have run into a bit of a trouble for what is seems to be an easy question. </p>

<p>My scenario:
I have a k8s job which can be run at any time (not a cronJob) which in turn creates a pod to perform some tasks. Once the pod performs its task it completes, thus completing the job that spawned it.</p>

<p>What I want:
I want to alert via prometheus if the pod is in a running state for more than 1h signalling that the task is taking too much time.
I'm interested to alert ONLY when duration symbolised by the arrow in the attached image exceeds 1h. Also have no alerts triggered when the pod is no longer running.<a href="https://i.stack.imgur.com/AUC1S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AUC1S.png" alt="enter image description here"></a></p>

<p>What I tried:
The following prometheus metric, which is an instant vector that can be either 0(pod not running) or 1(pod is running):</p>

<pre><code>kube_pod_status_ready{condition="true",pod_name=~".+POD-A.+"}
</code></pre>

<p>I figured I tried to use this metric with the following formula for computing the duration for when the metric was one during a day</p>

<pre><code>(1 - avg_over_time(kube_pod_status_ready{condition="true",pod_name=~".+POD-A.+"}[1d])) * 86400 &gt; 3600
</code></pre>

<p>Because these pods come and go and are not always present I'm encountering the following problems:</p>

<ul>
<li>The expr above starts from the 86400 value and eventually drops once
the container is running this would trigger an alert</li>
<li>The pod eventually goes away and I would not like to send out fake alerts for pods which are no longer running(although they took over 1h to run)</li>
</ul>

<p>Thanks to the suggestion of @HelloWorld i think this would be the best solution to achieve what I wanted:</p>

<pre><code>(sum_over_time(kube_pod_status_ready{condition="true",pod_name=~".+POD-A.+"}[1d:1s]) &gt; 3600) and (kube_pod_status_ready{condition="true",pod_name=~".+POD-A.+"}==1)
</code></pre>

<ul>
<li>Count the number of times pods is running in the past day/6h/3h and verify if that exceeds 1h(3600s)
AND </li>
<li>Check if the pod is still running - so that it doesn't take into consideration old pods or if the pod terminates.</li>
</ul>
