Skip to content

Instantly share code, notes, and snippets.

@MartinLuksik
Last active March 29, 2022 08:13
Show Gist options
  • Save MartinLuksik/69eee7ec0a1221f5c701873b9db6d21f to your computer and use it in GitHub Desktop.
Save MartinLuksik/69eee7ec0a1221f5c701873b9db6d21f to your computer and use it in GitHub Desktop.
[PromQL] PromQL #promql
group (disk_used_percent{function=~".*"}) by (function)
#### CPU
## Num of cores that can be allocated on a node:
kube_node_status_allocatable{resource="cpu"})
## Num of cores that can be allocated by pool (expecting that the node have name e.g.: aks-system-27000824-vmss000000 where last 10 characters are ommited during grouping):
sum(label_replace(kube_node_status_allocatable{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool)
## Relative representation of the USED cpu from total allocatable CPU units per pool in %:
sum(label_replace(kube_pod_container_resource_requests{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool) / sum(label_replace(kube_node_status_allocatable{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool) * 100
## Relative representation of the FREE cpu from total allocatable CPU units per pool in %:
100 - (sum(label_replace(kube_pod_container_resource_requests{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool) / sum(label_replace(kube_node_status_allocatable{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool) * 100)
#### MEMORY
## LABEL REPLACE https://stackoverflow.com/questions/54235797/how-to-rename-label-within-a-metric-in-prometheus
Replace is not a true REPLACE
Your goal is to simply replace the old label name “old_job_id” with a new label name “new_task_id”. Prometheus label_replace will really “add” the new label name. It will preserve the old label name as well… So, that could be a problem, it’s not a true “replace in place”.
So if you want to “add” your new label name and “remove” the old label name, you need to do this:
sum without (old_job_id) (label_replace(metric, "new_task_id", "$1", "old_job_id", "(.*)"))
Here’s how this reads:
- sum without (old_job_id) will remove the old label name from the query output
- metric is your metric, like “node_filesystem_avail_bytes”
- “new_task_id” is where you would put your new label name
- “$1” is regex for using the string in new label name, don’t change this
- “old_job_id” is where you’ll put your old label, the one you want to get rid of (.*……. that mess is regex that will replace the whole label name
# example:
sum(label_replace(kube_node_status_allocatable{resource="memory"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool)
sum(label_replace(kube_node_status_allocatable{resource="cpu"}, "pool", "$1", "node", "(.*)-.{10}")) by (pool)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment