I'm building a Prometheus query (PromQL) to fetch metrics data for monitoring a custom metric on my servers. Some servers may be down at times, and I want to always include such servers in my monitoring output.
I have 2 separate queries for pulling information, like below:
Query 1:
up{instance=~"localhost:.+"}
Output 1: The value here indicates that the node is up or down.
Element | Value |
---|---|
up{instance="localhost:8080",job="prometheus",monitor="fav-monitor"} | 1 |
up{instance="localhost:8081",job="prometheus",monitor="fav-monitor"} | 0 |
Query 2:
my_node{instance=~"localhost:.+", job="prometheus"}
Output 2:
Element | Value |
---|---|
my_node{instance="localhost:8080",job="prometheus",monitor="fav-monitor",name="mynode-node1"} | 25 |
I would finally like to obtain my output as below, please help.
Element | Value |
---|---|
{instance="localhost:8080",job="prometheus",monitor="fav-monitor",name="mynode-node1"} | 25 |
{instance="localhost:8081",job="prometheus",monitor="fav-monitor",name="mynode-node2"} | 0 |
NOTE: It is preferable to be able to generate a name for localhost:8081, as in the expected output above, but an empty name would work as well.
I was able to get the required output with the below query. Improvements and suggestions welcome.
(
up{instance=~"localhost:.+"}
)
+ on(instance) group_left(name)
(
my_node{instance=~"localhost:.+", job="prometheus"}
)
or
(
label_replace(up{instance=~"localhost:.+"}, "name", "mynode-nodex", "", "") == 0
)
+ on (instance) group_left(name)
(
label_replace(up{instance=~"localhost:.+"}, "name", "mynode-nodex", "", "") == 0
)