Extract Loki fields into Grafana variables (workaround)

I recently ran into an issue while trying to use Loki fields as variables on a Grafana dashboard. Specifically, I could only extract Label names or Label values, which wasn’t exactly what I needed.

After some experimentation, I found a workaround that I thought might be useful for others facing a similar issue. The solution involved using the yesoreyeram-infinity-datasource plugin to directly query Loki, and then parsing the necessary fields.

Here’s how I did it:

  1. In the URL Query Params section, I specified a query that would return a single entry for each field value. In my case, the query was:
    count(count_over_time({kind="event", env="live"} | logfmt [1h])) by (view_name)
    This query aggregates logs from a specific view_name over one hour.

  2. The result was a list of values that I could use as variables for my dashboard:

  3. Finally, I was able to parse them using UQL and use these values in my dashboard.

The result was exactly what I needed, and I hope this helps others who might run into the same challenge!

1 Like