newrelic_rpm 3.4.2.1 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (49) hide show
  1. data/CHANGELOG +47 -2
  2. data/lib/new_relic/agent.rb +5 -5
  3. data/lib/new_relic/agent/agent.rb +88 -177
  4. data/lib/new_relic/agent/beacon_configuration.rb +33 -47
  5. data/lib/new_relic/agent/browser_monitoring.rb +26 -33
  6. data/lib/new_relic/agent/configuration/defaults.rb +21 -13
  7. data/lib/new_relic/agent/configuration/manager.rb +28 -14
  8. data/lib/new_relic/agent/configuration/server_source.rb +8 -5
  9. data/lib/new_relic/agent/database.rb +37 -22
  10. data/lib/new_relic/agent/error_collector.rb +32 -31
  11. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +4 -3
  12. data/lib/new_relic/agent/new_relic_service.rb +21 -19
  13. data/lib/new_relic/agent/pipe_channel_manager.rb +13 -13
  14. data/lib/new_relic/agent/sql_sampler.rb +9 -28
  15. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +21 -24
  16. data/lib/new_relic/agent/stats_engine/transactions.rb +20 -12
  17. data/lib/new_relic/agent/transaction_sample_builder.rb +5 -3
  18. data/lib/new_relic/agent/transaction_sampler.rb +43 -47
  19. data/lib/new_relic/control/frameworks/rails.rb +9 -4
  20. data/lib/new_relic/control/frameworks/rails3.rb +10 -0
  21. data/lib/new_relic/noticed_error.rb +18 -8
  22. data/lib/new_relic/rack.rb +4 -0
  23. data/lib/new_relic/rack/browser_monitoring.rb +2 -0
  24. data/lib/new_relic/rack/error_collector.rb +56 -0
  25. data/lib/new_relic/version.rb +3 -3
  26. data/newrelic.yml +0 -12
  27. data/newrelic_rpm.gemspec +6 -3
  28. data/test/new_relic/agent/agent/connect_test.rb +78 -113
  29. data/test/new_relic/agent/agent/start_test.rb +2 -2
  30. data/test/new_relic/agent/agent/start_worker_thread_test.rb +6 -33
  31. data/test/new_relic/agent/agent_test.rb +20 -6
  32. data/test/new_relic/agent/agent_test_controller_test.rb +7 -5
  33. data/test/new_relic/agent/beacon_configuration_test.rb +54 -60
  34. data/test/new_relic/agent/browser_monitoring_test.rb +88 -74
  35. data/test/new_relic/agent/configuration/manager_test.rb +21 -21
  36. data/test/new_relic/agent/configuration/server_source_test.rb +21 -4
  37. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  38. data/test/new_relic/agent/mock_scope_listener.rb +3 -0
  39. data/test/new_relic/agent/new_relic_service_test.rb +56 -17
  40. data/test/new_relic/agent/pipe_channel_manager_test.rb +4 -1
  41. data/test/new_relic/agent/rpm_agent_test.rb +1 -0
  42. data/test/new_relic/agent/stats_engine_test.rb +12 -7
  43. data/test/new_relic/agent/transaction_sampler_test.rb +106 -102
  44. data/test/new_relic/agent_test.rb +10 -9
  45. data/test/new_relic/control_test.rb +1 -17
  46. data/test/new_relic/rack/browser_monitoring_test.rb +11 -5
  47. data/test/new_relic/rack/error_collector_test.rb +74 -0
  48. data/test/test_helper.rb +1 -1
  49. metadata +9 -7
@@ -188,7 +188,7 @@ class NewRelic::Agent::Agent::StartTest < Test::Unit::TestCase
188
188
  def test_monitoring_negative
189
189
  log = mocked_log
190
190
  with_config(:monitor_mode => false) do
191
- log.expects(:send).with(:warn, "Agent configured not to send data in this environment - edit newrelic.yml to change this")
191
+ log.expects(:send).with(:warn, "Agent configured not to send data in this environment.")
192
192
  assert !monitoring?
193
193
  end
194
194
  end
@@ -202,7 +202,7 @@ class NewRelic::Agent::Agent::StartTest < Test::Unit::TestCase
202
202
  def test_has_license_key_negative
203
203
  with_config(:license_key => false) do
204
204
  log = mocked_log
205
- log.expects(:send).with(:error, 'No license key found. Please edit your newrelic.yml file and insert your license key.')
205
+ log.expects(:send).with(:warn, 'No license key found in newrelic.yml config.')
206
206
  assert !has_license_key?
207
207
  end
208
208
  end
@@ -7,8 +7,6 @@ class NewRelic::Agent::Agent::StartWorkerThreadTest < Test::Unit::TestCase
7
7
  self.expects(:catch_errors).yields
8
8
  self.expects(:connect).with('connection_options')
9
9
  @connected = true
10
- self.expects(:check_transaction_sampler_status)
11
- self.expects(:check_sql_sampler_status)
12
10
  self.expects(:log_worker_loop_start)
13
11
  self.expects(:create_and_run_worker_loop)
14
12
  deferred_work!('connection_options')
@@ -23,49 +21,24 @@ class NewRelic::Agent::Agent::StartWorkerThreadTest < Test::Unit::TestCase
23
21
  deferred_work!('connection_options')
24
22
  end
25
23
 
26
- def test_check_transaction_sampler_status_enabled
27
- with_config(:developer_mode => false) do
28
- @should_send_samples = true
29
- @transaction_sampler = mock('transaction_sampler')
30
- @transaction_sampler.expects(:enable)
31
- check_transaction_sampler_status
32
- end
33
- end
34
-
35
- def test_check_transaction_sampler_status_devmode
36
- with_config(:developer_mode => true) do
37
- @should_send_samples = false
38
- @transaction_sampler = mock('transaction_sampler')
39
- @transaction_sampler.expects(:enable)
40
- check_transaction_sampler_status
41
- end
42
- end
43
-
44
- def test_check_transaction_sampler_status_disabled
45
- with_config(:developer_mode => false) do
46
- @should_send_samples = false
47
- @transaction_sampler = mock('transaction_sampler')
48
- @transaction_sampler.expects(:disable)
49
- check_transaction_sampler_status
50
- end
51
- end
52
-
53
24
  def test_log_worker_loop_start
54
- @report_period = 30
55
25
  log = mocked_log
56
26
  log.expects(:info).with("Reporting performance data every 30 seconds.")
57
27
  log.expects(:debug).with("Running worker loop")
58
- log_worker_loop_start
28
+ with_config(:data_report_period => 30) do
29
+ log_worker_loop_start
30
+ end
59
31
  end
60
32
 
61
33
  def test_create_and_run_worker_loop
62
- @report_period = 30
63
34
  @should_send_samples = true
64
35
  wl = mock('worker loop')
65
36
  NewRelic::Agent::WorkerLoop.expects(:new).returns(wl)
66
37
  wl.expects(:run).with(30).yields
67
38
  self.expects(:transmit_data)
68
- create_and_run_worker_loop
39
+ with_config(:data_report_period => 30) do
40
+ create_and_run_worker_loop
41
+ end
69
42
  end
70
43
 
71
44
  def test_handle_force_restart
@@ -14,7 +14,7 @@ module NewRelic
14
14
  'Agent should use PipeService when directed to report to pipe channel')
15
15
  assert_equal 123, @agent.service.channel_id
16
16
  end
17
-
17
+
18
18
  def test_transmit_data_should_transmit
19
19
  @agent.instance_eval { transmit_data }
20
20
  assert @agent.service.agent_data.any?
@@ -24,7 +24,7 @@ module NewRelic
24
24
  NewRelic::Agent::Database.expects(:close_connections)
25
25
  @agent.instance_eval { transmit_data }
26
26
  end
27
-
27
+
28
28
  def test_transmit_data_should_not_close_db_connections_if_forked
29
29
  NewRelic::Agent::Database.expects(:close_connections).never
30
30
  @agent.after_fork
@@ -39,6 +39,20 @@ module NewRelic
39
39
  assert_equal([], @agent.send(:harvest_transaction_traces), 'should return transaction traces')
40
40
  end
41
41
 
42
+ def test_harvest_and_send_slowest_sample
43
+ with_config(:'transaction_tracer.explain_threshold' => 2,
44
+ :'transaction_tracer.explain_enabled' => true,
45
+ :'transaction_tracer.record_sql' => 'raw') do
46
+ trace = stub('transaction trace', :force_persist => true,
47
+ :truncate => 4000)
48
+ trace.expects(:prepare_to_send).with(:record_sql => :raw,
49
+ :explain_sql => 2,
50
+ :keep_backtraces => true)
51
+ @agent.instance_variable_set(:@traces, [ trace ])
52
+ @agent.send :harvest_and_send_slowest_sample
53
+ end
54
+ end
55
+
42
56
  def test_harvest_timeslice_data
43
57
  assert_equal({}, @agent.send(:harvest_timeslice_data),
44
58
  'should return timeslice data')
@@ -48,7 +62,7 @@ module NewRelic
48
62
  2000.times do |i|
49
63
  @agent.stats_engine.stats_hash[i.to_s] = NewRelic::StatsBase.new
50
64
  end
51
-
65
+
52
66
  harvest = Thread.new do
53
67
  @agent.send(:harvest_timeslice_data)
54
68
  end
@@ -56,7 +70,7 @@ module NewRelic
56
70
  app = Thread.new do
57
71
  @agent.stats_engine.stats_hash["a"] = NewRelic::StatsBase.new
58
72
  end
59
-
73
+
60
74
  assert_nothing_raised do
61
75
  [app, harvest].each{|t| t.join}
62
76
  end
@@ -92,7 +106,7 @@ module NewRelic
92
106
  }
93
107
  assert_equal(1, @agent.unsent_errors_size)
94
108
  end
95
-
109
+
96
110
  def test_unsent_traces_size_empty
97
111
  @agent.instance_eval {
98
112
  @traces = nil
@@ -121,7 +135,7 @@ module NewRelic
121
135
  }
122
136
  assert_equal(1, @agent.unsent_timeslice_data, "should have the key from above")
123
137
  end
124
-
138
+
125
139
  def test_merge_data_from_all_three_empty
126
140
  unsent_timeslice_data = mock('unsent timeslice data')
127
141
  unsent_errors = mock('unsent errors')
@@ -7,7 +7,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
7
7
  self.controller_class = NewRelic::Agent::AgentTestController
8
8
 
9
9
  attr_accessor :agent, :engine
10
-
10
+
11
11
  def test_initialization
12
12
  # Suggested by cee-dub for merb tests. I'm actually amazed if our tests work with merb.
13
13
  if defined?(Merb::Router)
@@ -23,7 +23,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
23
23
  Rails.application.routes.draw do
24
24
  match '/:controller/:action.:format'
25
25
  match '/:controller/:action'
26
- end
26
+ end
27
27
  end
28
28
 
29
29
  if defined?(Rails) && Rails.respond_to?(:application) && Rails.application.respond_to?(:routes)
@@ -41,7 +41,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
41
41
  end
42
42
  @engine = @agent.stats_engine
43
43
  end
44
-
44
+
45
45
  # Normally you can do this with #setup but for some reason in rails 2.0.2
46
46
  # setup is not called.
47
47
  if NewRelic::Control.instance.rails_version <= '2.1.0'
@@ -281,9 +281,11 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
281
281
  def test_controller_params
282
282
  agent.transaction_sampler.reset!
283
283
  get :index, 'number' => "001-555-1212"
284
- s = agent.transaction_sampler.harvest(nil, 0.0)
284
+ s = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
285
+ agent.transaction_sampler.harvest(nil)
286
+ end
285
287
  assert_equal 1, s.size
286
- assert_equal 6, s.first.params.size
288
+ assert_equal 5, s.first.params.size
287
289
  end
288
290
 
289
291
 
@@ -2,38 +2,33 @@ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper
2
2
  require "new_relic/agent/beacon_configuration"
3
3
  class NewRelic::Agent::BeaconConfigurationTest < Test::Unit::TestCase
4
4
  def test_initialize_basic
5
- connect_data = {}
6
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
7
- assert_equal true, bc.rum_enabled
8
- assert_equal '', bc.browser_timing_header
9
- %w[application_id browser_monitoring_key beacon].each do |method|
10
- value = bc.send(method.to_sym)
11
- assert_equal nil, value, "Expected #{method} to be nil, but was #{value.inspect}"
5
+ with_config(:application_id => 'an application id',
6
+ :beacon => 'beacon', :'rum.enabled' => true) do
7
+ bc = NewRelic::Agent::BeaconConfiguration.new
8
+ assert_equal true, bc.enabled?
9
+ assert_equal '', bc.browser_timing_header
12
10
  end
13
11
  end
14
12
 
15
13
  def test_initialize_with_real_data
16
- connect_data = {'browser_key' => 'a browser monitoring key', 'application_id' => 'an application id', 'beacon' => 'a beacon', 'rum_enabled' => true}
17
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
18
- assert_equal(true, bc.rum_enabled)
19
- assert_equal('a browser monitoring key', bc.browser_monitoring_key)
20
- assert_equal('an application id', bc.application_id)
21
- assert_equal('a beacon', bc.beacon)
22
- s = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
23
- assert_equal(s, bc.browser_timing_header)
14
+ with_config(:browser_key => 'a key', :application_id => 'an application id',
15
+ :beacon => 'beacon', :'rum.enabled' => true) do
16
+ bc = NewRelic::Agent::BeaconConfiguration.new
17
+ assert bc.enabled?
18
+ s = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
19
+ assert_equal(s, bc.browser_timing_header)
20
+ end
24
21
  end
25
22
 
26
23
  def test_license_bytes_nil
27
24
  with_config(:license_key => 'a' * 40) do
28
- connect_data = {}
29
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
25
+ bc = NewRelic::Agent::BeaconConfiguration.new
30
26
  assert_equal([97] * 40, bc.license_bytes, 'should return the bytes of the license key')
31
27
  end
32
28
  end
33
29
 
34
30
  def test_license_bytes_existing_bytes
35
- connect_data = {}
36
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
31
+ bc = NewRelic::Agent::BeaconConfiguration.new
37
32
  bc.instance_eval { @license_bytes = [97] * 40 }
38
33
  NewRelic::Agent.config.expects(:[]).with('license_key').never
39
34
  assert_equal([97] * 40, bc.license_bytes, "should return the cached value if it exists")
@@ -41,8 +36,7 @@ class NewRelic::Agent::BeaconConfigurationTest < Test::Unit::TestCase
41
36
 
42
37
  def test_license_bytes_should_set_instance_cache
43
38
  with_config(:license_key => 'a' * 40) do
44
- connect_data = {}
45
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
39
+ bc = NewRelic::Agent::BeaconConfiguration.new
46
40
  bc.instance_eval { @license_bytes = nil }
47
41
  bc.license_bytes
48
42
  assert_equal([97] * 40, bc.instance_variable_get('@license_bytes'), "should cache the license bytes for later")
@@ -50,64 +44,64 @@ class NewRelic::Agent::BeaconConfigurationTest < Test::Unit::TestCase
50
44
  end
51
45
 
52
46
  def test_build_browser_timing_header_disabled
53
- connect_data = {}
54
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
47
+ bc = NewRelic::Agent::BeaconConfiguration.new
55
48
  bc.instance_eval { @rum_enabled = false }
56
49
  assert_equal '', bc.build_browser_timing_header, "should not return a header when rum enabled is false"
57
50
  end
58
51
 
59
52
  def test_build_browser_timing_header_enabled_but_no_key
60
- connect_data = {}
61
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
53
+ bc = NewRelic::Agent::BeaconConfiguration.new
62
54
  bc.instance_eval { @rum_enabled = true; @browser_monitoring_key = nil }
63
55
  assert_equal '', bc.build_browser_timing_header, "should not return a header when browser_monitoring_key is nil"
64
56
  end
65
-
57
+
66
58
  def test_build_browser_timing_header_enabled_with_key
67
- connect_data = {}
68
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
69
- bc.instance_eval { @browser_monitoring_key = 'a browser monitoring key' }
70
- assert(bc.build_browser_timing_header.include?('NREUMQ'), "header should be generated when rum is enabled and browser monitoring key is set")
59
+ with_config(:browser_key => 'a browser monitoring key', :beacon => 'beacon') do
60
+ bc = NewRelic::Agent::BeaconConfiguration.new
61
+ assert(bc.build_browser_timing_header.include?('NREUMQ'),
62
+ "header should be generated when rum is enabled and browser monitoring key is set")
63
+ end
71
64
  end
72
65
 
73
66
  def test_build_browser_timing_header_should_html_safe_header
74
- mock_javascript = mock('javascript')
75
- connect_data = {'browser_key' => 'a' * 40}
76
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
77
- assert_equal('a' * 40, bc.instance_variable_get('@browser_monitoring_key'), "should save the key from the config")
78
- bc.expects(:javascript_header).returns(mock_javascript)
79
- mock_javascript.expects(:respond_to?).with(:html_safe).returns(true)
80
- mock_javascript.expects(:html_safe)
81
- bc.build_browser_timing_header
67
+ with_config(:browser_key => 'a' * 40, :beacon => 'beacon') do
68
+ mock_javascript = mock('javascript')
69
+ bc = NewRelic::Agent::BeaconConfiguration.new
70
+ bc.expects(:javascript_header).returns(mock_javascript)
71
+ mock_javascript.expects(:respond_to?).with(:html_safe).returns(true)
72
+ mock_javascript.expects(:html_safe)
73
+ bc.build_browser_timing_header
74
+ end
82
75
  end
83
-
76
+
84
77
  def test_build_load_file_js_load_episodes_file_false
85
- connect_data = {'rum.load_episodes_file' => false}
86
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
87
- 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"
88
- assert_equal(s, bc.build_load_file_js(connect_data))
78
+ with_config(:'rum.load_episodes_file' => false) do
79
+ bc = NewRelic::Agent::BeaconConfiguration.new
80
+ 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"
81
+ assert_equal(s, bc.build_load_file_js)
82
+ end
89
83
  end
90
-
84
+
91
85
  def test_build_load_file_js_load_episodes_file_missing
92
- connect_data = {}
93
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
94
- 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"
95
-
96
- assert_equal(s, bc.build_load_file_js(connect_data))
86
+ with_config(:'rum.load_episodes_file' => '') do
87
+ bc = NewRelic::Agent::BeaconConfiguration.new
88
+ 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"
89
+ assert_equal(s, bc.build_load_file_js)
90
+ end
97
91
  end
98
92
 
99
93
  def test_build_load_file_js_load_episodes_file_present
100
- connect_data = {'rum.load_episodes_file' => true}
101
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
102
- 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"
103
-
104
- assert_equal(s, bc.build_load_file_js(connect_data))
94
+ bc = NewRelic::Agent::BeaconConfiguration.new
95
+ # 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"
96
+ s = "if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";\ne.src=((\"http:\"===document.location.protocol)?\"http:\":\"https:\") + \"//\" +\n \"\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\n"
97
+ assert_equal(s, bc.build_load_file_js)
105
98
  end
106
-
107
- def test_build_load_file_js_load_episodes_file_with_episodes_url
108
- connect_data = {'episodes_url' => 'an episodes url'}
109
- bc = NewRelic::Agent::BeaconConfiguration.new(connect_data)
110
- assert(bc.build_load_file_js(connect_data).include?('an episodes url'),
111
- "should include the episodes url by default")
99
+
100
+ def test_build_load_file_js_load_episodes_file_with_episodes_file
101
+ with_config(:episodes_file => 'an episodes url') do
102
+ bc = NewRelic::Agent::BeaconConfiguration.new
103
+ assert(bc.build_load_file_js.include?('an episodes url'),
104
+ "should include the episodes url by default")
105
+ end
112
106
  end
113
107
  end
@@ -7,25 +7,37 @@ require 'ostruct'
7
7
  class NewRelic::Agent::BrowserMonitoringTest < Test::Unit::TestCase
8
8
  include NewRelic::Agent::BrowserMonitoring
9
9
  include NewRelic::Agent::Instrumentation::ControllerInstrumentation
10
-
10
+
11
11
  def setup
12
12
  NewRelic::Agent.manual_start
13
- config = {:disable_mobile_headers => false }
14
- NewRelic::Agent.config.apply_config(config)
15
- @browser_monitoring_key = "fred"
13
+ @config = {
14
+ :beacon => 'beacon',
15
+ :disable_mobile_headers => false,
16
+ :browser_key => 'browserKey',
17
+ :application_id => 5,
18
+ :'rum.enabled' => true,
19
+ :episodes_file => 'this_is_my_file',
20
+ :'rum.jsonp' => true
21
+ }
22
+ NewRelic::Agent.config.apply_config(@config)
16
23
  @episodes_file = "this_is_my_file"
17
24
  NewRelic::Agent.instance.instance_eval do
18
- @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})
25
+ @beacon_configuration = NewRelic::Agent::BeaconConfiguration.new
19
26
  end
27
+
28
+ def teardown
20
29
  Thread.current[:last_metric_frame] = nil
21
30
  NewRelic::Agent::TransactionInfo.clear
22
- NewRelic::Agent.config.remove_config(config)
31
+ NewRelic::Agent.config.remove_config(@config)
23
32
  end
24
33
 
25
- def teardown
26
34
  mocha_teardown
27
35
  end
28
36
 
37
+ def test_auto_instrumentation_config_defaults_to_enabled
38
+ assert NewRelic::Agent.config[:'browser_monitoring.auto_instrument']
39
+ end
40
+
29
41
  def test_browser_monitoring_start_time_is_reset_each_request_when_auto_instrument_is_disabled
30
42
  controller = Object.new
31
43
  def controller.perform_action_without_newrelic_trace(method, options={});
@@ -59,15 +71,17 @@ class NewRelic::Agent::BrowserMonitoringTest < Test::Unit::TestCase
59
71
  end
60
72
 
61
73
  def test_browser_timing_header_with_rum_enabled_not_specified
62
- NewRelic::Agent.instance.expects(:beacon_configuration).at_least_once.returns( NewRelic::Agent::BeaconConfiguration.new({"browser_key" => "browserKey", "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file"}))
74
+ NewRelic::Agent.instance.expects(:beacon_configuration).at_least_once.returns( NewRelic::Agent::BeaconConfiguration.new)
63
75
  header = browser_timing_header
64
76
  assert_equal "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>", header
65
77
  end
66
78
 
67
79
  def test_browser_timing_header_with_rum_enabled_false
68
- NewRelic::Agent.instance.expects(:beacon_configuration).twice.returns( NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => false, "browser_key" => "browserKey", "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file"}))
69
- header = browser_timing_header
70
- assert_equal "", header
80
+ with_config(:'rum.enabled' => false) do
81
+ NewRelic::Agent.instance.expects(:beacon_configuration).twice.returns( NewRelic::Agent::BeaconConfiguration.new)
82
+ header = browser_timing_header
83
+ assert_equal "", header
84
+ end
71
85
  end
72
86
 
73
87
  def test_browser_timing_header_disable_all_tracing
@@ -93,22 +107,28 @@ class NewRelic::Agent::BrowserMonitoringTest < Test::Unit::TestCase
93
107
  snippet = '<script type="text/javascript">if (!NREUMQ.f) { NREUMQ.f=function() {
94
108
  NREUMQ.push(["load",new Date().getTime()]);
95
109
  var e=document.createElement("script");'
96
- assert footer.include?(snippet), "Expected footer to include snippet: #{snippet}, but instead was #{footer}"
110
+ assert(footer.include?(snippet),
111
+ "Expected footer to include snippet: #{snippet}, but instead was #{footer}")
97
112
  end
98
113
  end
99
114
 
100
115
  def test_browser_timing_footer_with_no_browser_key_rum_enabled
101
- browser_timing_header
102
- 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"}))
103
- footer = browser_timing_footer
104
- assert_equal "", footer
116
+ with_config(:browser_key => '') do
117
+ browser_timing_header
118
+ NewRelic::Agent.instance.expects(:beacon_configuration).returns(NewRelic::Agent::BeaconConfiguration.new)
119
+ footer = browser_timing_footer
120
+ assert_equal "", footer
121
+ end
105
122
  end
106
123
 
107
124
  def test_browser_timing_footer_with_no_browser_key_rum_disabled
108
- browser_timing_header
109
- NewRelic::Agent.instance.expects(:beacon_configuration).returns( NewRelic::Agent::BeaconConfiguration.new({"rum.enabled" => false, "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file"}))
110
- footer = browser_timing_footer
111
- assert_equal "", footer
125
+ with_config(:'rum.enabled' => false) do
126
+ browser_timing_header
127
+ NewRelic::Agent.instance.expects(:beacon_configuration) \
128
+ .returns(NewRelic::Agent::BeaconConfiguration.new)
129
+ footer = browser_timing_footer
130
+ assert_equal "", footer
131
+ end
112
132
  end
113
133
 
114
134
  def test_browser_timing_footer_with_rum_enabled_not_specified
@@ -116,16 +136,18 @@ var e=document.createElement("script");'
116
136
 
117
137
  license_bytes = [];
118
138
  ("a" * 13).each_byte {|byte| license_bytes << byte}
119
- config = NewRelic::Agent::BeaconConfiguration.new({"browser_key" => "browserKey", "application_id" => "apId", "beacon"=>"beacon", "episodes_url"=>"this_is_my_file", "license_bytes" => license_bytes})
139
+ config = NewRelic::Agent::BeaconConfiguration.new
120
140
  config.expects(:license_bytes).returns(license_bytes).at_least_once
121
141
  NewRelic::Agent.instance.expects(:beacon_configuration).returns(config).at_least_once
122
142
  footer = browser_timing_footer
123
143
  beginning_snippet = '<script type="text/javascript">if (!NREUMQ.f) { NREUMQ.f=function() {
124
144
  NREUMQ.push(["load",new Date().getTime()]);
125
145
  var e=document.createElement("script");'
126
- ending_snippet = "])</script>"
127
- assert(footer.include?(beginning_snippet), "expected footer to include beginning snippet: #{beginning_snippet}, but was #{footer}")
128
- assert(footer.include?(ending_snippet), "expected footer to include ending snippet: #{ending_snippet}, but was #{footer}")
146
+ ending_snippet = "]);</script>"
147
+ assert(footer.include?(beginning_snippet),
148
+ "expected footer to include beginning snippet: #{beginning_snippet}, but was #{footer}")
149
+ assert(footer.include?(ending_snippet),
150
+ "expected footer to include ending snippet: #{ending_snippet}, but was #{footer}")
129
151
  end
130
152
 
131
153
  def test_browser_timing_footer_with_no_beacon_configuration
@@ -135,7 +157,6 @@ var e=document.createElement("script");'
135
157
  assert_equal "", footer
136
158
  end
137
159
 
138
-
139
160
  def test_browser_timing_footer_disable_all_tracing
140
161
  browser_timing_header
141
162
  footer = nil
@@ -154,30 +175,31 @@ var e=document.createElement("script");'
154
175
  assert_equal "", footer
155
176
  end
156
177
 
157
- def test_browser_timing_footer_browser_monitoring_key_missing
158
- fake_config = mock('beacon configuration')
159
- NewRelic::Agent.instance.expects(:beacon_configuration).returns(fake_config)
160
- fake_config.expects(:nil?).returns(false)
161
- fake_config.expects(:rum_enabled).returns(true)
162
- fake_config.expects(:browser_monitoring_key).returns(nil)
163
- self.expects(:generate_footer_js).never
164
- assert_equal('', browser_timing_footer, "should not return a footer when there is no key")
178
+ def test_browser_timing_footer_browser_key_missing
179
+ with_config(:browser_key => '') do
180
+ fake_config = mock('beacon configuration')
181
+ NewRelic::Agent.instance.expects(:beacon_configuration).returns(fake_config)
182
+ fake_config.expects(:nil?).returns(false)
183
+ fake_config.expects(:enabled?).returns(true)
184
+ self.expects(:generate_footer_js).never
185
+ assert_equal('', browser_timing_footer, "should not return a footer when there is no key")
186
+ end
165
187
  end
166
188
 
167
189
  def test_generate_footer_js_null_case
168
190
  self.expects(:browser_monitoring_start_time).returns(nil)
169
191
  assert_equal('', generate_footer_js(NewRelic::Agent.instance.beacon_configuration), "should not send javascript when there is no start time")
170
192
  end
171
-
193
+
172
194
  def test_generate_footer_js_with_start_time
173
- self.expects(:browser_monitoring_start_time).returns(Time.at(100))
174
- fake_bc = mock('beacon configuration')
175
- fake_bc.expects(:application_id).returns(1)
176
- fake_bc.expects(:beacon).returns('beacon')
177
- fake_bc.expects(:browser_monitoring_key).returns('a' * 40)
178
- NewRelic::Agent.instance.stubs(:beacon_configuration).returns(fake_bc)
179
- self.expects(:footer_js_string).with(NewRelic::Agent.instance.beacon_configuration, 'beacon', 'a' * 40, 1).returns('footer js')
180
- 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')
195
+ with_config(:browser_key => 'a' * 40) do
196
+ self.expects(:browser_monitoring_start_time).returns(Time.at(100))
197
+ fake_bc = mock('beacon configuration')
198
+ NewRelic::Agent.instance.stubs(:beacon_configuration).returns(fake_bc)
199
+ self.expects(:footer_js_string).with(NewRelic::Agent.instance.beacon_configuration).returns('footer js')
200
+ assert_equal('footer js', generate_footer_js(NewRelic::Agent.instance.beacon_configuration),
201
+ 'should generate and return the footer JS when there is a start time')
202
+ end
181
203
  end
182
204
 
183
205
  def test_browser_monitoring_transaction_name_basic
@@ -199,27 +221,23 @@ var e=document.createElement("script");'
199
221
  def test_browser_monitoring_transaction_name_nil
200
222
  assert_equal('(unknown)', browser_monitoring_transaction_name, "should fill in a default when it is nil")
201
223
  end
202
-
224
+
203
225
  def test_browser_monitoring_transaction_name_when_tt_disabled
204
- @sampler = NewRelic::Agent.instance.transaction_sampler
205
- @sampler.disable
226
+ with_config(:'transaction_tracer.enabled' => false) do
227
+ perform_action_with_newrelic_trace(:name => 'disabled_transactions') do
228
+ self.class.inspect
229
+ end
206
230
 
207
- perform_action_with_newrelic_trace(:name => 'disabled_transactions') do
208
- self.class.inspect
231
+ assert_match(/disabled_transactions/, browser_monitoring_transaction_name,
232
+ "should name transaction when transaction tracing disabled")
209
233
  end
210
-
211
- assert_match(/disabled_transactions/, browser_monitoring_transaction_name,
212
- "should name transaction when transaction tracing disabled")
213
- ensure
214
- @sampler.enable
215
- end
216
-
217
-
234
+ end
235
+
218
236
  def test_browser_monitoring_start_time
219
237
  mock = mock('transaction info')
220
-
238
+
221
239
  NewRelic::Agent::TransactionInfo.set(mock)
222
-
240
+
223
241
  mock.stubs(:start_time).returns(Time.at(100))
224
242
  mock.stubs(:guid).returns('ABC')
225
243
  assert_equal(Time.at(100), browser_monitoring_start_time, "should take the value from the thread local")
@@ -234,7 +252,7 @@ var e=document.createElement("script");'
234
252
  def test_browser_monitoring_app_time_nonzero
235
253
  start = Time.now
236
254
  self.expects(:browser_monitoring_start_time).returns(start - 1)
237
- Time.expects(:now).returns(start)
255
+ Time.stubs(:now).returns(start)
238
256
  assert_equal(1000, browser_monitoring_app_time, 'should return a rounded time')
239
257
  end
240
258
 
@@ -261,10 +279,6 @@ var e=document.createElement("script");'
261
279
  end
262
280
 
263
281
  def test_footer_js_string_basic
264
- beacon = ''
265
- license_key = ''
266
- application_id = 1
267
-
268
282
  # mocking this because JRuby thinks that Time.now - Time.now
269
283
  # always takes at least 1ms
270
284
  self.expects(:browser_monitoring_app_time).returns(0)
@@ -275,21 +289,21 @@ var e=document.createElement("script");'
275
289
 
276
290
  sample = mock('transaction info')
277
291
  NewRelic::Agent::TransactionInfo.set(sample)
278
-
292
+
279
293
  sample.stubs(:start_time).returns(Time.at(100))
280
294
  sample.stubs(:guid).returns('ABC')
281
295
  sample.stubs(:transaction_name).returns('most recent transaction')
282
296
  sample.stubs(:include_guid?).returns(true)
283
297
  sample.stubs(:duration).returns(12.0)
284
298
  sample.stubs(:token).returns('0123456789ABCDEF')
285
-
299
+
286
300
  self.expects(:obfuscate).with(NewRelic::Agent.instance.beacon_configuration, 'most recent transaction').returns('most recent transaction')
287
301
  self.expects(:obfuscate).with(NewRelic::Agent.instance.beacon_configuration, 'user').returns('user')
288
302
  self.expects(:obfuscate).with(NewRelic::Agent.instance.beacon_configuration, 'account').returns('account')
289
303
  self.expects(:obfuscate).with(NewRelic::Agent.instance.beacon_configuration, 'product').returns('product')
290
304
 
291
- value = footer_js_string(NewRelic::Agent.instance.beacon_configuration, beacon, license_key, application_id)
292
- assert_equal("<script type=\"text/javascript\">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=\"this_is_my_file\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\nNREUMQ.push([\"nrfj\",\"\",\"\",1,\"most recent transaction\",0,0,new Date().getTime(),\"ABC\",\"0123456789ABCDEF\",\"user\",\"account\",\"product\"])</script>", value, "should return the javascript given some default values")
305
+ value = footer_js_string(NewRelic::Agent.instance.beacon_configuration)
306
+ assert_equal("<script type=\"text/javascript\">if (!NREUMQ.f) { NREUMQ.f=function() {\nNREUMQ.push([\"load\",new Date().getTime()]);\nvar e=document.createElement(\"script\");\ne.type=\"text/javascript\";\ne.src=((\"http:\"===document.location.protocol)?\"http:\":\"https:\") + \"//\" +\n \"this_is_my_file\";\ndocument.body.appendChild(e);\nif(NREUMQ.a)NREUMQ.a();\n};\nNREUMQ.a=window.onload;window.onload=NREUMQ.f;\n};\nNREUMQ.push([\"nrfj\",\"beacon\",\"browserKey\",5,\"most recent transaction\",0,0,new Date().getTime(),\"ABC\",\"0123456789ABCDEF\",\"user\",\"account\",\"product\"]);</script>", value, "should return the javascript given some default values")
293
307
  end
294
308
 
295
309
  def test_html_safe_if_needed_unsafed
@@ -326,11 +340,11 @@ var e=document.createElement("script");'
326
340
  output = obfuscate(NewRelic::Agent.instance.beacon_configuration, text)
327
341
  assert_equal('YCJrZXV2fih5Y25vaCFtZSR2a2ZkZSp/aXV1YyNsZHZ3cSl6YmluZCJsYiV1amllZit4aHl2YiRtZ3d4cCp7ZWhiZyNrYyZ0ZWhmZyx5ZHp3ZSVuZnh5cyt8ZGRhZiRqYCd7ZGtnYC11Z3twZCZvaXl6cix9aGdgYSVpYSh6Z2pgYSF2Znxx', output, "should output obfuscated text")
328
342
  end
329
-
343
+
330
344
  def test_no_mobile_response_header_if_no_mobile_request_header_given
331
345
  request = Rack::Request.new({})
332
346
  response = Rack::Response.new
333
-
347
+
334
348
  NewRelic::Agent::BrowserMonitoring.insert_mobile_response_header(request, response)
335
349
  assert_nil response['X-NewRelic-Beacon-Url']
336
350
  end
@@ -342,13 +356,13 @@ var e=document.createElement("script");'
342
356
  NewRelic::Agent::BrowserMonitoring.insert_mobile_response_header(request, response)
343
357
  assert_nil response['X-NewRelic-Beacon-Url']
344
358
  end
345
-
359
+
346
360
  def test_place_beacon_url_header_when_given_mobile_request_header
347
- response = mobile_transaction
361
+ response = mobile_transaction
348
362
  assert_equal('http://beacon/mobile/1/browserKey',
349
363
  response['X-NewRelic-Beacon-Url'])
350
364
  end
351
-
365
+
352
366
  def test_place_beacon_url_header_when_given_mobile_request_header_with_https
353
367
  request = Rack::Request.new('X_NEWRELIC_MOBILE_TRACE' => 'true',
354
368
  'rack.url_scheme' => 'https')
@@ -359,11 +373,11 @@ var e=document.createElement("script");'
359
373
 
360
374
  def test_place_beacon_payload_head_when_given_mobile_request_header
361
375
  Time.stubs(:now).returns(6)
362
- response = mobile_transaction
376
+ response = mobile_transaction
363
377
  txn_name = obfuscate(NewRelic::Agent.instance.beacon_configuration,
364
378
  browser_monitoring_transaction_name)
365
- expected_payload = %|["apId","#{txn_name}",#{browser_monitoring_queue_time},#{browser_monitoring_app_time}]|
366
-
379
+ expected_payload = %|["5","#{txn_name}",#{browser_monitoring_queue_time},#{browser_monitoring_app_time}]|
380
+
367
381
  assert_equal expected_payload, response['X-NewRelic-App-Server-Metrics'].strip
368
382
  end
369
383