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
Get Updates on the Splunk Community!

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...

Purpose in Action: How Splunk Is Helping Power an Inclusive Future for All

At Cisco, purpose isn’t a tagline—it’s a commitment. Cisco’s FY25 Purpose Report outlines how the company is ...

[Upcoming Webinar] Demo Day: Transforming IT Operations with Splunk

Join us for a live Demo Day at the Cisco Store on January 21st 10:00am - 11:00am PST In the fast-paced world ...