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.
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
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: