2

I have Grafana 5.2 dashboards sourcing data from Prometheus.

I have some labels in a dashboard that seem to be in the format *.<domain> for e.g. *.google.com e.t.c however, this doesn't play with Grafana without some smart regex to ignore the first two characters.

I have the following regex (?<=^\*\.|^)[-a-zA-Z0-9._ ]+ which doesn't seem to work in Grafana but works in regex101. It should result in the label as google.com i.e. without the first two characters *..

Without regex

With regex

Can someone please let me know what causes this ?

nixgadget
  • 6,983
  • 16
  • 70
  • 103
  • 1
    Try using a capturing group - `^(?:\*\.)?([-a-zA-Z0-9._ ]+)` – Wiktor Stribiżew Sep 17 '18 at 08:50
  • I assume this is because Grafana doesn't support lookbehinds. You could try using `\K` to reset the match `^(?:\*\.)?\K[-a-zA-Z0-9._ ]+`. [regex101](https://regex101.com/r/eG8uP2/5). – Addison Sep 17 '18 at 08:57
  • Also, the placeholder in that field suggests regex delimiters, and if they are required, try `/^(?:\*\.)?([-a-zA-Z0-9._ ]+)/`. Or, if it is PCRE/Boost/Onigmo powered, use `/^(?:\*\.)?\K[-a-zA-Z0-9._ ]+/` – Wiktor Stribiżew Sep 17 '18 at 09:23
  • @WiktorStribiżew your first suggestion seems to have worked. I guess Grafana look* type regex. Thanks for the tip. Also feel free to add it as an answer so i can mark it – nixgadget Sep 17 '18 at 09:28

1 Answers1

3

According to Grafana documentation, you may capture the part of a regex to return that substring:

Filter and modify the options using a regex capture group to return part of the text: Regex:

/.*(01|02)/

Result:

01
02

Hence, you may use

^(?:\*\.)?([-a-zA-Z0-9._ ]+)
          ^                ^

See the regex demo.

Here,

  • ^ - start of a string
  • (?:\*\.)? - an optional (due to ? quantifier that matches 1 or 0 sequences) non-capturing group that matches a *. substring (1 or 0 times)
  • ([-a-zA-Z0-9._ ]+) - a capturing group that matches 1+ ASCII letters, digits, -, ., _ and space and places its matched value into Group 1 and returns it in Grafana as a result of a match.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563