2

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!

user2087103
  • 141
  • 2
  • 13

1 Answers1

4

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.

  • 1
    Thank u! That was awesome, didn't know about that pipe option! – user2087103 Nov 10 '21 at 09:28
  • Could you help me on the other problem I'm facing? I don't know why I do get a %0A (Line Feed) at the end of the query. This is my query: `{job=~"${varServer}"}` and this is the error I'm facing on the query inspector `"api/datasources/proxy/13/loki/api/v1/query_range?direction=BACKWARD&limit=1000&query=%7Bjob%3D~%22Saturn%0A%22%7D&start=1636515957302000000&end=1636537557302000000&step=15"` I tried that without the %0A and works like a charm! That LF char, is it a thing of the DB or is it related to how I'm coding the variable in the query? – user2087103 Nov 10 '21 at 09:47
  • Great, could you accept the answer? I don't know about the %0A issue, are you using Windows? – Marcelo Ávila de Oliveira Nov 10 '21 at 10:32
  • Accepted! Both Grafana and Postgre are installed on CentOS7, not Windows! – user2087103 Nov 10 '21 at 10:53
  • 1
    Was a data problem on DB, there were EOL on those variables. Changed them and now works like a charm!! Thank u! – user2087103 Nov 10 '21 at 11:34