Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grafana Loki query with regex interpolation for multiple choice variable

In Grafana Dashboards settings, I created a variable $logfile based on a query (label_values(filename) from Loki) to be able to select multiple filenames. These filenames sometimes contain dots (sub extensions).

In the query of a Logs Panel, I try to interpolate the values selected in a regex expression:

{filename=~".*\\/${logfile:regex}\\.log"}

The regex interpolation described below indicates the special characters are escaped once with this notation: https://grafana.com/docs/grafana/latest/variables/advanced-variable-format-options/

But the regex part of the select query requires a double backslash to be correctly escaped (so when I select options such as "file1" or "file2", it works, but with "file3.ext" or "file4.ext", it won't).

How to achieve this?

Thank you,

like image 758
Corentin Avatar asked Oct 23 '25 15:10

Corentin


2 Answers

I got help from another developer who figured it out.

The key is to use backquotes:

{filename=~`.*\/${logfile:regex}\.log`}

Then it works!

Hoping it'll be useful for anyone searching

like image 104
Corentin Avatar answered Oct 26 '25 17:10

Corentin


I use the following to search for multiple values in Loki.

|~ "(string1|string2)"

OR

{namespace="name", container="service"} |~ "(string1|string2)"
like image 39
Sohail Ahmed Avatar answered Oct 26 '25 17:10

Sohail Ahmed