Splunk Search

How to extract integer value in search from string JSON in log event

soshua
New Member

I am trying to extract 'timeTaken' value from json inside a log event string in order to build a dashboard.

Example log value:

2020-02-12 17:50:15.228  INFO 1 --- [io-8080-exec-45] c.m.v.c.RequestInterceptor         : {"logType":"RESPONSE","traceId":"03927a51-23d6-4530-a0e6-112b6d4b5539","timestamp":"Feb 12, 2020 5:50:15 PM","requestMethod":"GET","requestUrl":"http://my.url","responseStatus":500,"timeTaken":28}

Search example :

index = "my_cluster_name" "kubernetes.labels.app"=my.app | spath log | search log="*"timeTaken"" | rex field=_raw ""timeTaken":(?<timeTaken>.*)}" | timechart span=60s avg(timeTaken)

I also tried:

index = "my_cluster_name" "kubernetes.labels.app"=my.app | spath log | search log="*"timeTaken"" | rex field=_raw "timeTaken\":(?<timeTaken>.*)}" | timechart span=60s avg(timeTaken)

It appears that the value for timeTaken is not populated. I would be grateful for any guidance. Thanks!

0 Karma

soshua
New Member

Solution:
index = "my_cluster_name" "kubernetes.labels.app"=my.app | spath log | search log="*"timeTaken"" | rex field=log "(?<time>(?<=timeTaken\":)(.\d?))" | timechart span=60s avg(time) as avgTime

0 Karma

to4kawa
Ultra Champion

have you try my answer?

0 Karma

to4kawa
Ultra Champion
index = "my_cluster_name" "kubernetes.labels.app"=my.app "timeTaken"
| rex "timeTaken.*?(?<timeTaken>\d+)" 
| timechart span=60s avg(timeTaken)

spath is not need.

somesoni2
Revered Legend

Your log is not in pure json format (some header text are available before json starts). Give this a try (extracting json part and running spath on that)

index = "my_cluster_name" "kubernetes.labels.app"=my.app  | rex "^([^\{]+)(?<jsonpart>.+)" | spath input=jsonpart | timechart span=60s avg(timeTaken)

soshua
New Member

Still doesn't work.

timechart span=60s avg(timeTaken) -- is this how I would reference jsonpart.timeTaken?

I tried timechart span=60s avg(timeTaken) as well as timechart span=60s avg(jsonpart.timeTaken)

0 Karma

Esky73
Builder

you timechart should work ok on the timeTaken field ?

 | makeresults count=1 
    | eval data="2020-02-12 17:50:15.228  INFO 1 --- [io-8080-exec-45] c.m.v.c.RequestInterceptor         : {\"logType\":\"RESPONS\",\"traceId\":\"03927a51-23d6-4530-a0e6-112b6d4b5539\",\"timestamp\":\"Feb 12, 2020 5:50:15 PM\",\"requestMethod\":\"GET\",\"requestUrl\":\"http://my.url\",\"responseStatus\":500,\"timeTaken\":28}\""
    | rex field=data "^([^{]+)(?P<json>.+})\"$"
    | spath input=json
0 Karma
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...