Splunk Search

Session duration calculation

dbcase
Motivator

Hi,

I have this data

    Time    Event
11/13/17
5:12:53.000 PM  
{   [-] 
     analyticType:   SessionEnd 
     buildTarget:    comcast    
     clientSessionId:    DXCRKSZ-ANLDDLZ    
}

11/13/17
4:22:02.000 PM  
{   [-] 
     Properties:    {   [+] 
    }   
     analyticType:   SessionStart   
     buildTarget:    comcast    
     clientSessionId:    DXCRKSZ-ANLDDLZ    
}

There are a lot of different session ID's each one is unique this is just a snippet example. What I'd like to do is to compute the session duration time. Is there a way to do this with the stats command?

Tags (1)
0 Karma
1 Solution

micahkemp
Champion

Try this:

| stats min(_time) AS session_earliest, max(_time) AS session_latest, values(analyticType) AS analyticType BY clientSessionId
| search analyticType=sessionStart analyticType=sessionEnd
| eval duration=session_latest-session_earliest
| stats avg(duration)

If there is more than one event per clientSessionId with analyticType=sessionStart, this may give incorrect results, but I did at least try to ensure that only sessions with a Start and End were considered.

View solution in original post

0 Karma

micahkemp
Champion

Try this:

| stats min(_time) AS session_earliest, max(_time) AS session_latest, values(analyticType) AS analyticType BY clientSessionId
| search analyticType=sessionStart analyticType=sessionEnd
| eval duration=session_latest-session_earliest
| stats avg(duration)

If there is more than one event per clientSessionId with analyticType=sessionStart, this may give incorrect results, but I did at least try to ensure that only sessions with a Start and End were considered.

0 Karma

dbcase
Motivator

Hi Micahkemp,

Yea there are several sessions that have multiple starts and ends during the time range...... Guess using transaction might be the best way to do this???

0 Karma

dbcase
Motivator

I tried this query and I think it works but still would like to see if this can be done with the stats command

index=wholesale_app analyticType=sessionStart OR analyticType=sessionEnd |transaction clientSessionId startswith="sessionStart" endswith="sessionEnd"|stats avg(duration)
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!

[Puzzles] Solve, Learn, Repeat: Tiling

This puzzle (first published here) is based on finding groups of tessellated tiles (inspired by floor tiles I ...

SOK it to Me: Top 3 Benefits of Using Splunk Operator on Kubernetes that’ll Make ...

    Thursday, July 9, 2026  |  11:00AM–12:00PM PDT Duration: 1 hour (includes Q&A) Managing can feel like a ...

Upgrade Prep for 10.4, Network Observability Deep Dives, and More from Splunk Lantern

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...