Extract number from log line (json)

Hi there, i am starting to understand how to display data in grafana. i have a log comming in from loki where i need to extract a string from a json value.

for now i got this far:

{container_name=~".*selfs.*"} |= has completed in | json | line_format "{{.msg}}"

this gives me a log line like this for example:

Job Nr: 6, ID: 4t9UV3ngr5_0mu4toV8UB has completed in 46.05699282288551 seconds!

what do i need to do t just get the seconds out of this? i played around with pattern and regex, but nothing seems to work.

is this possible?

Thank you so much,

pcace

You need parser, e.g. pattern

1 Like

but in this case:

Job Nr: 18, ID: DOpEoCQh0UCj7Pop6SBpZ has completed in 64.88847192001343 seconds!

how would a pattern look like to extract seconds? I am really having trouble to understand the documentation there.

i tried variations of this:

{container_name=~".*selfs.*"} 
|= `has completed in` #
| pattern `<_>has completed in <seconds>` 
| line_format "seconds = {{.seconds}}"

or this:

{container_name=~".*selfs.*"} 
|= "has completed in" 
| pattern `Job Nr: <_>, ID: <_> has completed in <seconds> seconds` 
| line_format "seconds = {{.seconds}}"

but without any success

Thanks a lot

1 Like

With regex is should be like this:

{container_name=~".*selfs.*"} 
|= "has completed in" 
| regexp "has completed in (?P<seconds>[\\d\\.]*) seconds" 
| line_format "seconds = {{.seconds}}"

A pattern requires to match the full line, it looks like you missed the ! at the end of the line.

Hi, thank you so much for your reply. I just cannot get my head around why this is working.
why is \d\ not the literal caracter ā€œdā€ ? like here: regex101: build, test, and debug regex

or what kind of regex is grafana using here?

Thanks a lot for help!

Grafana uses the Golang RE2 syntax, see

\d for digits
\. to match the dot as well

But you have to escape an \, therefor the double \\

I updated your regex, see https://regex101.com/r/M5ATsH/2