<?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 do I track request time for splunkweb? in Monitoring Splunk</title>
    <link>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46365#M536</link>
    <description>&lt;P&gt;Another option:  Upgrade to Splunk 4.2.3.  There is now a field called &lt;CODE&gt;spent&lt;/CODE&gt; that contains basically the same information that I manually collected above.  (The time is calculated slightly differently, but Splunk's implementation probably involves less overhead.)&lt;/P&gt;</description>
    <pubDate>Wed, 28 Sep 2011 21:26:23 GMT</pubDate>
    <dc:creator>Lowell</dc:creator>
    <dc:date>2011-09-28T21:26:23Z</dc:date>
    <item>
      <title>How do I track request time for splunkweb?</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46363#M534</link>
      <description>&lt;P&gt;I'm seeing some odd delays in web user interface, and I'm trying to figure out if this is a browser issue or something in splunkweb.  Is there any way to figure out how long requests are taking in splunkweb?&lt;/P&gt;

&lt;P&gt;It seems like splunkweb is freezing periodically, and this is happening more frequently over time, but I'm looking for some way to quantify this.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;Notes and observations:&lt;/STRONG&gt;&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt; Using browser-side analysis, I can tell that some HTTP requests are taking many seconds to return.  But I'm not 100% sure if this is a client or server issue.&lt;/LI&gt;
&lt;LI&gt;I found request time information in &lt;CODE&gt;splunkd_access.log&lt;/CODE&gt;, but there doesn't appear to be anything similar in &lt;CODE&gt;web_access.log&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;I've noted that when the web interface is slow, it's hanging aross multiple clients all at the same time; so it doesn't seem to be a session related problem.&lt;/LI&gt;
&lt;LI&gt;I've also seen a very significant CPU spike for the "splunkweb" (python) process whenever this problem is occurring.  Often the cpu usage goes to 100% and sharply drops within a few seconds of when the web interface starts responding again.&lt;/LI&gt;
&lt;LI&gt;There is a mechanism that displays the view &lt;CODE&gt;PERF&lt;/CODE&gt; log message that shows view vs. template time, but that hasn't been helpful in tracking the issue.  It appears that the issue is not the initial page load when first opening a view, but instead the unresponsiveness must be with one of the background aync HTTP calls.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;I'm running Splunk 4.1.8 on Ubuntu 8.04 (32 bit).&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2011 14:16:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46363#M534</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-07-21T14:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I track request time for splunkweb?</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46364#M535</link>
      <description>&lt;P&gt;&lt;STRONG&gt;This is a hack&lt;/STRONG&gt; to log the duration of requests in splunkweb.  This allows you to (1) log the time of very long requests (any request over a specified number of milliseconds), and (2) log the request processing time for each request in the &lt;CODE&gt;web_access.log&lt;/CODE&gt; access log.&lt;/P&gt;

&lt;P&gt;If you are not at all familiar with python, and/or don't feel comfortable poking around in the splunkweb (cherrypy) code, than you should avoid this hack.  You have been warned.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;NOTE:&lt;/STRONG&gt;  (1.) You should backup all files before making any modifications.  (2.) Be aware that any splunk upgrade will overwrite your changes.  (3.) This is intended to be used to temporarily monitor request processing times, and hasn't been tested on a large scale or for long-term production use.  Your millage may vary.  (4.)  Please read all the instructions before beginning.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Okay, enough disclaimers.  On to the fun stuff...&lt;/P&gt;

&lt;H1&gt;Overview&lt;/H1&gt;

&lt;P&gt;Basically, this hack involves the following:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Install a simple &lt;A href="http://www.cherrypy.org/wiki/CustomTools"&gt;CherryPy custom tool&lt;/A&gt; that captures the start/end times of your request.  (Basically you just copy-n-paste into a file.)&lt;/LI&gt;
&lt;LI&gt;Update the core splunkweb startup script (&lt;CODE&gt;root.py&lt;/CODE&gt;) to import the CherryPy custom request logging module.&lt;/LI&gt;
&lt;LI&gt;Add new entries to the &lt;CODE&gt;web.conf&lt;/CODE&gt; to enable our custom cherrypy tool.&lt;/LI&gt;
&lt;LI&gt;If desired, modify the &lt;CODE&gt;customlogmanager.py&lt;/CODE&gt; to add request processing time to each request in the &lt;CODE&gt;splunkweb_access.log&lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;H1&gt;Steps&lt;/H1&gt;

&lt;H2&gt;Step 1:  Create new 'requesttime' python module.&lt;/H2&gt;

&lt;P&gt;Create a new file &lt;CODE&gt;$SPLUNK_HOME/lib/python2.6/site-packages/requesttime.py&lt;/CODE&gt;, with the following contents:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# The idea and code borrowed from here:
# &lt;A href="http://groups.google.com/group/cherrypy-users/browse_thread/thread/cb520cfa6588b550" target="test_blank"&gt;http://groups.google.com/group/cherrypy-users/browse_thread/thread/cb520cfa6588b550&lt;/A&gt;
import cherrypy, logging, time

class RequestDurationTool(cherrypy.Tool):
    """Tool to provide logging if a request exceeds a configured duration
    (in ms) set by the threshold configuration parameter"""
    CFG_THRESHOLD="tools.request_duration.threshold"
    def __init__(self):
        cherrypy.Tool.__init__(self, 'before_request_body', self._start_request)
        self.logger = logging.getLogger('splunk')
    def _setup(self):
        cherrypy.Tool._setup(self)
        # Attach extra hook to compute timing after both raw processing and I/O
        cherrypy.request.hooks.attach('on_end_resource', self._end_resource)
        cherrypy.request.hooks.attach('on_end_request', self._end_request)
    def _start_request(self, **kwargs):
        cherrypy.request.request_start = time.time()
    def _end_resource(self):
        cherrypy.request.request_stop = time.time()
    def _end_request(self):
        # Elapsed time and threshold is in ms
        elapsed = round((time.time() - cherrypy.request.request_start) * 1000)
        limit = cherrypy.request.config.get(self.CFG_THRESHOLD, None)
        if (limit is not None and elapsed &amp;gt; limit):
            rsc_elapsed = round((cherrypy.request.request_stop -
                                 cherrypy.request.request_start) * 1000)
            self.logger.log(logging.WARNING,
                            'Request duration: %d [%d] ms (%s)' %
                            (elapsed, rsc_elapsed,
                             cherrypy.request.request_line))

cherrypy.tools.request_duration = RequestDurationTool()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;Step 2:  Update your &lt;CODE&gt;root.py&lt;/CODE&gt; main application&lt;/H2&gt;

&lt;P&gt;The root CherryPy script must be updated to import our "requesttime" module.  (Make a backup of the original &lt;CODE&gt;root.py&lt;/CODE&gt; file before starting.) This file is located here:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE&gt;$SPLUNK_HOME/lib/python2.6/site-packages/splunk/appserver/mrsparkle/root.py&lt;/CODE&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Add the following python block to the top of the &lt;CODE&gt;root.py&lt;/CODE&gt; script. When you are done, the first few lines should look like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

# CUSTOM HACK:   Import the "requesttime" cherrypy tool
try:
    import requesttime
except Exception, e:
    print "Failed to import requesttime cherrypy request time tracker!!"
    print e
# END OF CUSTOM HACK
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;Step 3:  Enable the request_duration tool in web.conf&lt;/H2&gt;

&lt;P&gt;In &lt;CODE&gt;$SPLUNK_HOME/etc/system/local/web.conf&lt;/CODE&gt;, add the following to the &lt;CODE&gt;[settings]&lt;/CODE&gt; section:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# Enable custom cherrypy toolbox for debugging request processing time
# NOTE:  Comment out these two lines if you experience problems starting splunkweb!!
tools.request_duration.on = True
tools.request_duration.threshold = 5000
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The &lt;CODE&gt;threshold&lt;/CODE&gt; line is optional.  If you choose to perform step 4, and you do not want to log long requests separately, then you can exclude this entry.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt;  After a splunk upgrade, the &lt;CODE&gt;root.py&lt;/CODE&gt; script will be replaced and the above configuration entries &lt;EM&gt;could&lt;/EM&gt; prevent splunkweb from starting.  You will see these errors in &lt;CODE&gt;web_service.log&lt;/CODE&gt;.  You will need to redo step 2 after the upgrade, or comment out these entries in &lt;CODE&gt;web.conf&lt;/CODE&gt;.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;H2&gt;Step 4:  Enable request duration logging in &lt;CODE&gt;web_access.log&lt;/CODE&gt;&lt;/H2&gt;

&lt;P&gt;This step is optional, if you only want to log really long request, then you can skip it.  If you want to do any trending, then you'll probably want to capture the timing of all requests.&lt;/P&gt;

&lt;P&gt;We will need to update the following python module:  &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE&gt;$SPLUNK_HOME/lib/python2.6/site-packages/splunk/appserver/mrsparkle/lib/customlogmanager.py&lt;/CODE&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Towards the bottom of this module, you'll find this line:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;self.access_log.log(logging.INFO, self.access_log_format % atoms + ' - ' + get_request_id())
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Replace that line with the following block of code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        try:
            timming = "%dms" % (round((cherrypy.request.request_stop - cherrypy.request.request_start) * 1000))
        except:
            timming = "-"
        self.access_log.log(logging.INFO, self.access_log_format % atoms + ' - ' + get_request_id() + ' ' + timming)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You will need to make sure you have the indentation correct, or the python module will have issues; this could prevent splunkweb from even starting.&lt;/P&gt;

&lt;P&gt;You may also want to setup field extractions for the request duration, which can be done by adding the following to your &lt;CODE&gt;$SPLUNK_HOME/etc/system/local/props.conf&lt;/CODE&gt; config file:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[splunk_web_access]
EXTRACT-request-time = \s(?&amp;lt;req_duration&amp;gt;\d+)ms$
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jul 2011 15:10:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46364#M535</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-07-21T15:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I track request time for splunkweb?</title>
      <link>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46365#M536</link>
      <description>&lt;P&gt;Another option:  Upgrade to Splunk 4.2.3.  There is now a field called &lt;CODE&gt;spent&lt;/CODE&gt; that contains basically the same information that I manually collected above.  (The time is calculated slightly differently, but Splunk's implementation probably involves less overhead.)&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2011 21:26:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Monitoring-Splunk/How-do-I-track-request-time-for-splunkweb/m-p/46365#M536</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-09-28T21:26:23Z</dc:date>
    </item>
  </channel>
</rss>

