Logical or condition between multiple labeld

29 views
Skip to first unread message

Anoop

unread,
Mar 3, 2022, 11:32:32 AM3/3/22
to Prometheus Users
Hi,

Is there anyway to write a query with "or" condition in prometheus to check if any of the label is present. 

For eg: I would like to write a query something like below.

container_cpu_usage_seconds_total{cluster="abc" or cluster=~"xyz"}

Is there anyway to do this?

Thanks,

Brian Candler

unread,
Mar 3, 2022, 11:45:49 AM3/3/22
to Prometheus Users
Yes, and you've almost answered it yourself :-)

container_cpu_usage_seconds_total{cluster=~"abc|xyz"}

Anoop Mohan

unread,
Mar 3, 2022, 11:56:56 AM3/3/22
to Brian Candler, Prometheus Users
ah..  Sorry. There was a typo in my previous email. Actually I need to add the or condition with different labels, like below

container_cpu_usage_seconds_total{container="prometheus", cluster="abc" or environment=~"xyz"}

Thanks,


Thanks & Regards,

Anoop Mohan



--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/45ae4649-bfc6-49c3-854b-cedeea668e1en%40googlegroups.com.

Brian Candler

unread,
Mar 3, 2022, 12:16:56 PM3/3/22
to Prometheus Users
I think you can do that by merging two instant vectors:

(container_cpu_usage_seconds_total{container="prometheus", cluster="abc"} or container_cpu_usage_seconds_total{container="prometheus", environment=~"xyz"})

A missing label also matches as if it were empty string.  So if your metrics have *either* cluster *or* environment label (but not neither or both), you could do this:

container_cpu_usage_seconds_total{container="prometheus", cluster=~"abc|",environment=~"xyz|"}

And there is really horrible stuff you can do with label_join() or label_replace().

But if you need to do any of those tricks, then something isn't right. You should consider:
- making your labels consistent, such that filtering on just cluster or just environment gives you what you need
- whether there's a better way to label your metrics, e.g. add a new common label dimension (either statically or via relabelling);
- whether you should merge the results elsewhere, e.g. in your dashboard software

Anoop Mohan

unread,
Mar 3, 2022, 11:06:30 PM3/3/22
to Brian Candler, Prometheus Users
Thank you Brian for your help. It worked.

Regards,
Anoop

Reply all
Reply to author
Forward
0 new messages