'how agreggate all results into 1 average from a query in prometheus promql
I have a kubernetes cluster running many apps. 1 pod per 1 namespace is considered an app. only 1 pod (the app pod) runs per namespace. an example looks like this:
(Note that id<x> is a complete random string, so id1 it's not what an id looks like)
namespace: app-id1, only-pod-running-in-this-namespace: app-id1
namespace: app-id2, only-pod-running-in-this-namespace: app-id2
namespace: app-id3, only-pod-running-in-this-namespace: app-id3
namespace: app-id4, only-pod-running-in-this-namespace: app-id4
The list goes on and on. I am trying to get the uptime for each app, by checking the pod status. In prometheus, I am doing it like this:
kube_pod_status_ready{condition="true", namespace=~"app-.*", pod=~"app-.*"}
this returns a table with all existing apps (each record is 1 app status), and their uptime value (1 if the pod is up, 0 is the pod is down).
Now I want to create another metric, that returns the average for all apps combined. This is, I want only 1 record returned with the average for all apps. So, let's say I have 100 apps, then if 1 went down for 5 min, I want that 5 min window to show 99 (or 0.99 actually) as a result, instead of 99 apps showing 1, and 1 app showing 0. I hope that make sense. This is how I am trying, but it's not working, as it's returning a table with 1 record per app.
avg_over_time(kube_pod_status_ready{condition="true", namespace=~"app-.*", pod=~"app-.*"}[5m])
Solution 1:[1]
My understanding is you want all your app instances' up percent? is kube_pod_status_ready{condition="true", namespace=~"app-.*", pod=~"app-.*"} return 1 when up and 0 when down?If so,may be a sum(kube_pod_status_ready{condition="true", namespace=~"app-.*", pod=~"app-.*"}) / count(kube_pod_status_ready{condition="true", namespace=~"app-.*", pod=~"app-.*"}) can do?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xiang.Bao |
