You can use the argument list of group_left to include extra labels from the right operand (parentheses and indents for clarity):
(
max(consul_health_service_status{status="critical"})
by (service_name,status,node) == 1
)
+ on(service_name,node) group_left(env)
(
0 * consul_service_tags
)
The important part here is the operation + on(service_name,node) group_left(env):
- the
+is "abused" as a join operator (fine since0 * consul_service_tagsalways has the value 0) group_left(env)is the modifier that includes the extra labelenvfrom the right (consul_service_tags)
You can use the argument list of group_left to include extra labels from the right operand (parentheses and indents for clarity):
(
max(consul_health_service_status{status="critical"})
by (service_name,status,node) == 1
)
+ on(service_name,node) group_left(env)
(
0 * consul_service_tags
)
The important part here is the operation + on(service_name,node) group_left(env):
- the
+is "abused" as a join operator (fine since0 * consul_service_tagsalways has the value 0) group_left(env)is the modifier that includes the extra labelenvfrom the right (consul_service_tags)
It is a good practice in Prometheus ecosystem to expose additional labels, which can be joined to multiple metrics, via a separate info-like metric as explained in this article. For example, consul_service_tags metric exposes a set of tags, which can be joined to metrics via (service_name, node) labels.
The join is usually performed via on() and group_left() modifiers applied to * operation. The * doesn't modify values for time series on the left side because info-like metrics usually have constant 1 values. The on() modifier is used for limiting the labels used for finding matching time series on the left and the right side of *. The group_left() modifier is used for adding additional labels from time series on the right side of *. See these docs for details.
For example, the following PromQL query adds env label from consul_service_tags metric to consul_health_service_status metric with the same set of (service_name, node) labels:
consul_health_service_status
* on(service_name, node) group_left(env)
consul_service_tags
Additional label filters can be added to consul_health_service_status if needed. For example, the following query returns only time series with status="critical" label:
consul_health_service_status{status="critical"}
* on(service_name, node) group_left(env)
consul_service_tags
Joining two metrics - PromQL - Prometheus Monitoring System
Prometheus - How to “join” two metrics and calculate the difference?
How to combine two metrics into one in Prometheus
Merge/join two metrics in Prometheus/PromQL - Stack Overflow
I am new to Prometheus metrics and I still struggle to see the right way to do things.
I recently discovered that the job I created had one target that was reading two instances metrics. That means that I had two sequences merged into one and the counter was going up and down.
After a question at Stackoverflow I changed the job to
- job_name: 'example'
scrape_interval: 30s
params:
-instance: ['372c43937e6acd56073246ffdf0e95597480cdba24688cc331d77a8c3c07e1a9','be007691832f4002a86cf0148768a3b039304e9b1fa7ae91a032b0beddc152b2']
static_configs:
- targets: ['example.com']
metrics_path: /metrics
scheme: httpsThat way I get two sequences of metrics within the same job name.
Is there anything that can group these two sequences into one?
I know that I can query the Prometheus with a sum and combine them in every query, but I would like to simplify this and query them as a single sequence.
{__name__=~"metric_1|metric_2"}
https://stackoverflow.com/a/47415934/661150 https://prometheus.io/docs/prometheus/latest/querying/basics/
As a workaround, you can append + 0 to the query so that Prometheus strips out its __name__ label so Grafana will merge them together. I found this workaround on GitHub.
Is there a better solution?