Class: NewRelic::Agent::JavaScriptInstrumentor

Inherits:
Object
  • Object
show all
Includes:
Coerce
Defined in:
lib/new_relic/agent/javascript_instrumentor.rb

Constant Summary collapse

RUM_KEY_LENGTH =
13
BEACON_KEY =
'beacon'.freeze
ERROR_BEACON_KEY =
'errorBeacon'.freeze
LICENSE_KEY_KEY =
'licenseKey'.freeze
APPLICATIONID_KEY =
'applicationID'.freeze
TRANSACTION_NAME_KEY =
'transactionName'.freeze
QUEUE_TIME_KEY =
'queueTime'.freeze
APPLICATION_TIME_KEY =
'applicationTime'.freeze
AGENT_KEY =
'agent'.freeze
SSL_FOR_HTTP_KEY =
'sslForHttp'.freeze
ATTS_KEY =
'atts'.freeze
ATTS_USER_SUBKEY =
'u'.freeze
ATTS_AGENT_SUBKEY =
'a'.freeze

Instance Method Summary collapse

Methods included from Coerce

boolean_int!, float, float!, int, int!, int_or_nil, log_failure, scalar, string, value_or_nil

Constructor Details

#initialize(event_listener) ⇒ JavaScriptInstrumentor

Returns a new instance of JavaScriptInstrumentor.



15
16
17
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 15

def initialize(event_listener)
  event_listener.subscribe(:initial_configuration_complete, &method(:log_configuration))
end

Instance Method Details

#add_attributes(data, txn) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 183

def add_attributes(data, txn)
  return unless txn

  atts = {}
  append_custom_attributes!(txn, atts)
  append_agent_attributes!(txn, atts)

  unless atts.empty?
    json = ::JSON.dump(atts)
    data[ATTS_KEY] = obfuscator.obfuscate(json)
  end
end

#add_ssl_for_http(data) ⇒ Object



177
178
179
180
181
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 177

def add_ssl_for_http(data)
  ssl_for_http = NewRelic::Agent.config[:'browser_monitoring.ssl_for_http']

  data[SSL_FOR_HTTP_KEY] = ssl_for_http if ssl_for_http
end

#append_agent_attributes!(txn, atts) ⇒ Object



208
209
210
211
212
213
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 208

def append_agent_attributes!(txn, atts)
  agent_attributes = txn.attributes.agent_attributes_for(NewRelic::Agent::AttributeFilter::DST_BROWSER_MONITORING)
  unless agent_attributes.empty?
    atts[ATTS_AGENT_SUBKEY] = agent_attributes
  end
end

#append_custom_attributes!(txn, atts) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 196

def append_custom_attributes!(txn, atts)
  custom_attributes = txn.attributes.custom_attributes_for(NewRelic::Agent::AttributeFilter::DST_BROWSER_MONITORING)
  if custom_attributes.empty?
    NewRelic::Agent.logger.debug("#{self.class}: No custom attributes found to append.")
    return
  end

  NewRelic::Agent.logger.debug("#{self.class}: Appending the following custom attribute keys for browser " \
    "monitoring: #{custom_attributes.keys}")
  atts[ATTS_USER_SUBKEY] = custom_attributes
end

#browser_timing_config(state, nonce = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 117

def browser_timing_config(state, nonce = nil)
  txn = state.current_transaction
  return '' if txn.nil?

  txn.freeze_name_and_execute_if_not_ignored do
    data = data_for_js_agent(txn)
    json = ::JSON.dump(data)
    return html_safe_if_needed("\n<script type=\"text/javascript\"#{create_nonce(nonce)}>window.NREUM||(NREUM={});NREUM.info=#{json}</script>")
  end

  ''
end

#browser_timing_header(nonce = nil) ⇒ Object

THREAD_LOCAL_ACCESS



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 97

def browser_timing_header(nonce = nil) # THREAD_LOCAL_ACCESS
  return '' unless js_enabled_and_ready? # fast exit

  state = NewRelic::Agent::Tracer.state

  return '' unless insert_js?(state) # slower exit

  bt_config = browser_timing_config(state, nonce)
  return '' if bt_config.empty?

  bt_config + browser_timing_loader(nonce)
rescue => e
  ::NewRelic::Agent.logger.debug('Failure during RUM browser_timing_header construction', e)
  ''
end

#browser_timing_loader(nonce = nil) ⇒ Object



113
114
115
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 113

def browser_timing_loader(nonce = nil)
  html_safe_if_needed("\n<script type=\"text/javascript\"#{create_nonce(nonce)}>#{Agent.config[:js_agent_loader]}</script>")
end

#create_nonce(nonce = nil) ⇒ Object



130
131
132
133
134
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 130

def create_nonce(nonce = nil)
  return '' unless nonce

  " nonce=\"#{nonce.to_s}\""
end

#data_for_js_agent(transaction) ⇒ Object

NOTE: Internal prototyping may override this, so leave name stable!



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 150

def data_for_js_agent(transaction)
  queue_time_in_seconds = [transaction.queue_time, 0.0].max
  start_time_in_seconds = [transaction.start_time, 0.0].max
  app_time_in_seconds = Process.clock_gettime(Process::CLOCK_REALTIME) - start_time_in_seconds

  queue_time_in_millis = (queue_time_in_seconds * 1000.0).round
  app_time_in_millis = (app_time_in_seconds * 1000.0).round

  transaction_name = transaction.best_name || ::NewRelic::Agent::UNKNOWN_METRIC

  data = {
    BEACON_KEY => NewRelic::Agent.config[:beacon],
    ERROR_BEACON_KEY => NewRelic::Agent.config[:error_beacon],
    LICENSE_KEY_KEY => NewRelic::Agent.config[:browser_key],
    APPLICATIONID_KEY => NewRelic::Agent.config[:application_id],
    TRANSACTION_NAME_KEY => obfuscator.obfuscate(transaction_name),
    QUEUE_TIME_KEY => queue_time_in_millis,
    APPLICATION_TIME_KEY => app_time_in_millis,
    AGENT_KEY => NewRelic::Agent.config[:js_agent_file]
  }

  add_ssl_for_http(data)
  add_attributes(data, transaction)

  data
end

#enabled?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 41

def enabled?
  Agent.config[:'rum.enabled'] && !!Agent.config[:beacon]
end

#html_safe_if_needed(string) ⇒ Object



215
216
217
218
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 215

def html_safe_if_needed(string)
  string = string.html_safe if string.respond_to?(:html_safe)
  string
end

#insert_js?(state) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 74

def insert_js?(state)
  if !state.current_transaction
    ::NewRelic::Agent.logger.debug('Not in transaction. Skipping browser instrumentation.')
    false
  elsif !state.is_execution_traced?
    ::NewRelic::Agent.logger.debug('Execution is not traced. Skipping browser instrumentation.')
    false
  elsif state.current_transaction.ignore_enduser?
    ::NewRelic::Agent.logger.debug('Ignore end user for this transaction is set. Skipping browser instrumentation.')
    false
  else
    true
  end
rescue => e
  ::NewRelic::Agent.logger.debug('Failure during insert_js', e)
  false
end

#js_enabled_and_ready?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 49

def js_enabled_and_ready?
  if !enabled?
    ::NewRelic::Agent.logger.log_once(:debug, :js_agent_disabled,
      'JS agent instrumentation is disabled.')
    false
  elsif missing_config?(:js_agent_loader)
    ::NewRelic::Agent.logger.log_once(:debug, :missing_js_agent_loader,
      'Missing :js_agent_loader. Skipping browser instrumentation.')
    false
  elsif missing_config?(:beacon)
    ::NewRelic::Agent.logger.log_once(:debug, :missing_beacon,
      'Beacon configuration not received (yet?). Skipping browser instrumentation.')
    false
  elsif missing_config?(:browser_key)
    ::NewRelic::Agent.logger.log_once(:debug, :missing_browser_key,
      'Browser key is not set. Skipping browser instrumentation.')
    false
  else
    true
  end
rescue => e
  ::NewRelic::Agent.logger.debug("Failure during 'js_enabled_and_ready?'", e)
  false
end

#log_configurationObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 19

def log_configuration
  NewRelic::Agent.logger.debug("JS agent loader requested: #{NewRelic::Agent.config[:'browser_monitoring.loader']}",
    "JS agent loader debug: #{NewRelic::Agent.config[:'browser_monitoring.debug']}",
    "JS agent loader version: #{NewRelic::Agent.config[:'browser_monitoring.loader_version']}")

  if !NewRelic::Agent.config[:'rum.enabled']
    NewRelic::Agent.logger.debug('Real User Monitoring is disabled for this agent. Edit your configuration to change this.')
  end

  warn_if_requested_version_not_used
end

#missing_config?(key) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 92

def missing_config?(key)
  value = NewRelic::Agent.config[key]
  value.nil? || value.empty?
end

#obfuscatorObject



45
46
47
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 45

def obfuscator
  @obfuscator ||= NewRelic::Agent::Obfuscator.new(NewRelic::Agent.config[:license_key], RUM_KEY_LENGTH)
end

#warn_if_requested_version_not_usedObject



31
32
33
34
35
36
37
38
39
# File 'lib/new_relic/agent/javascript_instrumentor.rb', line 31

def warn_if_requested_version_not_used
  return if missing_config?(:'browser_monitoring.version')
  return unless missing_config?(:js_agent_loader)

  requested_version = NewRelic::Agent.config[:'browser_monitoring.version']
  NewRelic::Agent.logger.warn("Requested browser_monitoring.version '#{requested_version}' did not return a " \
    "browser agent loader; please ensure '#{requested_version}' is available and supported. No browser " \
    'monitoring will be injected for this connect.')
end