Getting Data In

Join two rows and compare fields

Peely
Explorer

I feel i'm so close, but can't quite make it work. I've tried map and am now trying a sub search (I think it's a sub search). I'm trying to get the time difference between two events, but now using the "_time" field, instead using a timestamp field of my own. My events look something like this

 

{

    action: "start",

    correlationId:"_GUID_",

    timestamp: "2021-07-13T03:44:46.100Z"

}

{

    action: "end",

    correlationId:"_GUID_",

    timestamp: "2021-07-13T03:44:46.260Z"

}

 

 

And my query so far is

index=* action=start
| eval start_time=timestamp
| join correlationId [ search index=action=end | eval end_time=timestamp ]
| eval timeTaken=end_time-start_time


But timeTaken is never populated. It seams my `timestamp` field has a "none" in it as well as a timestamp, but i'm not sure why as the raw text does not have any spaces or anything.

 

I also tried a selfjoin, that overwrite the first `timestamp` with the second one, and a map, which came back with no results. 

Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

It looks like the timestamp field is multi-valued.  The strptime function doesn't work with multi-valued fields.  Use mvindex to select one of the values or visit how the timestamp field is created to avoid multi-values.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

You can't do math on timestamp strings because they're not numbers.  Convert them to integers using strptime() and then do the math.

index=* action=start
| eval start_time=timestamp
| join correlationId [ search index=action=end | eval end_time=timestamp ]
| eval endTime=strptime(end_time,"%Y-%m-%dT%H:%M:%S.%3N%Z"), startTime=strptime(start_time,"%Y-%m-%dT%H:%M:%S.%3N%Z")
| eval timeTaken=endTime-startTime
---
If this reply helps you, Karma would be appreciated.

Peely
Explorer

Ok cool, I didn't know that! So I think that takes me a step closer but I'm still not getting a value for `timeTaken` in the result. I think my input timestamps have multiple values (though they shouldn't), would that effect it? See attached a screenshot of the Splunk GUI showing one of the timestamps on one of the input events.

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

It looks like the timestamp field is multi-valued.  The strptime function doesn't work with multi-valued fields.  Use mvindex to select one of the values or visit how the timestamp field is created to avoid multi-values.

---
If this reply helps you, Karma would be appreciated.

Peely
Explorer

`mvindex` was the missing piece! Thank you very much. My final query was in the form of

 

index=*(action=start) 
| eval singleTimestamp=mvindex(timestamp, 1) | eval start_time=singleTimestamp 
| join correlationId
    [ search index=* action=end   | eval singleTimestamp=mvindex(timestamp, 1) | eval end_time=singleTimestamp  ]
| eval endTime=strptime(end_time,"%Y-%m-%dT%H:%M:%S.%3N%Z"), startTime=strptime(start_time,"%Y-%m-%dT%H:%M:%S.%3N%Z")
| eval timeTaken=floor((endTime-startTime)*1000)

 

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...