Search code examples
variablesgrafanagrafana-loki

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,


Solution

  • 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