<?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 to write a transaction restricting time of start event? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388739#M113349</link>
    <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/76771"&gt;@stewartevans&lt;/a&gt;, for your use case you would be better off using stats instead of transaction for correlation. Refer to &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation" target="_blank"&gt;About event grouping and correlation&lt;/A&gt; documentation. This should give you more control over your correlation using search filter as per your need after the stats command. Also stats would perform better for longer duration/more events as compared to transaction.&lt;/P&gt;

&lt;P&gt;Following is a run anywhere example with some sample data as based on points  described in the question. It generates some events with Request and Response i.e. for transaction id &lt;CODE&gt;1234&lt;/CODE&gt;, &lt;CODE&gt;4567&lt;/CODE&gt;. But no response for &lt;CODE&gt;8910&lt;/CODE&gt;. For testing purpose it also add a request &lt;CODE&gt;1112&lt;/CODE&gt;, which has _time set as &lt;CODE&gt;9 sec&lt;/CODE&gt; before current time. While testing you can change the &lt;CODE&gt;| eval _time=_time-9&lt;/CODE&gt; pipe to &lt;CODE&gt;10&lt;/CODE&gt;, &lt;CODE&gt;11&lt;/CODE&gt; etc test out less than, equal to and greater than 10 sec scenarios.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;1.&lt;/STRONG&gt; The &lt;CODE&gt;searchmatch()&lt;/CODE&gt; evaluation function has been used to create &lt;CODE&gt;request&lt;/CODE&gt; and &lt;CODE&gt;response&lt;/CODE&gt; &lt;CODE&gt;type&lt;/CODE&gt; fields for corresponding events, as the same does not seem to be present in your data as per transaction query you have run.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt; The stats command groups &lt;CODE&gt;type&lt;/CODE&gt; together for each transaction id i.e. &lt;CODE&gt;tid&lt;/CODE&gt; using &lt;CODE&gt;list()&lt;/CODE&gt; function. &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;3.&lt;/STRONG&gt; Although for the following use case you dont need this but as per the type for data &lt;CODE&gt;mvindex(type,0)&lt;/CODE&gt; should give &lt;CODE&gt;startswith&lt;/CODE&gt; condition i.e. &lt;CODE&gt;request&lt;/CODE&gt; and &lt;CODE&gt;mvindex(type,1)&lt;/CODE&gt; should give &lt;CODE&gt;endswith&lt;/CODE&gt; condition i.e. &lt;CODE&gt;response&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;4.&lt;/STRONG&gt; In your case you are interested in events where &lt;CODE&gt;request exist but there is not response&lt;/CODE&gt; i.e. &lt;CODE&gt;| search type="request" AND type!="response"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;5.&lt;/STRONG&gt; Further, such events will have same earliestTime and latestTime as there is no response. &lt;CODE&gt;now()-earliestTime&lt;/CODE&gt; has been used to get the time duration between request received and current time. So that we can filter only request received older than 10 seconds.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval data="Time=\"2018/07/31 01:00:00\" some request transaction_id=1234;Time=\"2018/07/31 01:00:10\" some response original_transaction_id=1234;Time=\"2018/07/31 01:10:00\" some request transaction_id=4567;Time=\"2018/07/31 01:10:20\" some response original_transaction_id=4567;Time=\"2018/07/31 02:00:00\" some request transaction_id=8910;"
| makemv data delim=";"
| mvexpand data
| rename data as _raw
| KV
| eval _time=strptime(Time,"%Y/%m/%d %H:%M:%S")
| fields - Time
| append [| makeresults
| eval _time=_time-9
| eval _raw="some request transaction_id=1112"
| KV]
| eval tid=coalesce(transaction_id, original_transaction_id) 
| eval type=case(searchmatch("request"),"request",searchmatch("response"),"response",true(),"N/A")
| stats list(type) as type min(_time) as earliestTime max(_time) as latestTime by tid
| search type="request" AND type!="response"
| eval requestTimeDuration=now()-earliestTime
| where requestTimeDuration&amp;gt;10
| fieldformat earliestTime=strftime(earliestTime,"%Y/%m/%d %H:%M:%S")
| fieldformat latestTime=strftime(latestTime,"%Y/%m/%d %H:%M:%S")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: fieldformat has been applied to convert epoch time to human readable string time format for earliest and latest time.&lt;/P&gt;

&lt;P&gt;If you need events where &lt;CODE&gt;response was received&lt;/CODE&gt; however, &lt;CODE&gt;duration took longer than 10 seconds&lt;/CODE&gt; you search filter can be changed to the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | search type="request" AND type="response"`
 | eval duratino=latestTime-earliestTime
 | where duration&amp;gt;10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Sep 2020 20:41:12 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2020-09-29T20:41:12Z</dc:date>
    <item>
      <title>How to write a transaction restricting time of start event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388738#M113348</link>
      <description>&lt;P&gt;I want to create an alert which will find requests which have not received a response.&lt;/P&gt;

&lt;P&gt;I have created the following search which will find the the requests which have no responses. The request has an id called transaction_id and the response has the same identifier called original_transaction_id.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval tid = coalesce(transaction_id, original_transaction_id) 
| tid maxpause=2m startswith="request" endswith="response" keepevicted=true  
| where evicted=1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works as a search however as an alert I'm currently getting false alerts as the response may take up to 10 seconds to be received from when the request has been posted. So the alert needs to take that into account and ignore any requests which are younger than 10 seconds old.&lt;/P&gt;

&lt;P&gt;Can someone please help me to add the time restriction to the "request" event to prevent the false alerts&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:45:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388738#M113348</guid>
      <dc:creator>stewartevans</dc:creator>
      <dc:date>2020-09-29T20:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a transaction restricting time of start event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388739#M113349</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/76771"&gt;@stewartevans&lt;/a&gt;, for your use case you would be better off using stats instead of transaction for correlation. Refer to &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation" target="_blank"&gt;About event grouping and correlation&lt;/A&gt; documentation. This should give you more control over your correlation using search filter as per your need after the stats command. Also stats would perform better for longer duration/more events as compared to transaction.&lt;/P&gt;

&lt;P&gt;Following is a run anywhere example with some sample data as based on points  described in the question. It generates some events with Request and Response i.e. for transaction id &lt;CODE&gt;1234&lt;/CODE&gt;, &lt;CODE&gt;4567&lt;/CODE&gt;. But no response for &lt;CODE&gt;8910&lt;/CODE&gt;. For testing purpose it also add a request &lt;CODE&gt;1112&lt;/CODE&gt;, which has _time set as &lt;CODE&gt;9 sec&lt;/CODE&gt; before current time. While testing you can change the &lt;CODE&gt;| eval _time=_time-9&lt;/CODE&gt; pipe to &lt;CODE&gt;10&lt;/CODE&gt;, &lt;CODE&gt;11&lt;/CODE&gt; etc test out less than, equal to and greater than 10 sec scenarios.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;1.&lt;/STRONG&gt; The &lt;CODE&gt;searchmatch()&lt;/CODE&gt; evaluation function has been used to create &lt;CODE&gt;request&lt;/CODE&gt; and &lt;CODE&gt;response&lt;/CODE&gt; &lt;CODE&gt;type&lt;/CODE&gt; fields for corresponding events, as the same does not seem to be present in your data as per transaction query you have run.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt; The stats command groups &lt;CODE&gt;type&lt;/CODE&gt; together for each transaction id i.e. &lt;CODE&gt;tid&lt;/CODE&gt; using &lt;CODE&gt;list()&lt;/CODE&gt; function. &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;3.&lt;/STRONG&gt; Although for the following use case you dont need this but as per the type for data &lt;CODE&gt;mvindex(type,0)&lt;/CODE&gt; should give &lt;CODE&gt;startswith&lt;/CODE&gt; condition i.e. &lt;CODE&gt;request&lt;/CODE&gt; and &lt;CODE&gt;mvindex(type,1)&lt;/CODE&gt; should give &lt;CODE&gt;endswith&lt;/CODE&gt; condition i.e. &lt;CODE&gt;response&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;4.&lt;/STRONG&gt; In your case you are interested in events where &lt;CODE&gt;request exist but there is not response&lt;/CODE&gt; i.e. &lt;CODE&gt;| search type="request" AND type!="response"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;5.&lt;/STRONG&gt; Further, such events will have same earliestTime and latestTime as there is no response. &lt;CODE&gt;now()-earliestTime&lt;/CODE&gt; has been used to get the time duration between request received and current time. So that we can filter only request received older than 10 seconds.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval data="Time=\"2018/07/31 01:00:00\" some request transaction_id=1234;Time=\"2018/07/31 01:00:10\" some response original_transaction_id=1234;Time=\"2018/07/31 01:10:00\" some request transaction_id=4567;Time=\"2018/07/31 01:10:20\" some response original_transaction_id=4567;Time=\"2018/07/31 02:00:00\" some request transaction_id=8910;"
| makemv data delim=";"
| mvexpand data
| rename data as _raw
| KV
| eval _time=strptime(Time,"%Y/%m/%d %H:%M:%S")
| fields - Time
| append [| makeresults
| eval _time=_time-9
| eval _raw="some request transaction_id=1112"
| KV]
| eval tid=coalesce(transaction_id, original_transaction_id) 
| eval type=case(searchmatch("request"),"request",searchmatch("response"),"response",true(),"N/A")
| stats list(type) as type min(_time) as earliestTime max(_time) as latestTime by tid
| search type="request" AND type!="response"
| eval requestTimeDuration=now()-earliestTime
| where requestTimeDuration&amp;gt;10
| fieldformat earliestTime=strftime(earliestTime,"%Y/%m/%d %H:%M:%S")
| fieldformat latestTime=strftime(latestTime,"%Y/%m/%d %H:%M:%S")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: fieldformat has been applied to convert epoch time to human readable string time format for earliest and latest time.&lt;/P&gt;

&lt;P&gt;If you need events where &lt;CODE&gt;response was received&lt;/CODE&gt; however, &lt;CODE&gt;duration took longer than 10 seconds&lt;/CODE&gt; you search filter can be changed to the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | search type="request" AND type="response"`
 | eval duratino=latestTime-earliestTime
 | where duration&amp;gt;10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:41:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388739#M113349</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2020-09-29T20:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a transaction restricting time of start event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388740#M113350</link>
      <description>&lt;P&gt;This is brilliant @niketnilay I've just tested out your recommendation and it appears to work perfectly. I also learnt a lot about stats and sample data generation at the same time. Thank you very much!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 07:22:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388740#M113350</guid>
      <dc:creator>stewartevans</dc:creator>
      <dc:date>2018-07-31T07:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a transaction restricting time of start event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388741#M113351</link>
      <description>&lt;P&gt;@stewartevans I am glad you found it useful. I have learnt these things by hanging out here on Splunk Answers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Now you need to "pass on" the knowledge.&lt;/P&gt;

&lt;P&gt;The link that I provided is by Nick Mealy's and his flowchart for deciding event grouping and correlation is epic &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; There are more commands that have been introduced like &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/union"&gt;union&lt;/A&gt; in Splunk 6.6 and previously undocumented gem &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Multisearch"&gt;multisearch&lt;/A&gt;. They would eventually be documented in above flowchart as well.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 08:45:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-transaction-restricting-time-of-start-event/m-p/388741#M113351</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-07-31T08:45:34Z</dc:date>
    </item>
  </channel>
</rss>

