How to Conditionally Apply a Dashboard Variable in LokiQL?

I have a shared library pane that displays logs used across multiple dashboards. This setup has been beneficial since any changes to the query are automatically reflected across all dashboards.

Challenge:

I’ve cloned one of these dashboards and want to add the ability to filter specific logs dynamically, using a dashboard variable as a text input. However, this filter variable is only present on some of the dashboards.

Is there a way in LokiQL to check if a variable is defined and then apply its value? If the variable isn’t defined, I’d like to either ignore the filter or use a default value.

For example:

{app=~"$app", cluster=~"$cluster"} 
|~ `"level":"${level:regex}"`
|~ "${filter:.*}"
| json 
. . .

In this example:

  • The level variable is always defined, so the query works as expected.
  • The filter variable, however, is not always defined across all dashboards using this pane.

Question:

Is there a more efficient way in LokiQL to handle this situation, rather than creating a hidden variable in every dashboard that imports this shared pane? How can I make the query conditional on whether the filter variable is present?

Thank you!

There is indeed a default filter in template function, see Template functions | Grafana Loki documentation.

Ah! neat. I have heard about it but was not too sure how it was used so was getting the syntax wrong :man_facepalming:

Thanks!