Class: NewRelic::Rack::BrowserMonitoring

Inherits:
AgentMiddleware show all
Defined in:
lib/new_relic/rack/browser_monitoring.rb

Overview

This middleware is used by the agent for the Real user monitoring (RUM) feature, and will usually be automatically injected in the middleware chain. If automatic injection is not working, you may manually use it in your middleware chain instead.

Constant Summary collapse

SCAN_LIMIT =

The maximum number of bytes of the response body that we will examine in order to look for a RUM insertion point.

50_000
CONTENT_TYPE =
'Content-Type'.freeze
CONTENT_DISPOSITION =
'Content-Disposition'.freeze
CONTENT_LENGTH =
'Content-Length'.freeze
ATTACHMENT =
'attachment'.freeze
TEXT_HTML =
'text/html'.freeze
BODY_START =
'<body'.freeze
HEAD_START =
'<head'.freeze
GT =
'>'.freeze
ALREADY_INSTRUMENTED_KEY =
'newrelic.browser_monitoring_already_instrumented'
CHARSET_RE =
/<\s*meta[^>]+charset\s*=[^>]*>/im.freeze
X_UA_COMPATIBLE_RE =
/<\s*meta[^>]+http-equiv\s*=\s*['"]x-ua-compatible['"][^>]*>/im.freeze

Constants included from Agent::Instrumentation::MiddlewareTracing

Agent::Instrumentation::MiddlewareTracing::TXN_STARTED_KEY

Instance Attribute Summary

Attributes inherited from AgentMiddleware

#category, #target, #transaction_options

Instance Method Summary collapse

Methods inherited from AgentMiddleware

#build_transaction_name, #initialize

Methods included from Agent::Instrumentation::MiddlewareTracing

#_nr_has_middleware_tracing, #build_transaction_options, #call, #capture_http_response_code, #capture_response_attributes, #capture_response_content_length, #capture_response_content_type, #events, #merge_first_middleware_options, #note_transaction_started

Constructor Details

This class inherits a constructor from NewRelic::Rack::AgentMiddleware

Instance Method Details

#should_instrument?(env, status, headers) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/new_relic/rack/browser_monitoring.rb', line 61

def should_instrument?(env, status, headers)
  NewRelic::Agent.config[:'browser_monitoring.auto_instrument'] &&
    status == 200 &&
    !env[ALREADY_INSTRUMENTED_KEY] &&
    html?(headers) &&
    !attachment?(headers) &&
    !streaming?(env, headers)
end

#traced_call(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/rack/browser_monitoring.rb', line 37

def traced_call(env)
  result = @app.call(env)
  (status, headers, response) = result

  js_to_inject = NewRelic::Agent.browser_timing_header
  if (js_to_inject != NewRelic::EMPTY_STR) && should_instrument?(env, status, headers)
    response_string = autoinstrument_source(response, js_to_inject)
    if headers.key?(CONTENT_LENGTH)
      content_length = response_string ? response_string.bytesize : 0
      headers[CONTENT_LENGTH] = content_length.to_s
    end

    env[ALREADY_INSTRUMENTED_KEY] = true
    if response_string
      response = ::Rack::Response.new(response_string, status, headers)
      response.finish
    else
      result
    end
  else
    result
  end
end