Wildcard in PromQL "5.+" vs "5\\d{2}"

160 views
Skip to first unread message

Jason

unread,
Oct 3, 2023, 4:09:52 AM10/3/23
to promethe...@googlegroups.com
Hi

I will write my query like this (with * wildcad)

sum(http_requests_total{status_code=~"5.+"})

In internet I found this syntax \\d{2}

sum(http_requests_total{status_code=~"5\\d{2}"})

What is this? Where to find more info?
Why I should use the 2nd query and not the first?

cheers
Jason
OpenPGP_0x0D0C34B5DF58FE9D.asc
OpenPGP_signature.asc

Stuart Clark

unread,
Oct 3, 2023, 4:55:17 AM10/3/23
to Jason, promethe...@googlegroups.com
In reality both will do the same thing, although the second is
technically more correct.

The first regular expression is matching "5" followed by 1 or more other
characters, while the second is matching "5" followed by exactly 2
numbers. So the first one would also match "50" or "5frogs" which aren't
valid status codes, but in reality your application would have to be
having serious problems to be setting those values anyway.

--
Stuart Clark

Jason

unread,
Oct 3, 2023, 6:03:58 AM10/3/23
to promethe...@googlegroups.com
On 03.10.2023 10:55, Stuart Clark wrote:
> In reality both will do the same thing, although the second is
> technically more correct.
>
> The first regular expression is matching "5" followed by 1 or more other
> characters, while the second is matching "5" followed by exactly 2
> numbers. So the first one would also match "50" or "5frogs" which aren't
> valid status codes, but in reality your application would have to be
> having serious problems to be setting those values anyway.

Thanks Stuart

Is there any docs about this type of regex? I didn't found "\\d" in
official docs.

How is this regex called?

cheers
Jason
OpenPGP_0x0D0C34B5DF58FE9D.asc
OpenPGP_signature.asc

Julius Volz

unread,
Oct 8, 2023, 8:49:08 AM10/8/23
to Jason, promethe...@googlegroups.com
Hi Jason,

Prometheus uses the RE2 regular expression dialect, you can find the documentation about that here: https://github.com/google/re2/wiki/Syntax

The double backslash ("\\") is just to escape the one backslash in the Prometheus string. So the actual regex is just "5\\d{2}". You can remove the escaping by using backticks as quotes, which do not interpret escape sequences: `5\d{2}`

Kind regards,
Julius

--
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/988b8358-d588-4120-8768-d8c7f29fe8f5%407748229.xyz.


--
Julius Volz
PromLabs - promlabs.com
Reply all
Reply to author
Forward
0 new messages