I'm trying to write a prometheus query in grafana that will select visits_total{route!~"/api/docs/*"}

What I'm trying to say is that it should select all the instances where the route doesn't match /api/docs/* (regex) but this isn't working. It's actually just selecting all the instances. I tried to force it to select others by doing this:visits_total{route=~"/api/order/*"} but it doesn't return anything. I found these operators in the querying basics page of prometheus. What am I doing wrong here?

3

Best Answer


May be because you have / in the regex. Try with something like visits_total{route=~".*order.*"} and see if the result is generated or not.

Try this also,

visits_total{route!~"\/api\/docs\/\*"}

If you want to exclude all the things that has the word docs you can use below,

visits_total{route!~".*docs.*"}

The main problem with your original query is that /api/docs/* will only match things like /api/docs and /api/docs//////; i.e. the * in your query will match 0 or more / characters.

I think what you meant to use was /api/docs/.*.

PromQL regular expressions follow RE2 syntax. As per this using * is tricky, it is considered as x* (where x is last non-empty character preceeding *). Now as per the table x* (* implies x*) and hence the match will be on zero or more occurance of x (in your case /). It is always safe to use ".*" or ".+" unless you intentionally want to match specific character zero or more occurances. More details belowenter image description here