Search code examples
grafanagrafana-variablegrafana-lokipromtaillogql

Use variables on grafana loki queries (LogQL)


I have succesfully installed Loki on a server and Promtail on multiple hosts of my datacenter. Each host runs a different number of tomcats, like tomcat10, tomcat11...tomcat20.

So on promtail, my job gets a label with the hostname and __path__ is something like work/java/tomcat*/logs*.

Now I add on a Postgre the different appnames related to those tomcats, and the hosts and its names.

But I am struggling with the LogQL query on Grafana to access to those variables. For example, I define in Grafana the following variable named varHost

select server __value, server __text from loki_promtail.servers order by id ASC

Let's say I want to access to every log on the Host Saturn. It's easy using {job="saturn"}, but instead of Saturn (which is the name of a host), I would like to use ${varHost} for doing so, so the same panel can show multiple host info.

On the same way, lets suppose I'm trying to access to /work/java/tomcat10/logs/catalina.out. I am expecting to be able to concat the varApp in the LogQL query, so, instead of {filename="/work/java/tomcat10/logs/catalina.out"} I would like something like {filename="/work/java/tomcat${varApp}/logs/catalina.out"}, which I'm doing with a regex {filename=~"/work/java/tomcat${varApp}/logs/catalina.out"} but fails when selecting all in the ${varApp} in Grafana.

How could I achieve that?

Thank u id advance!


Solution

  • Select the filename with the following LogQL:

    {filename=~"/work/java/tomcat(${varApp:pipe})/logs/catalina.out"}
    

    The ":pipe" modifier will change "$varApp" to 10|11|20, for example.

    More info in the Grafana documentation here.