Sha256: fab4ae01a6ceb6ac5ceedbea43ef1927548bb8cc0024fe2b1ad3eed271939736

Contents?: true

Size: 1.86 KB

Versions: 7

Compression:

Stored size: 1.86 KB

Contents

require 'rack'

module NewRelic::Rack
  class BrowserMonitoring

    def initialize(app, options = {})
      @app = app
    end

    # method required by Rack interface
    def call(env)
      
      # clear out the thread locals we use in case this is a static request
      Thread.current[:newrelic_most_recent_transaction] = nil
      Thread.current[:newrelic_start_time] = Time.now
      Thread.current[:newrelic_queue_time] = 0
      
      result = @app.call(env)   # [status, headers, response]

      if (NewRelic::Agent.browser_timing_header != "") && should_instrument?(result[0], result[1])
        response_string = autoinstrument_source(result[2], result[1])

        if (response_string)
          Rack::Response.new(response_string, result[0], result[1]).finish
        else
          result
        end
      else
        result
      end
    end

    def should_instrument?(status, headers)
      status == 200 && headers["Content-Type"] && headers["Content-Type"].include?("text/html")
    end

    def autoinstrument_source(response, headers)
      source = nil
      response.each {|fragment| (source) ? (source << fragment) : (source = fragment)}

      body_start = source.index("<body")
      body_close = source.rindex("</body>")

      if body_start && body_close
        footer = NewRelic::Agent.browser_timing_footer
        header = NewRelic::Agent.browser_timing_header

        head_open = source.index("<head")

        if head_open
          head_close = source.index(">", head_open)

          head_pos = head_close + 1
        else
          # put the header right above body start
          head_pos = body_start
        end

        source = source[0..(head_pos-1)] + header + source[head_pos..(body_close-1)] + footer + source[body_close..-1]

        headers['Content-Length'] = source.length.to_s if headers['Content-Length']
      end

      source
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
newrelic_rpm-3.1.0.beta5 lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.1.0.beta4 lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.1.0.djlogging2 lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.1.0.djlogging lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.1.0.beta3 lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.1.0.beta2 lib/new_relic/rack/browser_monitoring.rb
newrelic_rpm-3.0.1 lib/new_relic/rack/browser_monitoring.rb