How to plot Loki parsed fields as numerical values using LogQL on Grafana?
Asked Answered
K

1

9

My setup: Loki: 2.1.0, Grafana: 6.7.3

My software runs on Kubernetes, Loki collects its logs. It looks something like this:

[2021-03-29 10:13:05] [INFO] Q_len=256 sol_q=0.049 info_q=0.240
[2021-03-29 10:13:05] [INFO] Q_len=196 sol_q=0.047 info_q=0.144 

I used logfmt in the logs, so loki can detect my fields: ![image|496x169](upload://vlvULKFu8DhvVZJb9qxURLMlSso.png)

Now I want info_q 's avg value plotted over time on Grafana. Here are the things I tried:

avg by (info) (avg_over_time({job="ism/ism-core-es"} | regexp `.*info_q=(?P<info_q>.*)` | unwrap info_q [1m]))

returns enter image description here

As the error message suggested, l did

avg by (info) (avg_over_time({job="ism/ism-core-es"} | regexp `.*info_q=(?P<info_q>.*)` | unwrap info_q | __error__="" [1m] ))

which returns empty chart. And this

avg_over_time(
{job="ism/ism-core-es"}
| regexp ".*info_q=(?P<info_q>.*?)"
| unwrap info_q [5m])

returns nothing either.

What am I doing wrong? Do I have to type cast? Any help is appreciated!

Katz answered 29/3, 2021 at 10:53 Comment(0)
K
4

As it turned out:

avg by (info) (avg_over_time( ({job="ism/ism-core-es"} |logfmt |unwrap info|__error__="")[1m]))

did the trick

Katz answered 24/11, 2021 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.