Splunk Search

How to calculate time between events within a session

jy190
New Member

I have events like

session_id = 1 device_time = 2017-11-26T12:13:32
session_id = 1 device_time = 2017-11-26T12:13:35
session_id = 1 device_time = 2017-11-26T12:13:37

session_id = 2 device_time = 2017-11-26T12:13:41
session_id = 2 device_time = 2017-11-26T12:13:48

session_id = 3 device_time = 2017-11-26T12:13:30

session_id = 4 device_time = 2017-11-26T12:13:21
session_id = 4 device_time = 2017-11-26T12:13:25
session_id = 4 device_time = 2017-11-26T12:13:34
session_id = 4 device_time = 2017-11-26T12:13:38

I would like to calculate time difference between consecutive events within a session, and get result like:

session_id = 1 time_diff = 3
session_id = 1 time_diff = 2
session_id = 2 time_diff = 7
session_id = 4 time_diff = 4
session_id = 4 time_diff = 9
session_id = 4 time_diff = 4

Tags (1)
0 Karma

woodcock
Esteemed Legend

You can use autoregress for this and it is a bit simpler than biting off streamstats.

0 Karma

micahkemp
Champion

Look into what you can do with streamstats. As an example:

<your search> | streamstats current=false last(device_time) AS last_device_time BY session_id | eval time_diff=device_time-last_device_time
0 Karma

jy190
New Member

Thanks. I'm new to splunk. Could you explain why this logic works?

0 Karma

DalJeanis
SplunkTrust
SplunkTrust

@micahkemp - (1) you need to ensure that you know which order the events are in. Put an appropriate |sort 0 statement before the streamstats... unless you are certain the default order will work for the use case. (2) you need to deal with the fact that the first record for each session id will have a null last_device_time. you need a final verb that will deal with that.

0 Karma

micahkemp
Champion

Great points that need to be considered.

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 ...