We are having a Kubernetes cluster and using Prometheus + Grafana for monitoring and alerting. We need to show a panel on Grafana that shows us the view (same as kubectl get namespaces) . Currently we are able to get name and status column using the below PROMQL along with Hide options in Visualization section of Grafana.
count(kube_namespace_status_phase) by (phase, namespace)
But we also want to find the AGE from when a namespace was active/created. We are not able to find AGE in any of the 4 kube metrics of namespace available -
Any suggestions would be helpful.
Unfortunately as you already noticed there is no specific metric that could be used to calculate the age of an object. The closest thing that you could use to achieve your goal would be to use kube_namespace_created
which shows at what time namespace in Kubernetes was created.
I was also not able to find a proper Prometheus operator/function in order to make some kind of workaround PROMQL.
I am posting this answer as a community wiki. Feel free to expand on it as you wish.
I hope it helps.
I have seen on other metrics regarding age that you need to multiply by 1000
So if you do;
kube_namespace_created * 1000
You would need to check/test but I've seen that used on other queries about finding the age of worker nodes.
The following query returns the age of every Kubernetes namespace in seconds:
time() - kube_namespace_created
It uses time() function, which returns the current time in seconds.