24

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?

ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • May be because you have `/` in the regex. Try with something like `visits_total{route=~".*order.*"}` – prime Feb 21 '19 at 18:17

2 Answers2

48

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.*"}
prime
  • 14,464
  • 14
  • 99
  • 131
  • 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 '19 at 18:32
  • @ninesalt you might need to escape the `*` too. Check the updated answer. – prime Feb 21 '19 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 '19 at 18:45
  • I'd like to add a link to the docs: https://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 '19 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/.*.

Sam
  • 61
  • 1
  • 1