Splunk Search

Multivalue Extraction

cquinney
Communicator

I have the following set of data within each event:

stack_trace: [ [-]
{ [-]
class_name: FOO
file_name: BAR
line_number: -2
method_name: WALK
}
{ [-]
class_name: FOO2
file_name: BAR2
line_number: 1356
method_name: JUMP
}
{ [-]
class_name: FOO
file_name: BAR
line_number: 808
method_name: SKIP
}
]

I want to extract only the first method_name within the stack (| spath "stack_trace{}.method_name" | search "stack_trace{}.method_name"=WALK), which can change from event to event. I've tried using mvindex but I'm having no success. Any suggestions would be greatly appreciated.

0 Karma

to4kawa
Ultra Champion
| makeresults
| eval _raw="{\"stack_trace\":[{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"-2\",\"method_name\":\"WALK\"},{\"class_name\":\"FOO2\",\"file_name\":\"BAR2\",\"line_number\":\"1356\",\"method_name\":\"JUMP\"},{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"808\",\"method_name\":\"SKIP\"}]}"
| rex "method_name.*?(?<method_name>\w+)"

If you want only first method_name, try rex . Regex works by order.

0 Karma

dmarling
Builder

Using mvindex will work for you use case, but you will need to wrap the field name in single quotes since there are special characters in the field name that mean concatenation when using where or eval: . Using this eval will get the first method_name in the stack: | eval first_method_name=mvindex('stack_trace{}.method_name', 0) Here's a run anywhere example using your own data to demonstrate it:

| makeresults count=1
| eval data="{\"stack_trace\":[{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"-2\",\"method_name\":\"WALK\"},{\"class_name\":\"FOO2\",\"file_name\":\"BAR2\",\"line_number\":\"1356\",\"method_name\":\"JUMP\"},{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"808\",\"method_name\":\"SKIP\"}]}"
| spath input=data
| eval first_method_name=mvindex('stack_trace{}.method_name', 0)
If this comment/answer was helpful, please up vote it. Thank you.

cquinney
Communicator

Thank you! The eval works great, I was using double quotes instead of single, great catch!

0 Karma

dmarling
Builder

I'm glad I could help you! Please make sure to also accept the answer. Thank you!

If this comment/answer was helpful, please up vote it. Thank you.
0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...