title: Displaying Rails response time in view
body: <p>I'm trying to set up custom Pingdom monitoring of my Rails application and would like to craft an XML response that looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;pingdom_http_custom_check&gt;
    &lt;status&gt;OK&lt;/status&gt;
    &lt;response_time&gt;210.22&lt;/response_time&gt;
  &lt;/pingdom_http_custom_check&gt;
  </code></pre>
  
  <p>Where the response_time value is the time taken for Rails to render the XML response.</p>
  
  <p>I've found some <a href="http://bmorearty.wordpress.com/2008/05/21/how-to-show-response-time-in-a-rails-page-with-mongrel/" rel="nofollow noreferrer">resources</a> for doing this with Mongrel but I'm using Apache Passenger.</p>
  
  <p>I have a solution implemented as a Sinatra-Metal endpoint like this:</p>
  
  <pre><code>class Pingdom &lt; Sinatra::Base  
    set :views, "#{RAILS_ROOT}/app/views/metal"
    get '/pingdom' do
      @start_time = Time.now
      builder :pingdom
    end
  end
  </code></pre>
  
  <p>and then I have a builder view:</p>
  
  <pre><code>xml.instruct!
  xml.pingdom_http_custom_check do |pingdom_http_custom_check|
    pingdom_http_custom_check.status("OK")
    pingdom_http_custom_check.response_time((Time.now-@start_time)*1000)
  end
  </code></pre>
  
  <p>(Note the response_time is in milliseconds).</p>
  
  <p>This implementation is sufficient for checking that the application is up but the very fact that it is a metal endpoint means that it's not actually hitting the main Rails application and so isn't really a very good measure of the current response time of my application.  </p>
  
  <p>Does anyone have any good suggestions for a better approach?</p>
  
  <p>Cheers, Arfon</p>