Filter query based on result from first query.

32 views
Skip to first unread message

Laurent Dumont

unread,
Aug 6, 2021, 3:20:13 PM8/6/21
to Prometheus Users
Hi!

Trying to wrap my brain around the group_left / group_right in PromQL.

I have the following metrics (removed some labels)

potato{short_hostname="qasite1-compute001",site="qasite1",hardware="hardwaretype1"} 1
potato{short_hostname="qasite1-compute001",site="qasite1",hardware="hardwaretype2"} 1
carrot{short_hostname="qasite1-compute001",site="qasite1", type="running_carrots"} 0
carrot{short_hostname="qasite1-compute001",site="qasite1", type="running_carrots"} 5

The goal is to "merge" these two metrics to allow some cross queries where
  • our common label is short_hostname
  • for all short_hostname values, I want to check the value of the "hardware" label and get the value of the "carrot" metric only if the label "hardware" in "potato" is equal to "hardwaretype1"
  • Furthermore, I want to also validate that the resulting metric value of "carrot" is equal to 0 (not a label in this case)
It seems that there is no easy to merge the two queries and create a single result that could be further sorted in Grafana/other stuff?

Thanks!

Bjoern Rabenstein

unread,
Aug 13, 2021, 8:39:29 AM8/13/21
to Laurent Dumont, Prometheus Users
On 06.08.21 15:19, Laurent Dumont wrote:
>
> I have the following metrics (removed some labels)
>
> potato{short_hostname="qasite1-compute001",site="qasite1",hardware="hardwaretype1"} 1
> potato{short_hostname="qasite1-compute001",site="qasite1",hardware="hardwaretype2"} 1
> carrot{short_hostname="qasite1-compute001",site="qasite1",type="running_carrots"} 0
> carrot{short_hostname="qasite1-compute001",site="qasite1",type="running_carrots"} 5
>
> The goal is to "merge" these two metrics to allow some cross queries where
>
> - our common label is short_hostname

Which means you have to specify `on(short_hostname)` after the binary
operator.

This might get you into cases where there is no unique match
possible. In this case, you add `group_left` or `group_right` to mark
the side with the higher cardinality.

If both sides have a cardinality of more than one for that label, then
no match on just that label is possible.

In your example above, `potato` has cardinality 2. The two lines for
`carrot` are actually for the same metric. (They have exactly the same
label set.)

> - for all short_hostname values, I want to check the value of the
> "hardware" label and get the value of the "carrot" metric only if the label
> "hardware" in "potato" is equal to "hardwaretype1"

That's actually easy:

carrot and on(short_hostname) potato{hardware="hardwaretype1"}

> - Furthermore, I want to also validate that the resulting metric value
> of "carrot" is equal to 0 (not a label in this case)

carrot==0 and on(short_hostname) potato{hardware="hardwaretype1"}

--
Björn Rabenstein
[PGP-ID] 0x851C3DA17D748D03
[email] bjo...@rabenste.in
Reply all
Reply to author
Forward
0 new messages