Module: NewRelic::Agent::BrowserMonitoring

Included in:
Agent
Defined in:
lib/new_relic/agent/browser_monitoring.rb

Overview

This module contains support for Real User Monitoring - the javascript generation and configuration

Defined Under Namespace

Classes: DummyTransaction

Constant Summary collapse

@@dummy_txn =
DummyTransaction.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.beacon_url(request) ⇒ Object



130
131
132
# File 'lib/new_relic/agent/browser_monitoring.rb', line 130

def beacon_url(request)
  "#{request.scheme || 'http'}://#{Agent.config[:beacon]}/mobile/1/#{Agent.config[:browser_key]}"
end

.browser_monitoring_app_timeObject



88
89
90
# File 'lib/new_relic/agent/browser_monitoring.rb', line 88

def browser_monitoring_app_time
  clamp_to_positive(((Time.now - browser_monitoring_start_time).to_f * 1000.0).round)
end

.browser_monitoring_queue_timeObject



84
85
86
# File 'lib/new_relic/agent/browser_monitoring.rb', line 84

def browser_monitoring_queue_time
  clamp_to_positive((current_transaction.queue_time.to_f * 1000.0).round)
end

.browser_monitoring_start_timeObject



101
102
103
# File 'lib/new_relic/agent/browser_monitoring.rb', line 101

def browser_monitoring_start_time
  NewRelic::Agent::TransactionInfo.get.start_time
end

.browser_monitoring_transaction_nameObject



80
81
82
# File 'lib/new_relic/agent/browser_monitoring.rb', line 80

def browser_monitoring_transaction_name
  current_transaction.name || ::NewRelic::Agent::UNKNOWN_METRIC
end

.clamp_to_positive(value) ⇒ Object



96
97
98
99
# File 'lib/new_relic/agent/browser_monitoring.rb', line 96

def clamp_to_positive(value)
  return 0.0 if value < 0
  value
end

.current_transactionObject



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

def current_transaction
  NewRelic::Agent::TransactionInfo.get.transaction || @@dummy_txn
end

.insert_mobile_response_header(request, response) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/new_relic/agent/browser_monitoring.rb', line 111

def insert_mobile_response_header(request, response)
  if mobile_header_found_in?(request) &&
      NewRelic::Agent.instance.beacon_configuration

    config = NewRelic::Agent.instance.beacon_configuration

    response['X-NewRelic-Beacon-Url'] = beacon_url(request)

    payload = %[ ["#{Agent.config[:application_id]}","#{obfuscate(config, browser_monitoring_transaction_name)}",#{browser_monitoring_queue_time},#{browser_monitoring_app_time}] ]
    response['X-NewRelic-App-Server-Metrics'] = payload
  end
end

.mobile_header_found_in?(request) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/new_relic/agent/browser_monitoring.rb', line 124

def mobile_header_found_in?(request)
  headers = ['HTTP_X_NEWRELIC_MOBILE_TRACE', 'X_NEWRELIC_MOBILE_TRACE',
             'X-NewRelic-Mobile-Trace']
  headers.inject(false){|i,m| i || (request.env[m] == 'true')}
end

.obfuscate(config, text) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/new_relic/agent/browser_monitoring.rb', line 68

def obfuscate(config, text)
  obfuscated = ""
  key_bytes = config.license_bytes
  index = 0
  text.each_byte{|byte|
    obfuscated.concat((byte ^ key_bytes[index % 13].to_i))
    index+=1
  }

  [obfuscated].pack("m0").gsub("\n", '')
end

.timingsObject



105
106
107
108
109
# File 'lib/new_relic/agent/browser_monitoring.rb', line 105

def self.timings
  NewRelic::Agent::Instrumentation::BrowserMonitoringTimings.new(
    current_transaction.queue_time,
    NewRelic::Agent::TransactionInfo.get)
end

Instance Method Details

This method returns a string suitable for inclusion in a page

  • known as ‘manual instrumentation’ for Real User

Monitoring. Can return either a script tag with associated javascript, or in the case of disabled Real User Monitoring, an empty string

This is the footer string - it should be placed as low in the page as is reasonably possible.



57
58
59
60
61
62
63
64
# File 'lib/new_relic/agent/browser_monitoring.rb', line 57

def browser_timing_footer
  if insert_js?
    NewRelic::Agent::Transaction.freeze_name
    generate_footer_js(NewRelic::Agent.instance.beacon_configuration)
  else
    ""
  end
end

#browser_timing_headerObject

This method returns a string suitable for inclusion in a page

  • known as ‘manual instrumentation’ for Real User

Monitoring. Can return either a script tag with associated javascript, or in the case of disabled Real User Monitoring, an empty string

This is the header string - it should be placed as high in the page as is reasonably possible - that is, before any style or javascript inclusions, but after any header-related meta tags



41
42
43
44
45
46
47
# File 'lib/new_relic/agent/browser_monitoring.rb', line 41

def browser_timing_header
  if insert_js?
    NewRelic::Agent.instance.beacon_configuration.browser_timing_header
  else
    ""
  end
end