<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How can I get the time difference between two events? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/621106#M106992</link>
    <description>&lt;P&gt;I know I'm late to the game here but here is another option for determining the difference in time between two events.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{base search}
| streamstats window=2 min(_time) as prevTime
| eval diffTime = _time-prevTime
| {the rest of your search here}&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 16 Nov 2022 16:52:34 GMT</pubDate>
    <dc:creator>fredclown</dc:creator>
    <dc:date>2022-11-16T16:52:34Z</dc:date>
    <item>
      <title>How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374861#M67895</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;I am looking at logs in an IIS index. These are events performed by someone who is using a product that we make at the company I work at. So far, when someone logs in we have been using the (custom field) value of action=login to view this event.&lt;BR /&gt;
In order to work out how long it takes someone to log in, we have simply been using the time_taken field for this action. &lt;BR /&gt;
However, we have come to realize that what actually happens when someone logs in, is that the action=login starts the process, and then another log/event finishes this process, called a_action=event_status&lt;/P&gt;

&lt;P&gt;Is it possible to find the time difference between these two events? I know they both have timestamps, which can be converted in epoch. The difference between these values is all we need, what is the easiest way to calculate this?&lt;/P&gt;

&lt;P&gt;(However, we do get onto the issue of making sure that we are looking at the same person instance of the product. We can see this from the cs_username field. If anyone knows how to tackle this issue at the same time that would be hugely convenient, but one issue at a time will suffice for now &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; ) &lt;/P&gt;

&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 18:39:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374861#M67895</guid>
      <dc:creator>samwatson45</dc:creator>
      <dc:date>2020-09-29T18:39:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374862#M67896</link>
      <description>&lt;P&gt;You can try something like this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=iis action=login cs_username=* 
| stats latest(_time) as time_login by cs_username 
| join cs_username 
    [ search index=iis a_action=event_status cs_username=* 
    | stats latest(_time) as time_finish by cs_username ] 
| eval difference=time_finish-time_login 
| eval difference=strftime(difference,"%d-%m-%Y %H:%M:%S")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also pass the &lt;CODE&gt;earliest= latest=&lt;/CODE&gt; in the base search of both the search as desired.&lt;BR /&gt;
let me know if this helps!&lt;/P&gt;</description>
      <pubDate>Thu, 22 Mar 2018 16:52:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374862#M67896</guid>
      <dc:creator>mayurr98</dc:creator>
      <dc:date>2018-03-22T16:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374863#M67897</link>
      <description>&lt;P&gt;Couple possible alternatives that might work for you:&lt;BR /&gt;
&lt;CODE&gt;&lt;BR /&gt;
index=iis (action="login" OR a_action="event_status") cs_username=* | eval start_time=if(action=="login",_time,null()), end_time=if(a_action=="event_status",_time,null()) | stats min(start_time) as start_time,max(end_time) as end_time by cs_username | eval diff=abs(end_time-start_time)&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Or:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;&lt;BR /&gt;
| multisearch [| search index=iis action=login cs_username=&lt;EM&gt;] [| search index=iis a_action=event_status cs_username=&lt;/EM&gt;] | stats earliest(_time) as start_time,latest(_time) as end_time by cs_username | eval diff=abs(end_time-start_time)&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 18:39:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374863#M67897</guid>
      <dc:creator>jlanders</dc:creator>
      <dc:date>2020-09-29T18:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374864#M67898</link>
      <description>&lt;P&gt;Thanks! This is really useful. &lt;BR /&gt;
Do you know if there is any way to ensure that the second event (event_status) definitely happens after the first one (login)? And ideally, the first happening of event_status after the login event? (As it is a background process that happens fairly often, but is the way we benchmark the login finishing).&lt;/P&gt;

&lt;P&gt;This is kind of an add-on question, so as you did answer the initial question I will accept the answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 18:39:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374864#M67898</guid>
      <dc:creator>samwatson45</dc:creator>
      <dc:date>2020-09-29T18:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374865#M67899</link>
      <description>&lt;P&gt;Thanks! I couldn't get that first one to work (I think it didn't join the separate events together).&lt;BR /&gt;
With the second one, it was giving me results but I am not sure it is definitely the time difference between those two specific events, not just any two events for a username.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 10:01:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374865#M67899</guid>
      <dc:creator>samwatson45</dc:creator>
      <dc:date>2018-03-23T10:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374866#M67900</link>
      <description>&lt;P&gt;well you can use &lt;CODE&gt;transaction&lt;/CODE&gt; command to look at the event details&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=iis action=login OR a_action=event_status cs_username=* | transaction cs_username startswith=action=login endswith=a_action=event_status
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can look at the event flow per cs_username. and the positive time difference will always ensure that there is the first login and then event_sattus.&lt;BR /&gt;
This will also create a &lt;CODE&gt;duration&lt;/CODE&gt; field which is same as your initial query i.e. time difference.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 18:38:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374866#M67900</guid>
      <dc:creator>mayurr98</dc:creator>
      <dc:date>2020-09-29T18:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374867#M67901</link>
      <description>&lt;P&gt;Ah brilliant, I shall look into that!&lt;BR /&gt;
Many thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 10:58:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/374867#M67901</guid>
      <dc:creator>samwatson45</dc:creator>
      <dc:date>2018-03-23T10:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/512159#M86908</link>
      <description>&lt;P&gt;The transaction command is the simplest way to aggregate related logs.&amp;nbsp; Depending on your log volume and what you want to see, the following will show the 95th percentile of the time between two events.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=iis cs_username=* 
| eval action = if(match(a_action, "event_status"), "login_complete", action)
| transaction cs_username 
    maxspan=5m
    startswith="action=login" 
    endswith="action=login_com"
| eventstats p95(duration) AS p95_duration
`comment("| timechart avg(duration) AS avg_duration, p95(duration) AS p95_duration, max(duration) AS max_duration, min(duration) AS min_duration by cs_username | predict avg_duration p95_duration max_duration")`&lt;/LI-CODE&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/8.0.4/SearchReference/Transaction" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/8.0.4/SearchReference/Transaction&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you click on the duration field on the left of the events list, it will show the average, minimum, maximum, and standard deviation.&amp;nbsp; Or you can calculate those with timechart/stats/chart and get a table of values or a visual representation, and use predict to forecast the values.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/8.0.5/SearchReference/Predict" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/8.0.5/SearchReference/Predict&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Using a reasonable maxspan value and startswith will significantly reduce the number of transactions in memory. I think the default maxspan is 1 day, which can cause a large number of evicted records if you have a large log volume.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Aug 2020 14:25:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/512159#M86908</guid>
      <dc:creator>malvidin</dc:creator>
      <dc:date>2020-08-03T14:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the time difference between two events?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/621106#M106992</link>
      <description>&lt;P&gt;I know I'm late to the game here but here is another option for determining the difference in time between two events.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{base search}
| streamstats window=2 min(_time) as prevTime
| eval diffTime = _time-prevTime
| {the rest of your search here}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Nov 2022 16:52:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-can-I-get-the-time-difference-between-two-events/m-p/621106#M106992</guid>
      <dc:creator>fredclown</dc:creator>
      <dc:date>2022-11-16T16:52:34Z</dc:date>
    </item>
  </channel>
</rss>

