Splunk Search

Regular expression works separately but, not able to work it within Splunk query.

mappu
Engager

Hello,

I'm trying to find average response time of all events after the field totalTimeTaken. Thing is, when I tested this regular expression on Regular Expression Site

It shows I'm extracting the field and value correctly but, when I put the same into the Splunk statement it is not yielding the expected result. 

Log: 

 

 

 

 

 

{"Record: {"ATimeTaken":0, "BTimeTaken":0 ,"totalTimeTaken":4},{anotherFields}}

 

 

 

 

Query:

 

 

 

 

| makeresults ns=project*
| eval _raw="\"totalTimeTaken\":4" 
| rex field=_raw "\"totalTimeTaken\":+(?<Response_Time>\d+)"
| stats avg(response_time)

 

 

 

 

 

Could I know where I'm going wrong?

Labels (1)
0 Karma

mappu
Engager

Removed the eval statement and got it working. 

0 Karma

burwell
SplunkTrust
SplunkTrust

Hi. 

So you tried

| makeresults ns=project*
| eval _raw="\"totalTimeTaken\":4" 
| rex field=_raw "\"totalTimeTaken\":+(?<Response_Time>\d+)"
| stats avg(response_time)

 

And there are two problems.

1) the first makeresults .. I don't know what the ns=project* is. Here's the reference
https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Makeresults

2) your rex is extracting the value into the field Response_Time but then use do stats avg on response_time in lowercase.  Case matters in Splunk field names.

Here's what seems to work

| makeresults=1
| eval _raw="\"totalTimeTaken\":4" 
| rex field=_raw "\"totalTimeTaken\":+(?<Response_Time>\d+)"
| stats avg(Response_Time)

mappu
Engager

Thanks.

And, It is taking only one event and returning me average response time as 4 or any number I put in the eval field. I couldn't grab all the numbers in such logs/events and take average value.

0 Karma
Get Updates on the Splunk Community!

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...