Regex for alerting rules expressions

2,833 views
Skip to first unread message

BlueChips23

unread,
Aug 9, 2018, 4:42:00 PM8/9/18
to Prometheus Users
Hi,

I have a Prometheus alerting rule like this:

alert: wrong_number_of_pods
expr: kube_deployment_status_replicas_available{namespace="abc"} != 2
for: 3m
annotations:
  description: 'Deployment: {{ $labels.deployment }} has wrong number of pods running.'
  summary: 'Deployment: {{ $labels.deployment }} has wrong number of pods running.'

I'm trying to apply this rule to two namespaces - "abc" and "def-xyz". Note that the second namespace has a hyphen in it's name.

I'm wondering if it is possible to use regex to specify both the namespaces - may be something like this: expr: kube_deployment_status_replicas_available{namespace="abc"|"def-xyz"} (although this exact expr doesn't work).



The alternate approach is to include the two namespaces in relabel-configs. Right now, my relabel-config looks like this:

- source_labels: [__meta_kubernetes_namespace] regex: abc action: keep

How should I change the regex above to include both "abc" and "def-xyz"? Is it like this?

- source_labels: [__meta_kubernetes_namespace] regex: abc | def-xyz action: keep

Thanks.

Christian Hoffmann

unread,
Aug 9, 2018, 4:46:53 PM8/9/18
to BlueChips23, Prometheus Users
On 8/9/18 10:42 PM, BlueChips23 wrote:
> I'm wondering if it is possible to use regex to specify both the
> namespaces - may be something like this: *expr:
> kube_deployment_status_replicas_available{namespace="abc"|"def-xyz"}* (although
> this exact expr doesn't work).
Sounds like this should do the trick?

kube_deployment_status_replicas_available{namespace=~"abc|def-xyz"}

Or do you mean some kind of range with the hyphen? If so, can you be
more concrete?

Kind regards,
Christian

Matt Doughty

unread,
Aug 9, 2018, 4:47:35 PM8/9/18
to BlueChips23, Prometheus Users
kube_deployment_status_replicas_available{namespace=~"(abc|xyz)"} should do what you want.

--Matt

--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/7fbfd432-6694-43cb-bbc1-5eeaabacf3d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--Matt

l.mi...@gmail.com

unread,
Aug 9, 2018, 4:49:02 PM8/9/18
to Prometheus Users
kube_deployment_status_replicas_available{namespace=~"(abc|def-xyz)"}

unless I'm missing something above is the query you want. Regex syntax is explained on https://prometheus.io/docs/prometheus/latest/querying/basics/

BlueChips23

unread,
Aug 9, 2018, 5:09:38 PM8/9/18
to Prometheus Users
kube_deployment_status_replicas_available{namespace=~"abc|def-xyz"} 

Gotcha. I was doing it wrong. I was trying kube_deployment_status_replicas_available{namespace="abc|def-xyz"}  without the ~. Thank you!

Meier

unread,
Aug 11, 2018, 2:19:46 PM8/11/18
to Prometheus Users
You could also do it much more generic:
expr: (kube_deployment_status_replicas_unavailable > 0) or (kube_deployment_status_replicas != kube_deployment_status_replicas_available) or (kube_deployment_status_replicas unless kube_deployment_status_replicas_available)

Reply all
Reply to author
Forward
0 new messages