Skip to content

Commit

Permalink
RUBY-891 moved browser_key to new system
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarg authored and rsaul committed Oct 9, 2012
1 parent 73c88a6 commit dfeca16
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 90 deletions.
33 changes: 14 additions & 19 deletions lib/new_relic/agent/beacon_configuration.rb
Expand Up @@ -12,37 +12,32 @@ class BeaconConfiguration
# the static portion of the RUM footer - this part does not vary
# by which request is in progress
attr_reader :browser_timing_static_footer

# the application id we include in the javascript -
# crossreferences with the application id on the collectors
attr_reader :application_id

# the key used for browser monitoring. This is different from
# the account key
attr_reader :browser_monitoring_key

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

# whether RUM is enabled or not - determined based on server and
# local config
attr_reader :rum_enabled

# whether JSONP is used to communicate with the Beacon or not
attr_reader :rum_jsonp

# RUM footer command used for 'finish' - based on whether JSONP is
# being used. 'nrfj' for JSONP, otherwise 'nrf2'
attr_reader :finish_command

# A static javascript header that is identical for every account
# and application
JS_HEADER = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"

# Creates a new browser configuration data. Argument is a hash
# of configuration values from the server
def initialize(connect_data)
@browser_monitoring_key = connect_data['browser_key']
def initialize(connect_data={})
@application_id = connect_data['application_id']
@beacon = connect_data['beacon']
@rum_enabled = connect_data['rum.enabled']
Expand All @@ -57,7 +52,7 @@ def initialize(connect_data)
NewRelic::Control.instance.log.debug("Real User Monitoring is using JSONP protocol") if @rum_jsonp
@finish_command = @rum_jsonp ? 'nrfj' : 'nrf2'
end

# returns a memoized version of the bytes in the license key for
# obscuring transaction names in the javascript
def license_bytes
Expand All @@ -67,7 +62,7 @@ def license_bytes
end
@license_bytes
end

# 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
Expand All @@ -77,16 +72,16 @@ def build_load_file_js(connect_data)
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', '')
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();
};
Expand All @@ -101,12 +96,12 @@ def build_load_file_js(connect_data)
def javascript_header
JS_HEADER.dup
end

# Returns the header string, properly html-safed if needed
def build_browser_timing_header
return "" if !@rum_enabled
return "" if @browser_monitoring_key.nil?
return "" if Agent.config[:browser_key].nil?

value = javascript_header
if value.respond_to?(:html_safe)
value.html_safe
Expand Down
29 changes: 14 additions & 15 deletions lib/new_relic/agent/browser_monitoring.rb
Expand Up @@ -34,8 +34,8 @@ def queue_time
# page as is reasonably possible - that is, before any style or
# javascript inclusions, but after any header-related meta tags
def browser_timing_header
if NewRelic::Agent.instance.beacon_configuration.nil? ||
!NewRelic::Agent.is_transaction_traced? ||
if NewRelic::Agent.instance.beacon_configuration.nil? ||
!NewRelic::Agent.is_transaction_traced? ||
!NewRelic::Agent.is_execution_traced? ||
NewRelic::Agent::TransactionInfo.get.ignore_end_user?
return ""
Expand All @@ -54,16 +54,17 @@ def browser_timing_header
# page as is reasonably possible.
def browser_timing_footer
config = NewRelic::Agent.instance.beacon_configuration

if config.nil? ||
!config.rum_enabled ||
config.browser_monitoring_key.nil? ||
!NewRelic::Agent.is_transaction_traced? ||
!NewRelic::Agent.is_execution_traced? ||
NewRelic::Agent::TransactionInfo.get.ignore_end_user?
return ""

if config.nil? ||
!config.rum_enabled ||
Agent.config[:browser_key].nil? ||
Agent.config[:browser_key].empty? ||
!NewRelic::Agent.is_transaction_traced? ||
!NewRelic::Agent.is_execution_traced? ||
NewRelic::Agent::TransactionInfo.get.ignore_end_user?
return ""
end

generate_footer_js(config)
end

Expand Down Expand Up @@ -127,7 +128,7 @@ def mobile_header_found_in?(request)

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

private
Expand All @@ -136,9 +137,7 @@ def generate_footer_js(config)
if browser_monitoring_start_time
application_id = config.application_id
beacon = config.beacon
license_key = config.browser_monitoring_key

footer_js_string(config, beacon, license_key, application_id)
footer_js_string(config, beacon, Agent.config[:browser_key], application_id)
else
''
end
Expand Down
56 changes: 29 additions & 27 deletions test/new_relic/agent/beacon_configuration_test.rb
Expand Up @@ -6,21 +6,22 @@ def test_initialize_basic
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
assert_equal true, bc.rum_enabled
assert_equal '', bc.browser_timing_header
%w[application_id browser_monitoring_key beacon].each do |method|
%w[application_id beacon].each do |method|
value = bc.send(method.to_sym)
assert_equal nil, value, "Expected #{method} to be nil, but was #{value.inspect}"
end
end

def test_initialize_with_real_data
connect_data = {'browser_key' => 'a browser monitoring key', 'application_id' => 'an application id', 'beacon' => 'a beacon', 'rum_enabled' => true}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
assert_equal(true, bc.rum_enabled)
assert_equal('a browser monitoring key', bc.browser_monitoring_key)
assert_equal('an application id', bc.application_id)
assert_equal('a beacon', bc.beacon)
s = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
assert_equal(s, bc.browser_timing_header)
with_config(:browser_key => 'a key') do
connect_data = {'application_id' => 'an application id', 'beacon' => 'a beacon', 'rum_enabled' => true}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
assert_equal(true, bc.rum_enabled)
assert_equal('an application id', bc.application_id)
assert_equal('a beacon', bc.beacon)
s = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
assert_equal(s, bc.browser_timing_header)
end
end

def test_license_bytes_nil
Expand Down Expand Up @@ -62,48 +63,49 @@ def test_build_browser_timing_header_enabled_but_no_key
bc.instance_eval { @rum_enabled = true; @browser_monitoring_key = nil }
assert_equal '', bc.build_browser_timing_header, "should not return a header when browser_monitoring_key is nil"
end

def test_build_browser_timing_header_enabled_with_key
connect_data = {}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
bc.instance_eval { @browser_monitoring_key = 'a browser monitoring key' }
assert(bc.build_browser_timing_header.include?('NREUMQ'), "header should be generated when rum is enabled and browser monitoring key is set")
with_config(:browser_key => 'a browser monitoring key') do
bc = NewRelic::Agent::BeaconConfiguration.new
assert(bc.build_browser_timing_header.include?('NREUMQ'),
"header should be generated when rum is enabled and browser monitoring key is set")
end
end

def test_build_browser_timing_header_should_html_safe_header
mock_javascript = mock('javascript')
connect_data = {'browser_key' => 'a' * 40}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
assert_equal('a' * 40, bc.instance_variable_get('@browser_monitoring_key'), "should save the key from the config")
bc.expects(:javascript_header).returns(mock_javascript)
mock_javascript.expects(:respond_to?).with(:html_safe).returns(true)
mock_javascript.expects(:html_safe)
bc.build_browser_timing_header
with_config(:browser_key => 'a' * 40) do
mock_javascript = mock('javascript')
bc = NewRelic::Agent::BeaconConfiguration.new
bc.expects(:javascript_header).returns(mock_javascript)
mock_javascript.expects(:respond_to?).with(:html_safe).returns(true)
mock_javascript.expects(:html_safe)
bc.build_browser_timing_header
end
end

def test_build_load_file_js_load_episodes_file_false
connect_data = {'rum.load_episodes_file' => false}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"
assert_equal(s, bc.build_load_file_js(connect_data))
end

def test_build_load_file_js_load_episodes_file_missing
connect_data = {}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";e.async=true;e.src=\"\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"

assert_equal(s, bc.build_load_file_js(connect_data))
end

def test_build_load_file_js_load_episodes_file_present
connect_data = {'rum.load_episodes_file' => true}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";e.async=true;e.src=\"\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"

assert_equal(s, bc.build_load_file_js(connect_data))
end

def test_build_load_file_js_load_episodes_file_with_episodes_url
connect_data = {'episodes_url' => 'an episodes url'}
bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
Expand Down
58 changes: 31 additions & 27 deletions test/new_relic/agent/browser_monitoring_test.rb
Expand Up @@ -10,19 +10,19 @@ class NewRelic::Agent::BrowserMonitoringTest < Test::Unit::TestCase

def setup
NewRelic::Agent.manual_start
config = {:disable_mobile_headers => false }
NewRelic::Agent.config.apply_config(config)
@browser_monitoring_key = "fred"
@config = {:disable_mobile_headers => false, :browser_key => 'browserKey' }
NewRelic::Agent.config.apply_config(@config)
@episodes_file = "this_is_my_file"
NewRelic::Agent.instance.instance_eval do
@beacon_configuration = NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => true, "browser_key" => "browserKey", "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file", 'rum.jsonp' => true})
@beacon_configuration = NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => true, "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file", 'rum.jsonp' => true})
end

def teardown
Thread.current[:last_metric_frame] = nil
NewRelic::Agent::TransactionInfo.clear
NewRelic::Agent.config.remove_config(config)
NewRelic::Agent.config.remove_config(@config)
end

def teardown
mocha_teardown
end

Expand Down Expand Up @@ -102,10 +102,12 @@ def test_browser_timing_footer
end

def test_browser_timing_footer_with_no_browser_key_rum_enabled
browser_timing_header
NewRelic::Agent.instance.expects(:beacon_configuration).returns( NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => true, "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file"}))
footer = browser_timing_footer
assert_equal "", footer
with_config(:browser_key => '') do
browser_timing_header
NewRelic::Agent.instance.expects(:beacon_configuration).returns( NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => true, "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file"}))
footer = browser_timing_footer
assert_equal "", footer
end
end

def test_browser_timing_footer_with_no_browser_key_rum_disabled
Expand Down Expand Up @@ -139,7 +141,6 @@ def test_browser_timing_footer_with_no_beacon_configuration
assert_equal "", footer
end


def test_browser_timing_footer_disable_all_tracing
browser_timing_header
footer = nil
Expand All @@ -158,14 +159,15 @@ def test_browser_timing_footer_disable_transaction_tracing
assert_equal "", footer
end

def test_browser_timing_footer_browser_monitoring_key_missing
fake_config = mock('beacon configuration')
NewRelic::Agent.instance.expects(:beacon_configuration).returns(fake_config)
fake_config.expects(:nil?).returns(false)
fake_config.expects(:rum_enabled).returns(true)
fake_config.expects(:browser_monitoring_key).returns(nil)
self.expects(:generate_footer_js).never
assert_equal('', browser_timing_footer, "should not return a footer when there is no key")
def test_browser_timing_footer_browser_key_missing
with_config(:browser_key => '') do
fake_config = mock('beacon configuration')
NewRelic::Agent.instance.expects(:beacon_configuration).returns(fake_config)
fake_config.expects(:nil?).returns(false)
fake_config.expects(:rum_enabled).returns(true)
self.expects(:generate_footer_js).never
assert_equal('', browser_timing_footer, "should not return a footer when there is no key")
end
end

def test_generate_footer_js_null_case
Expand All @@ -174,14 +176,16 @@ def test_generate_footer_js_null_case
end

def test_generate_footer_js_with_start_time
self.expects(:browser_monitoring_start_time).returns(Time.at(100))
fake_bc = mock('beacon configuration')
fake_bc.expects(:application_id).returns(1)
fake_bc.expects(:beacon).returns('beacon')
fake_bc.expects(:browser_monitoring_key).returns('a' * 40)
NewRelic::Agent.instance.stubs(:beacon_configuration).returns(fake_bc)
self.expects(:footer_js_string).with(NewRelic::Agent.instance.beacon_configuration, 'beacon', 'a' * 40, 1).returns('footer js')
assert_equal('footer js', generate_footer_js(NewRelic::Agent.instance.beacon_configuration), 'should generate and return the footer JS when there is a start time')
with_config(:browser_key => 'a' * 40) do
self.expects(:browser_monitoring_start_time).returns(Time.at(100))
fake_bc = mock('beacon configuration')
fake_bc.expects(:application_id).returns(1)
fake_bc.expects(:beacon).returns('beacon')
NewRelic::Agent.instance.stubs(:beacon_configuration).returns(fake_bc)
self.expects(:footer_js_string).with(NewRelic::Agent.instance.beacon_configuration, 'beacon', 'a' * 40, 1).returns('footer js')
assert_equal('footer js', generate_footer_js(NewRelic::Agent.instance.beacon_configuration),
'should generate and return the footer JS when there is a start time')
end
end

def test_browser_monitoring_transaction_name_basic
Expand Down
6 changes: 4 additions & 2 deletions test/new_relic/rack/browser_monitoring_test.rb
Expand Up @@ -46,9 +46,10 @@ def app
def setup
super
clear_cookies
@config = { :browser_key => 'some browser key' }
NewRelic::Agent.config.apply_config(@config)
NewRelic::Agent.manual_start
config = NewRelic::Agent::BeaconConfiguration.new("browser_key" => "browserKey",
"application_id" => "apId",
config = NewRelic::Agent::BeaconConfiguration.new("application_id" => "apId",
"beacon"=>"beacon",
"episodes_url"=>"this_is_my_file")
NewRelic::Agent.instance.stubs(:beacon_configuration).returns(config)
Expand All @@ -60,6 +61,7 @@ def teardown
clear_cookies
mocha_teardown
TestApp.doc = nil
NewRelic::Agent.config.remove_config(@config)
end

def test_make_sure_header_is_set
Expand Down

0 comments on commit dfeca16

Please sign in to comment.