25

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?

1
  • May be because you have / in the regex. Try with something like visits_total{route=~".*order.*"}
    – prime
    Feb 21, 2019 at 18:17

3 Answers 3

50

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.*"}
4
  • The first option worked but the second one gave an error. I'd rather just exclude docs than manually include everything I want.
    – ninesalt
    Feb 21, 2019 at 18:32
  • @ninesalt you might need to escape the * too. Check the updated answer.
    – prime
    Feb 21, 2019 at 18:39
  • 4
    Thanks. This worked. I didn't need to escape the *. I wanted to exclude two things so I just did it like this route!~".*docs.*|.*metrics.*"
    – ninesalt
    Feb 21, 2019 at 18:45
  • I'd like to add a link to the docs: prometheus.io/docs/prometheus/latest/querying/examples and I'd like to highlight that sentence: "All regular expressions in Prometheus use RE2 syntax."
    – Kai
    Aug 14, 2019 at 17:53
6

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/.*.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.