Class: OneApm::Rack::BrowserMonitoring

Inherits:
MiddlewareBase show all
Defined in:
lib/one_apm/rack/browser_monitoring.rb

Constant Summary collapse

OA_SCAN_LIMIT =
50_000
OA_ALREADY_INSTRUMENTED_KEY =
"oneapm.browser_monitoring_already_instrumented"
OA_REMOTE_IP =
"action_dispatch.remote_ip"
OA_CHARSET_RE =
/<\s*meta[^>]+charset\s*=[^>]*>/im.freeze
OA_X_UA_COMPATIBLE_RE =
/<\s*meta[^>]+http-equiv\s*=\s*['"]x-ua-compatible['"][^>]*>/im.freeze

Constants included from MiddlewareTracing

MiddlewareTracing::OA_TXN_STARTED_KEY

Instance Attribute Summary

Attributes inherited from MiddlewareBase

#category, #target, #transaction_options

Instance Method Summary collapse

Methods inherited from MiddlewareBase

#build_transaction_name, #initialize, #middleware_ignore?

Methods included from MiddlewareHelper

#close_old_response, #gather_source, #middleware_ignore?

Methods included from MiddlewareTracing

#_oa_has_middleware_tracing, #build_transaction_options, #call, #capture_http_response_code, #events, #merge_first_middleware_options, #note_transaction_started

Constructor Details

This class inherits a constructor from OneApm::Rack::MiddlewareBase

Instance Method Details

#autoinstrument_source(response, headers, js_to_inject) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/one_apm/rack/browser_monitoring.rb', line 84

def autoinstrument_source(response, headers, js_to_inject)
  begin
    source = gather_source(response)
    close_old_response(response)
    return nil unless source
    inject_js(source, headers, js_to_inject)
  rescue => e
    OneApm::Manager.logger.debug "Skipping RUM instrumentation on exception.", e
    nil
  end
end

#calculate_content_length(source) ⇒ Object

String does not respond to ‘bytesize’ in 1.8.6. Fortunately String#length returns bytes rather than characters in 1.8.6 so we can use that instead.



172
173
174
175
176
177
178
# File 'lib/one_apm/rack/browser_monitoring.rb', line 172

def calculate_content_length(source)
  if source.respond_to?(:bytesize)
    source.bytesize
  else
    source.length
  end
end

#find_body_end(source) ⇒ Object



150
151
152
# File 'lib/one_apm/rack/browser_monitoring.rb', line 150

def find_body_end source
  source.rindex("</body>")
end

#find_body_start(beginning_of_source) ⇒ Object



146
147
148
# File 'lib/one_apm/rack/browser_monitoring.rb', line 146

def find_body_start(beginning_of_source)
  beginning_of_source.index("<body")
end

#find_charset_position(beginning_of_source) ⇒ Object



160
161
162
163
# File 'lib/one_apm/rack/browser_monitoring.rb', line 160

def find_charset_position(beginning_of_source)
  match = OA_CHARSET_RE.match(beginning_of_source)
  match.end(0) if match
end

#find_end_of_head_open(beginning_of_source) ⇒ Object



165
166
167
168
# File 'lib/one_apm/rack/browser_monitoring.rb', line 165

def find_end_of_head_open(beginning_of_source)
  head_open = beginning_of_source.index("<head")
  beginning_of_source.index(">", head_open) + 1 if head_open
end

#find_x_ua_compatible_position(beginning_of_source) ⇒ Object



155
156
157
158
# File 'lib/one_apm/rack/browser_monitoring.rb', line 155

def find_x_ua_compatible_position(beginning_of_source)
  match = OA_X_UA_COMPATIBLE_RE.match(beginning_of_source)
  match.end(0) if match
end


135
136
137
138
139
140
141
142
143
144
# File 'lib/one_apm/rack/browser_monitoring.rb', line 135

def footer_index source
  if body_end = find_body_end(source)
    body_end
  else
    msg = "Skipping RUM instrumentation. Unable to find </body> tag from document."
    OneApm::Manager.logger.log_once(:warn, :rum_insertion_failure, msg)
    OneApm::Manager.logger.debug(msg)
    nil
  end
end

#header_index(source) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/one_apm/rack/browser_monitoring.rb', line 113

def header_index source
  beginning_of_source = source[0..OA_SCAN_LIMIT]
  if body_start = find_body_start(beginning_of_source)
    meta_tag_positions = [
      find_x_ua_compatible_position(beginning_of_source),
      find_charset_position(beginning_of_source)
    ].compact

      if !meta_tag_positions.empty?
        insertion_index = meta_tag_positions.max
      else
        insertion_index = find_end_of_head_open(beginning_of_source) || body_start
      end
    insertion_index
  else
    msg = "Skipping RUM instrumentation. Unable to find <body> tag in first #{OA_SCAN_LIMIT} bytes of document."
    OneApm::Manager.logger.log_once(:warn, :rum_insertion_failure, msg)
    OneApm::Manager.logger.debug(msg)
    nil
  end
end

#inject_js(source, headers, js_to_inject) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/one_apm/rack/browser_monitoring.rb', line 96

def inject_js source, headers, js_to_inject
  position = OneApm::Manager.config[:'browser_monitoring.position']
  insertion_index = (position.empty? || position.to_sym != :footer) ? header_index(source) : footer_index(source)
  if insertion_index
    source = source[0...insertion_index] \
      << js_to_inject \
      << source[insertion_index..-1]
    if headers['Content-Length']
      headers['Content-Length'] = calculate_content_length(source).to_s
    end
  else
    OneApm::Manager.logger.debug "Skipping RUM instrumentation. Could not properly determine location to inject script."
  end
  source
end

#ip_valid?(env) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/one_apm/rack/browser_monitoring.rb', line 62

def ip_valid?(env)
  whitelist_ips = OneApm::Manager.config[:'browser_monitoring.whitelist_ips']
  return true if whitelist_ips.empty? || env[OA_REMOTE_IP].to_s.empty?
  OneApm::Manager.logger.debug "Remote IP: #{env[OA_REMOTE_IP]}"
  begin
    client = IPAddr.new(env[OA_REMOTE_IP].to_s).to_i 
    whitelist_ips.split(",").any? do |ip|
      low_ip, high_ip = ip.split("-").map(&:strip)
      low = IPAddr.new(low_ip).to_i
      if high_ip
        high = IPAddr.new(high_ip).to_i
        (low..high) === client
      else
        low == client
      end
    end
  rescue => e
    OneApm::Manager.logger.error "Configuration for browser_monitoring.whitelist_ips has problems:#{whitelist_ips}"
    true
  end
end

#is_attachment?(headers) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/one_apm/rack/browser_monitoring.rb', line 53

def is_attachment?(headers)
  headers['Content-Disposition'].to_s.include?('attachment')
end

#is_html?(headers) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/one_apm/rack/browser_monitoring.rb', line 49

def is_html?(headers)
  headers["Content-Type"] && headers["Content-Type"].include?("text/html")
end

#is_streaming?(env) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/one_apm/rack/browser_monitoring.rb', line 57

def is_streaming?(env)
  return false unless defined?(ActionController::Live)
  env['action_controller.instance'].class.included_modules.include?(ActionController::Live)
end

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

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/one_apm/rack/browser_monitoring.rb', line 39

def should_instrument?(env, status, headers)
  OneApm::Manager.config[:'browser_monitoring.auto_instrument'] &&
    status == 200 &&
    !env[OA_ALREADY_INSTRUMENTED_KEY] &&
    is_html?(headers) &&
    !is_attachment?(headers) &&
    !is_streaming?(env) && 
    ip_valid?(env)
end

#traced_call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/one_apm/rack/browser_monitoring.rb', line 20

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

  js_to_inject = OneApm::Manager.browser_timing_header
  if (js_to_inject != "") && should_instrument?(env, result[0], result[1])
    response_string = autoinstrument_source(result[2], result[1], js_to_inject)

    env[OA_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