Class: NewRelic::Agent::BeaconConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/beacon_configuration.rb

Overview

This class contains the configuration data for setting up RUM headers and footers - acts as a cache of this data so we don’t need to look it up or reconfigure it every request

Constant Summary collapse

JS_HEADER =

A static javascript header that is identical for every account and application

"<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connect_data) ⇒ BeaconConfiguration

Creates a new browser configuration data. Argument is a hash of configuration values from the server



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/beacon_configuration.rb', line 44

def initialize(connect_data)
  @browser_monitoring_key = connect_data['browser_key']
  @application_id = connect_data['application_id']
  @beacon = connect_data['beacon']
  @rum_enabled = connect_data['rum.enabled']
  @rum_enabled = true if @rum_enabled.nil?
  NewRelic::Control.instance.log.warn("Real User Monitoring is disabled for this agent. Edit your configuration to change this.") unless @rum_enabled
  @browser_timing_header = build_browser_timing_header
  NewRelic::Control.instance.log.debug("Browser timing header: #{@browser_timing_header.inspect}")
  @browser_timing_static_footer = build_load_file_js(connect_data)
  NewRelic::Control.instance.log.debug("Browser timing static footer: #{@browser_timing_static_footer.inspect}")
  @rum_jsonp = connect_data['rum.jsonp']
  @rum_jsonp = true if @rum_jsonp.nil?
  NewRelic::Control.instance.log.debug("Real User Monitoring is using JSONP protocol") if @rum_jsonp
  @finish_command = @rum_jsonp ?  'nrfj' : 'nrf2'
end

Instance Attribute Details

#application_idObject (readonly)

the application id we include in the javascript - crossreferences with the application id on the collectors



18
19
20
# File 'lib/new_relic/agent/beacon_configuration.rb', line 18

def application_id
  @application_id
end

#beaconObject (readonly)

which beacon we should report to - set by startup of the agent



25
26
27
# File 'lib/new_relic/agent/beacon_configuration.rb', line 25

def beacon
  @beacon
end

#browser_monitoring_keyObject (readonly)

the key used for browser monitoring. This is different from the account key



22
23
24
# File 'lib/new_relic/agent/beacon_configuration.rb', line 22

def browser_monitoring_key
  @browser_monitoring_key
end

#browser_timing_headerObject (readonly)

the statically generated header - generated when the beacon configuration is created - does not vary per page



10
11
12
# File 'lib/new_relic/agent/beacon_configuration.rb', line 10

def browser_timing_header
  @browser_timing_header
end

the static portion of the RUM footer - this part does not vary by which request is in progress



14
15
16
# File 'lib/new_relic/agent/beacon_configuration.rb', line 14

def browser_timing_static_footer
  @browser_timing_static_footer
end

#finish_commandObject (readonly)

RUM footer command used for ‘finish’ - based on whether JSONP is being used. ‘nrfj’ for JSONP, otherwise ‘nrf2’



36
37
38
# File 'lib/new_relic/agent/beacon_configuration.rb', line 36

def finish_command
  @finish_command
end

#rum_enabledObject (readonly)

whether RUM is enabled or not - determined based on server and local config



29
30
31
# File 'lib/new_relic/agent/beacon_configuration.rb', line 29

def rum_enabled
  @rum_enabled
end

#rum_jsonpObject (readonly)

whether JSONP is used to communicate with the Beacon or not



32
33
34
# File 'lib/new_relic/agent/beacon_configuration.rb', line 32

def rum_jsonp
  @rum_jsonp
end

Instance Method Details

#build_browser_timing_headerObject

Returns the header string, properly html-safed if needed



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/new_relic/agent/beacon_configuration.rb', line 106

def build_browser_timing_header
  return "" if !@rum_enabled
  return "" if @browser_monitoring_key.nil?
  
  value = javascript_header
  if value.respond_to?(:html_safe)
    value.html_safe
  else
    value
  end
end

#build_load_file_js(connect_data) ⇒ Object

returns a snippet of text that does not change per-transaction. Is empty when rum is disabled, or we are not including the episodes file dynamically (i.e. the user includes it themselves)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/new_relic/agent/beacon_configuration.rb', line 75

def build_load_file_js(connect_data)
  js = <<-EOS
if (!NREUMQ.f) { NREUMQ.f=function() {
NREUMQ.push(["load",new Date().getTime()]);
EOS
    
  if connect_data.fetch('rum.load_episodes_file', true)
    episodes_url = connect_data.fetch('episodes_url', '')          
    js << <<-EOS
var e=document.createElement(\"script\");
e.type=\"text/javascript\";e.async=true;e.src=\"#{episodes_url}\";
document.body.appendChild(e);
EOS
  end
    
  js << <<-EOS
if(NREUMQ.a)NREUMQ.a();
};
NREUMQ.a=window.onload;window.onload=NREUMQ.f;
};
EOS
  js
end

#javascript_headerObject

returns a copy of the static javascript header, in case people are munging strings somewhere down the line



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

def javascript_header
  JS_HEADER.dup
end

#license_bytesObject

returns a memoized version of the bytes in the license key for obscuring transaction names in the javascript



63
64
65
66
67
68
69
# File 'lib/new_relic/agent/beacon_configuration.rb', line 63

def license_bytes
  if @license_bytes.nil?
    @license_bytes = []
    NewRelic::Control.instance.license_key.each_byte {|byte| @license_bytes << byte}
  end
  @license_bytes
end