<?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: Calculate elapsed time from field timestamp in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194602#M56101</link>
    <description>&lt;P&gt;I've tried both options, but resorted to using option 2 as it made things simpler. Thanks for your help Kristian!&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2014 07:58:56 GMT</pubDate>
    <dc:creator>johnsmithbitter</dc:creator>
    <dc:date>2014-01-07T07:58:56Z</dc:date>
    <item>
      <title>Calculate elapsed time from field timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194600#M56099</link>
      <description>&lt;P&gt;I'm new to splunk and I'm trying to calculate the elapsed time between two events 'STARTED &amp;amp; FINISHED' by event_type by context_event. The problem I have is the timestamp is an extracted field and not the _time given by splunk. I've tried various different ways using the support portal but have failed miserably &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; &lt;/P&gt;

&lt;P&gt;sourcetype=eventstore | transaction source_event startswith="STARTED" endswith="FINISHED" | eval elapsed=timestamp_event-timestamp_event &lt;/P&gt;

&lt;P&gt;This is the event output I get when I run the above query but I can't seem to get it to sum up the elapsed time &lt;/P&gt;

&lt;P&gt;03-01-2014 06:55:30, EventLoggerListener , Event=id='3241388266', message='Report1', timestamp=03-01-2014 06:55:30.535 GMT, type='ENRICHMENT', source='IAS', status='STARTED' 03-01-2014 06:55:30 , EventLoggerListener , Event=id='1670471136', message='Report1', timestamp=03-01-2014 06:55:30.544 GMT, type='ENRICHMENT', source='IAS', status='FINISHED'&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;context_event = Report1
date_event = 03-01-2014
event_id = 1670471136 event_id = 3241388266
event_type = ENRICHMENT
sourcetype = eventstore
status_event = FINISHED status_event = STARTED
timestamp_event = 06:55:30.535 timestamp_event = 06:55:30.544
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks in advance! &lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:34:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194600#M56099</guid>
      <dc:creator>johnsmithbitter</dc:creator>
      <dc:date>2020-09-28T15:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate elapsed time from field timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194601#M56100</link>
      <description>&lt;P&gt;So I guess that these are the two events that make up the transaction, right?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;03-01-2014 06:55:30, EventLoggerListener , Event=id='3241388266', message='Report1', timestamp=03-01-2014 06:55:30.535 GMT, type='ENRICHMENT', source='IAS', status='STARTED' 

03-01-2014 06:55:30 , EventLoggerListener , Event=id='1670471136', message='Report1', timestamp=03-01-2014 06:55:30.544 GMT, type='ENRICHMENT', source='IAS', status='FINISHED'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You have two options;&lt;/P&gt;

&lt;P&gt;1) convert the (second) timestamp in each event into a format that is suitable for mathematical operations like subtraction.&lt;/P&gt;

&lt;P&gt;2) configure splunk to use the second timestamp (instead of the first) when extracting the &lt;CODE&gt;_time&lt;/CODE&gt; field.&lt;/P&gt;

&lt;P&gt;If the second timestamp (&lt;CODE&gt;timestamp_event&lt;/CODE&gt; as you call it) is always going to be very close to 'regular' timestamp in the beginning of each event, you should consider option 2, as it's a simpler configuration, and will also let the &lt;STRONG&gt;transaction&lt;/STRONG&gt; command calculate the duration automatically. If there can be a significant difference between the first and second timestamp in the events - and if this is important - you should go for option 1. Also, I'm not 100% sure that the &lt;STRONG&gt;duration&lt;/STRONG&gt; field that is calculated by the &lt;STRONG&gt;transaction&lt;/STRONG&gt; will keep subsecond precision, so you might have to go for the longer version of option 1 below, if your transactions are very short. &lt;/P&gt;

&lt;P&gt;For &lt;STRONG&gt;option 1&lt;/STRONG&gt;, the you would do like this (assuming that the second timestamp is extracted as &lt;CODE&gt;timestamp&lt;/CODE&gt;);&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search 
| eval myTime = strptime(timestamp, "%d-%m-%Y %H:%M:%S.%3N %Z") 
| transaction source_event startswith="STARTED" endswith="FINISHED" 
| eval start = mvindex(timestamp,0) 
| eval end = mvindex(timestamp,1) 
| eval duration = end - start
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;A shorter version, which does not preserve the original &lt;CODE&gt;_time&lt;/CODE&gt; (but only for the search, the evens are not changed in any way on disk);&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search 
| eval _time = strptime(timestamp, "%d-%m-%Y %H:%M:%S.%3N %Z") 
| transaction source_event startswith="STARTED" endswith="FINISHED" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;now duration is calculated automatically by &lt;CODE&gt;transaction&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;For &lt;STRONG&gt;option 2&lt;/STRONG&gt; you need to edit which timestamp splunk uses when indexing an event, and this is done in props.conf;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[your_sourcetype_here]
TIME_PREFIX = timestamp=
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and you can create your searches in the shortest way possible;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search 
| transaction source_event startswith="STARTED" endswith="FINISHED" 
| table duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;the last &lt;CODE&gt;table&lt;/CODE&gt; is for illustration purposes.&lt;/P&gt;

&lt;P&gt;Hope this helps,&lt;/P&gt;

&lt;P&gt;K&lt;/P&gt;

&lt;P&gt;EDIT: there seems to be some limit on how many &lt;STRONG&gt;code&lt;/STRONG&gt; markups there can be in one post, which screws up the rendering of the page. Tried changing some of them to &lt;STRONG&gt;bold&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2014 11:58:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194601#M56100</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2014-01-03T11:58:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate elapsed time from field timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194602#M56101</link>
      <description>&lt;P&gt;I've tried both options, but resorted to using option 2 as it made things simpler. Thanks for your help Kristian!&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2014 07:58:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194602#M56101</guid>
      <dc:creator>johnsmithbitter</dc:creator>
      <dc:date>2014-01-07T07:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate elapsed time from field timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194603#M56102</link>
      <description>&lt;P&gt;I've tried both options, but resorted to using option 2 as it made things simpler. Thanks for your help Kristian!&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2014 07:59:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-elapsed-time-from-field-timestamp/m-p/194603#M56102</guid>
      <dc:creator>johnsmithbitter</dc:creator>
      <dc:date>2014-01-07T07:59:19Z</dc:date>
    </item>
  </channel>
</rss>

