Prometheus - Measure time a container has been running in Grafana Time Range for dashboard

5/27/2021

I am trying to get the time that a certain container/pod has been running in the time range interval from the grafana dashboard in kubernetes. Taking into account that the pod can be stopped and started with the same name.

Based on: https://stackoverflow.com/questions/64309828/how-to-create-query-to-monitoring-how-many-minutes-docker-containers-ran-for-a-d

I was able to test the following query:

count_over_time(
  (changes(container_last_seen{namespace="dslab", pod=~"$user"}[90s])>0)
  [<interval>:90s]
)*90

But it doesn't work because the pod is restarted with the same name

-- Javi Hernandez
docker
grafana
kubernetes
prometheus

1 Answer

5/27/2021

The following solution works.

In prometheus this query:

(kube_pod_container_status_running{pod=~"$pod_name"} > 0) * ($__interval_ms / 1000)

$__interval_ms is the time in ms for the time range selected in the dashboard

And then in the grafana transfromation option: group by and calculate Total or Count:

enter image description here

-- Javi Hernandez
Source: StackOverflow