Class: NewRelic::Rack::BrowserMonitoring
- Inherits:
-
Object
- Object
- NewRelic::Rack::BrowserMonitoring
- Defined in:
- lib/new_relic/rack/browser_monitoring.rb
Constant Summary collapse
- ALREADY_INSTRUMENTED_KEY =
"newrelic.browser_monitoring_already_instrumented"
Instance Method Summary collapse
- #autoinstrument_source(response, headers) ⇒ Object
-
#call(env) ⇒ Object
method required by Rack interface.
-
#initialize(app, options = {}) ⇒ BrowserMonitoring
constructor
A new instance of BrowserMonitoring.
- #should_instrument?(env, status, headers) ⇒ Boolean
Constructor Details
#initialize(app, options = {}) ⇒ BrowserMonitoring
Returns a new instance of BrowserMonitoring.
10 11 12 |
# File 'lib/new_relic/rack/browser_monitoring.rb', line 10 def initialize(app, = {}) @app = app end |
Instance Method Details
#autoinstrument_source(response, headers) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/new_relic/rack/browser_monitoring.rb', line 42 def autoinstrument_source(response, headers) source = nil response.each {|fragment| source ? (source << fragment.to_s) : (source = fragment.to_s)} return nil unless source # Only scan the first 50k (roughly) then give up. beginning_of_source = source[0..50_000] # Don't scan for body close unless we find body start if (body_start = beginning_of_source.index("<body")) && (body_close = source.rindex("</body>")) = NewRelic::Agent. header = NewRelic::Agent.browser_timing_header x_ua_compatible_found = beginning_of_source.include?('X-UA-Compatible') head_pos = if x_ua_compatible_found # put at end of header if X-UA-Compatible meta tag found ::NewRelic::Agent.logger.debug "Detected X-UA-Compatible meta tag. Attempting to insert RUM header at end of head." beginning_of_source.index("</head>") elsif head_open = beginning_of_source.index("<head") ::NewRelic::Agent.logger.debug "Attempting to insert RUM header at beginning of head." # put at the beginning of the header beginning_of_source.index(">", head_open) + 1 else ::NewRelic::Agent.logger.debug "Failed to detect head tag. Attempting to insert RUM header at above body tag." # otherwise put the header right above body start body_start end # check that head_pos is less than body close. If it's not something # is really weird and we should punt. if head_pos && (head_pos < body_close) # rebuild the source source = source[0..(head_pos-1)] << header << source[head_pos..(body_close-1)] << << source[body_close..-1] else if head_pos ::NewRelic::Agent.logger.debug "Skipping RUM instrumentation. Failed to detect head tags." else ::NewRelic::Agent.logger.debug "Skipping RUM instrumentation. Detected head is after detected body close." end end end headers['Content-Length'] = source.length.to_s if headers['Content-Length'] source end |
#call(env) ⇒ Object
method required by Rack interface
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/new_relic/rack/browser_monitoring.rb', line 15 def call(env) result = @app.call(env) # [status, headers, response] if (NewRelic::Agent.browser_timing_header != "") && should_instrument?(env, result[0], result[1]) response_string = autoinstrument_source(result[2], result[1]) env[ALREADY_INSTRUMENTED_KEY] = true if response_string response = Rack::Response.new(response_string, result[0], result[1]) response.finish else result end else result end end |
#should_instrument?(env, status, headers) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/new_relic/rack/browser_monitoring.rb', line 35 def should_instrument?(env, status, headers) status == 200 && !env[ALREADY_INSTRUMENTED_KEY] && headers["Content-Type"] && headers["Content-Type"].include?("text/html") && !headers['Content-Disposition'].to_s.include?('attachment') end |