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
@@ -67,15 +67,6 @@ module NewRelic::Agent::Configuration
67
67
  @manager.remove_config(source)
68
68
  end
69
69
 
70
- def test_source_accessors_should_be_available_as_keys
71
- source = TestSource.new
72
- @manager.apply_config(source)
73
-
74
- assert_equal 'some value', @manager[:test_config_accessor]
75
-
76
- @manager.remove_config(source)
77
- end
78
-
79
70
  def test_should_not_apply_removed_sources
80
71
  test_source = TestSource.new
81
72
  @manager.apply_config(test_source)
@@ -123,21 +114,30 @@ module NewRelic::Agent::Configuration
123
114
  .index(NewRelic::Agent::Configuration::ManualSource)
124
115
  end
125
116
 
126
- def test_app_names_single
127
- @manager.apply_config(:app_name => 'test')
128
- assert_equal ['test'], @manager.app_names
129
- end
117
+ def test_registering_a_callback
118
+ observed_value = 'old'
119
+ @manager.apply_config(:test => 'original')
130
120
 
131
- def test_app_names_list
132
- @manager.apply_config(:app_name => 'test0;test1;test2')
133
- assert_equal ['test0', 'test1', 'test2'], @manager.app_names
121
+ @manager.register_callback(:test) do |value|
122
+ observed_value = value
123
+ end
124
+ assert_equal 'original', observed_value
125
+
126
+ @manager.apply_config(:test => 'new')
127
+ assert_equal 'new', observed_value
134
128
  end
135
129
 
136
- def test_app_names_nil
137
- # nil has special meaning, so we emulate with an object that
138
- # will not respond as expected
139
- @manager.apply_config(:app_name => Object.new)
140
- assert_equal [], @manager.app_names
130
+ def test_callback_not_called_if_no_change
131
+ @manager.apply_config(:test => true, :other => false)
132
+ @manager.register_callback(:test) do |value|
133
+ state = 'wrong'
134
+ end
135
+ state = 'right'
136
+ config = {:test => true}
137
+ @manager.apply_config(config)
138
+ @manager.remove_config(config)
139
+
140
+ assert_equal 'right', state
141
141
  end
142
142
 
143
143
  class TestSource < ::Hash
@@ -5,14 +5,31 @@ module NewRelic::Agent::Configuration
5
5
  class ServerSourceTest < Test::Unit::TestCase
6
6
  def setup
7
7
  config = {
8
- 'slow_sql.enabled' => true,
9
- 'transaction_tracer.transaction_threshold' => 'apdex_f',
10
- 'error_collector.enabled' => true,
11
- 'collect_errors' => false
8
+ 'agent_config' => {
9
+ 'slow_sql.enabled' => true,
10
+ 'transaction_tracer.transaction_threshold' => 'apdex_f',
11
+ 'transaction_tracer.record_sql' => 'raw',
12
+ 'error_collector.enabled' => true
13
+ },
14
+ 'apdex_t' => 1.0,
15
+ 'collect_errors' => false,
16
+ 'collect_traces' => true
12
17
  }
13
18
  @source = ServerSource.new(config)
14
19
  end
15
20
 
21
+ def test_should_set_apdex_t
22
+ assert_equal 1.0, @source[:apdex_t]
23
+ end
24
+
25
+ def test_should_set_agent_config_values
26
+ assert_equal 'raw', @source[:'transaction_tracer.record_sql']
27
+ end
28
+
29
+ def test_should_not_dot_the_agent_config_sub_hash
30
+ assert_nil @source[:'agent_config.slow_sql.enabled']
31
+ end
32
+
16
33
  def test_should_enable_tracer_as_configured
17
34
  assert @source[:'slow_sql.enabled']
18
35
  end
@@ -77,8 +77,8 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
77
77
  assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
78
78
  sample = @agent.transaction_sampler.last_sample
79
79
  assert_not_nil sample
80
- assert_not_nil sample.params[:cpu_time], "cpu time nil: \n#{sample}"
81
- assert sample.params[:cpu_time] >= 0, "cpu time: #{sample.params[:cpu_time]},\n#{sample}"
80
+ assert_not_nil sample.params[:custom_params][:cpu_time], "cpu time nil: \n#{sample}"
81
+ assert sample.params[:custom_params][:cpu_time] >= 0, "cpu time: #{sample.params[:cpu_time]},\n#{sample}"
82
82
  assert_equal '10', sample.params[:request_params][:level]
83
83
  end
84
84
 
@@ -18,6 +18,9 @@ class NewRelic::Agent::MockScopeListener
18
18
  end
19
19
 
20
20
  def notice_scope_empty(time)
21
+ end
21
22
 
23
+ def enabled?
24
+ true
22
25
  end
23
26
  end
@@ -1,6 +1,25 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
2
 
3
3
  class NewRelicServiceTest < Test::Unit::TestCase
4
+ def initialize(*_)
5
+ [ :HTTPSuccess,
6
+ :HTTPNotFound,
7
+ :HTTPRequestEntityTooLarge,
8
+ :HTTPUnsupportedMediaType ].each do |class_name|
9
+ extend_with_mock(class_name)
10
+ end
11
+ super
12
+ end
13
+
14
+ def extend_with_mock(class_name)
15
+ if !self.class.const_defined?(class_name)
16
+ klass = self.class.const_set(class_name,
17
+ Class.new(Object.const_get(:Net).const_get(class_name)))
18
+ klass.class_eval { include HTTPResponseMock }
19
+ end
20
+ end
21
+ protected :extend_with_mock
22
+
4
23
  def setup
5
24
  @server = NewRelic::Control::Server.new('somewhere.example.com',
6
25
  30303, '10.10.10.10')
@@ -30,16 +49,16 @@ class NewRelicServiceTest < Test::Unit::TestCase
30
49
 
31
50
  def test_connect_sets_redirect_host
32
51
  assert_equal 'somewhere.example.com', @service.collector.name
33
- @service.connect
52
+ @service.connect
34
53
  assert_equal 'localhost', @service.collector.name
35
54
  end
36
-
55
+
37
56
  def test_connect_resets_cached_ip_address
38
57
  assert_equal '10.10.10.10', @service.collector.ip
39
- @service.connect
58
+ @service.connect
40
59
  assert_nil @service.collector.ip # 'localhost' resolves to nil
41
60
  end
42
-
61
+
43
62
  def test_connect_uses_proxy_collector_if_no_redirect_host
44
63
  @http_handle.reset
45
64
  @http_handle.respond_to(:get_redirect_host, nil)
@@ -85,13 +104,13 @@ class NewRelicServiceTest < Test::Unit::TestCase
85
104
  def test_error_data
86
105
  @http_handle.respond_to(:error_data, 'too human')
87
106
  response = @service.error_data([])
88
- assert_equal 'too human', response
107
+ assert_equal 'too human', response
89
108
  end
90
109
 
91
110
  def test_transaction_sample_data
92
111
  @http_handle.respond_to(:transaction_sample_data, 'MPC1000')
93
112
  response = @service.transaction_sample_data([])
94
- assert_equal 'MPC1000', response
113
+ assert_equal 'MPC1000', response
95
114
  end
96
115
 
97
116
  def test_sql_trace_data
@@ -112,21 +131,37 @@ class NewRelicServiceTest < Test::Unit::TestCase
112
131
  @service.send(:invoke_remote, :bogus_method)
113
132
  end
114
133
  end
115
-
134
+
116
135
  def test_should_connect_to_proxy_only_once_per_run
117
136
  @service.expects(:get_redirect_host).once
118
137
 
119
138
  @service.connect
120
139
  @http_handle.respond_to(:metric_data, '0')
121
140
  @service.metric_data(Time.now - 60, Time.now, {})
122
-
141
+
123
142
  @http_handle.respond_to(:transaction_sample_data, '1')
124
143
  @service.transaction_sample_data([])
125
144
 
126
145
  @http_handle.respond_to(:sql_trace_data, '2')
127
146
  @service.sql_trace_data([])
128
147
  end
129
-
148
+
149
+ # protocol 9
150
+ def test_should_raise_exception_on_413
151
+ @http_handle.respond_to(:metric_data, 'too big', 413)
152
+ assert_raise NewRelic::Agent::UnrecoverableServerException do
153
+ @service.metric_data(Time.now - 60, Time.now, {})
154
+ end
155
+ end
156
+
157
+ # protocol 9
158
+ def test_should_raise_exception_on_415
159
+ @http_handle.respond_to(:metric_data, 'too big', 415)
160
+ assert_raise NewRelic::Agent::UnrecoverableServerException do
161
+ @service.metric_data(Time.now - 60, Time.now, {})
162
+ end
163
+ end
164
+
130
165
  class HTTPHandle
131
166
  attr_accessor :read_timeout, :route_table
132
167
 
@@ -134,8 +169,17 @@ class NewRelicServiceTest < Test::Unit::TestCase
134
169
  reset
135
170
  end
136
171
 
137
- def respond_to(method, payload)
138
- register(HTTPResponse.new(Marshal.dump(payload))) do |request|
172
+ def respond_to(method, payload, code=200)
173
+ klass = HTTPSuccess
174
+ if code == 413
175
+ klass = HTTPRequestEntityTooLarge
176
+ elsif code == 415
177
+ klass = HTTPUnsupportedMediaType
178
+ elsif code >= 400
179
+ klass = HTTPServerError
180
+ end
181
+
182
+ register(klass.new(Marshal.dump(payload), code)) do |request|
139
183
  request.path.include?(method.to_s)
140
184
  end
141
185
  end
@@ -150,7 +194,7 @@ class NewRelicServiceTest < Test::Unit::TestCase
150
194
  return response
151
195
  end
152
196
  end
153
- HTTPFailure.new('not found', 404)
197
+ HTTPNotFound.new('not found', 404)
154
198
  end
155
199
 
156
200
  def reset
@@ -172,9 +216,4 @@ class NewRelicServiceTest < Test::Unit::TestCase
172
216
  @headers[key]
173
217
  end
174
218
  end
175
-
176
- HTTPResponse = Class.new(Net::HTTPOK)
177
- HTTPResponse.class_eval { include HTTPResponseMock }
178
- HTTPFailure = Class.new(Net::HTTPError)
179
- HTTPFailure.class_eval { include HTTPResponseMock }
180
219
  end
@@ -45,6 +45,7 @@ class NewRelic::Agent::PipeChannelManagerTest < Test::Unit::TestCase
45
45
  listener.stop
46
46
 
47
47
  assert_equal(3.0, engine.lookup_stats(metric).total_call_time)
48
+ engine.reset_stats
48
49
  end
49
50
 
50
51
  def test_listener_merges_transaction_traces
@@ -61,7 +62,9 @@ class NewRelic::Agent::PipeChannelManagerTest < Test::Unit::TestCase
61
62
  new_sampler = NewRelic::Agent::TransactionSampler.new
62
63
  sample = TransactionSampleTestHelper.run_sample_trace_on(new_sampler)
63
64
  new_sampler.store_force_persist(sample)
64
- listener.pipes[667].write(:transaction_traces => new_sampler.harvest([], 0))
65
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
66
+ listener.pipes[667].write(:transaction_traces => new_sampler.harvest([]))
67
+ end
65
68
  end
66
69
  Process.wait(pid)
67
70
  listener.stop
@@ -85,6 +85,7 @@ class NewRelic::Agent::RpmAgentTest < Test::Unit::TestCase # ActiveSupport::Test
85
85
  should "send_timeslice_data" do
86
86
  # this test fails due to a rubinius bug
87
87
  return if NewRelic::LanguageSupport.using_engine?('rbx')
88
+ @agent.service = NewRelic::FakeService.new
88
89
  @agent.service.expects(:metric_data).returns({ NewRelic::MetricSpec.new("/A/b/c") => 1,
89
90
  NewRelic::MetricSpec.new("/A/b/c", "/X") => 2,
90
91
  NewRelic::MetricSpec.new("/A/b/d") => 3 }.to_a)
@@ -9,7 +9,7 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
9
9
  puts e
10
10
  puts e.backtrace.join("\n")
11
11
  end
12
-
12
+
13
13
  def teardown
14
14
  @engine.harvest_timeslice_data({},{})
15
15
  mocha_teardown
@@ -193,8 +193,8 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
193
193
  ::Rubinius::Agent.stubs(:loopback).returns(agent)
194
194
  elsif NewRelic::LanguageSupport.using_version?('1.9')
195
195
  ::GC::Profiler.stubs(:enabled?).returns(true)
196
- ::GC::Profiler.stubs(:total_time).returns(1.0, 4.0)
197
- ::GC.stubs(:count).returns(1, 3)
196
+ ::GC::Profiler.stubs(:total_time).returns(1.0, 4.0)
197
+ ::GC.stubs(:count).returns(1, 3)
198
198
  elsif NewRelic::LanguageSupport.using_version?('1.8.7') &&
199
199
  RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/
200
200
  ::GC.stubs(:time).returns(1000000, 4000000)
@@ -202,20 +202,25 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
202
202
  else
203
203
  return true # no need to test if we're not collecting GC metrics
204
204
  end
205
-
205
+
206
206
  engine = NewRelic::Agent.instance.stats_engine
207
- scope = engine.push_scope "scope"
207
+ tracer = NewRelic::Agent::TransactionSampler.new
208
+ tracer.instance_variable_set(:@last_sample,
209
+ NewRelic::TransactionSample.new)
210
+ engine.transaction_sampler = tracer
208
211
  engine.start_transaction
212
+ scope = engine.push_scope "scope"
209
213
  engine.pop_scope scope, 0.01
210
214
  engine.end_transaction
211
-
215
+
212
216
  gc_stats = engine.get_stats('GC/cumulative')
213
217
  assert_equal 2, gc_stats.call_count
214
218
  assert_equal 3.0, gc_stats.total_call_time
219
+ assert_equal(3.0, tracer.last_sample.params[:custom_params][:gc_time])
215
220
  ensure
216
221
  GC.enable unless NewRelic::LanguageSupport.using_engine?('jruby')
217
222
  end
218
-
223
+
219
224
  private
220
225
  def check_time_approximate(expected, actual)
221
226
  assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
@@ -27,9 +27,8 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
27
27
  agent.stubs(:stats_engine).returns(stats_engine)
28
28
  @sampler = NewRelic::Agent::TransactionSampler.new
29
29
  stats_engine.transaction_sampler = @sampler
30
- @test_config = { 'developer_mode' => true }
30
+ @test_config = { :'transaction_tracer.enabled' => true }
31
31
  NewRelic::Agent.config.apply_config(@test_config)
32
- @sampler.configure!
33
32
  end
34
33
 
35
34
  def teardown
@@ -65,20 +64,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
65
64
  assert_equal(nil, @sampler.current_sample_id)
66
65
  end
67
66
 
68
- def test_enable
69
- assert_equal(nil, @sampler.instance_variable_get('@disabled'))
70
- @sampler.enable
71
- assert_equal(false, @sampler.instance_variable_get('@disabled'))
72
- assert_equal(@sampler, NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler'))
73
- end
74
-
75
- def test_disable
76
- assert_nil @sampler.instance_variable_get('@disabled')
77
- @sampler.disable
78
- assert @sampler.instance_variable_get('@disabled')
79
- assert_nil NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler')
80
- end
81
-
82
67
  def test_sampling_rate_equals_default
83
68
  @sampler.sampling_rate = 1
84
69
  assert_equal(1, @sampler.instance_variable_get('@sampling_rate'))
@@ -94,15 +79,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
94
79
  end
95
80
 
96
81
  def test_notice_first_scope_push_default
97
- @sampler.expects(:disabled).returns(false)
98
82
  @sampler.expects(:start_builder).with(100.0)
99
83
  @sampler.notice_first_scope_push(Time.at(100))
100
84
  end
101
85
 
102
86
  def test_notice_first_scope_push_disabled
103
- @sampler.expects(:disabled).returns(true)
104
- @sampler.expects(:start_builder).never
105
- @sampler.notice_first_scope_push(Time.at(100))
87
+ with_config(:'transaction_tracer.enabled' => false,
88
+ :developer_mode => false) do
89
+ @sampler.expects(:start_builder).never
90
+ @sampler.notice_first_scope_push(Time.at(100))
91
+ end
106
92
  end
107
93
 
108
94
  def test_notice_push_scope_no_builder
@@ -209,18 +195,20 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
209
195
  end
210
196
 
211
197
  def test_store_random_sample_no_random_sampling
212
- @sampler.instance_eval { @random_sampling = false }
213
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
214
- @sampler.store_random_sample(mock('sample'))
215
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
198
+ with_config(:'transaction_tracer.random_sample' => false) do
199
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
200
+ @sampler.store_random_sample(mock('sample'))
201
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
202
+ end
216
203
  end
217
204
 
218
205
  def test_store_random_sample_random_sampling
219
- @sampler.instance_eval { @random_sampling = true }
220
- sample = mock('sample')
221
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
222
- @sampler.store_random_sample(sample)
223
- assert_equal(sample, @sampler.instance_variable_get('@random_sample'))
206
+ with_config(:'transaction_tracer.random_sample' => true) do
207
+ sample = mock('sample')
208
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
209
+ @sampler.store_random_sample(sample)
210
+ assert_equal(sample, @sampler.instance_variable_get('@random_sample'))
211
+ end
224
212
  end
225
213
 
226
214
  def test_store_sample_for_developer_mode_in_dev_mode
@@ -232,7 +220,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
232
220
 
233
221
  def test_store_sample_for_developer_mode_no_dev
234
222
  with_config(:developer_mode => false) do
235
- @sampler.configure!
236
223
  sample = mock('sample')
237
224
  @sampler.store_sample_for_developer_mode(sample)
238
225
  assert_equal([], @sampler.instance_variable_get('@samples'))
@@ -305,13 +292,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
305
292
  end
306
293
 
307
294
  def test_notice_transaction_disabled
308
- @sampler.expects(:disabled).returns(true)
309
- @sampler.expects(:builder).never # since we're disabled
310
- @sampler.notice_transaction('foo')
295
+ builder = mock('builder')
296
+ builder.expects(:set_transaction_info).never # since we're disabled
297
+ @sampler.stubs(:builder).returns(builder)
298
+ with_config(:'transaction_tracer.enabled' => false,
299
+ :developer_mode => false) do
300
+ @sampler.notice_transaction('foo')
301
+ end
311
302
  end
312
303
 
313
304
  def test_notice_transaction_no_builder
314
- @sampler.expects(:disabled).returns(false)
315
305
  @sampler.expects(:builder).returns(nil).once
316
306
  @sampler.notice_transaction('foo')
317
307
  end
@@ -320,7 +310,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
320
310
  builder = mock('builder')
321
311
  builder.expects(:set_transaction_info).with('a path', 'a uri', {:some => :params})
322
312
  @sampler.expects(:builder).returns(builder).twice
323
- @sampler.expects(:disabled).returns(false)
324
313
  @sampler.notice_transaction('a path', 'a uri', {:some => :params})
325
314
  end
326
315
 
@@ -450,8 +439,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
450
439
  end
451
440
 
452
441
  def test_harvest_when_disabled
453
- @sampler.expects(:disabled).returns(true)
454
- assert_equal([], @sampler.harvest)
442
+ with_config(:'transaction_tracer.enabled' => false,
443
+ :developer_mode => false) do
444
+ assert_equal([], @sampler.harvest)
445
+ end
455
446
  end
456
447
 
457
448
  def test_harvest_defaults
@@ -462,8 +453,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
462
453
  @last_sample = 'a sample'
463
454
  end
464
455
 
465
- @sampler.expects(:disabled).returns(false)
466
- @sampler.expects(:add_samples_to).with([], 2.0).returns([])
456
+ @sampler.expects(:add_samples_to).with([]).returns([])
467
457
 
468
458
  assert_equal([], @sampler.harvest)
469
459
 
@@ -476,8 +466,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
476
466
  def test_harvest_with_previous_samples
477
467
  with_config(:'transaction_tracer.limit_segments' => 2000) do
478
468
  sample = mock('sample')
479
- @sampler.expects(:disabled).returns(false)
480
- @sampler.expects(:add_samples_to).with([sample], 2.0).returns([sample])
469
+ @sampler.expects(:add_samples_to).with([sample]).returns([sample])
481
470
  sample.expects(:truncate).with(2000)
482
471
  assert_equal([sample], @sampler.harvest([sample]))
483
472
  end
@@ -516,16 +505,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
516
505
  end
517
506
 
518
507
  def test_add_random_sample_to_activated
519
- @sampler.instance_eval { @random_sampling = true }
520
- sample = mock('sample')
521
- @sampler.instance_eval {
522
- @harvest_count = 3
523
- @sampling_rate = 1
524
- @random_sample = sample
525
- }
526
- result = []
527
- @sampler.add_random_sample_to(result)
528
- assert_equal([sample], result, "should add the random sample to the array")
508
+ with_config(:'transaction_tracer.random_sample' => true, :sample_rate => 1) do
509
+ sample = mock('sample')
510
+ @sampler.instance_eval {
511
+ @harvest_count = 3
512
+ @random_sample = sample
513
+ }
514
+ result = []
515
+ @sampler.add_random_sample_to(result)
516
+ assert_equal([sample], result, "should add the random sample to the array")
517
+ end
529
518
  end
530
519
 
531
520
  def test_add_random_sample_to_sampling_rate_zero
@@ -543,10 +532,9 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
543
532
 
544
533
  def test_add_samples_to_no_data
545
534
  result = []
546
- slow_threshold = 2.0
547
535
  @sampler.instance_eval { @slowest_sample = nil }
548
536
  @sampler.expects(:add_random_sample_to).with([])
549
- assert_equal([], @sampler.add_samples_to(result, slow_threshold))
537
+ assert_equal([], @sampler.add_samples_to(result))
550
538
  end
551
539
 
552
540
  def test_add_samples_to_one_result
@@ -554,30 +542,31 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
554
542
  sample.expects(:duration).returns(1).at_least_once
555
543
  sample.stubs(:force_persist).returns(false)
556
544
  result = [sample]
557
- slow_threshold = 2.0
558
545
  @sampler.instance_eval { @slowest_sample = nil }
559
546
  @sampler.expects(:add_random_sample_to).with([sample])
560
- assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
547
+ assert_equal([sample], @sampler.add_samples_to(result))
561
548
  end
562
549
 
563
550
  def test_add_samples_to_adding_slowest
564
551
  sample = mock('sample')
565
552
  sample.expects(:duration).returns(2.5).at_least_once
566
553
  result = []
567
- slow_threshold = 2.0
568
- @sampler.instance_eval { @slowest_sample = sample }
554
+ @sampler.instance_variable_set(:@slowest_sample, sample)
569
555
  @sampler.expects(:add_random_sample_to).with([sample])
570
- assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
556
+ with_config(:'transaction_tracer.transaction_threshold' => 2) do
557
+ assert_equal([sample], @sampler.add_samples_to(result))
558
+ end
571
559
  end
572
560
 
573
561
  def test_add_samples_to_under_threshold
574
562
  result = []
575
- slow_threshold = 2.0
576
563
  sample = mock('sample')
577
564
  sample.expects(:duration).returns(1.0).at_least_once
578
565
  @sampler.instance_eval { @slowest_sample = sample }
579
566
  @sampler.expects(:add_random_sample_to).with([])
580
- assert_equal([], @sampler.add_samples_to(result, slow_threshold))
567
+ with_config(:'transaction_tracer.transaction_threshold' => 2.0) do
568
+ assert_equal([], @sampler.add_samples_to(result))
569
+ end
581
570
  end
582
571
 
583
572
  def test_add_samples_to_two_sample_enter_one_sample_leave
@@ -587,69 +576,72 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
587
576
  faster_sample.expects(:duration).returns(5.0).at_least_once
588
577
  faster_sample.stubs(:force_persist).returns(false)
589
578
  result = [faster_sample]
590
- slow_threshold = 2.0
591
579
  @sampler.instance_eval { @slowest_sample = slower_sample }
592
580
  @sampler.expects(:add_random_sample_to).with([slower_sample])
593
- assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
581
+ assert_equal([slower_sample], @sampler.add_samples_to(result))
594
582
  end
595
583
 
596
584
  def test_add_samples_to_keep_older_slower_sample
597
585
  slower_sample = mock('slower')
598
586
  slower_sample.expects(:duration).returns(10.0).at_least_once
599
587
  slower_sample.stubs(:force_persist).returns(false)
600
-
588
+
601
589
  faster_sample = mock('faster')
602
590
  faster_sample.expects(:duration).returns(5.0).at_least_once
603
591
  result = [slower_sample]
604
- slow_threshold = 2.0
605
592
  @sampler.instance_eval { @slowest_sample = faster_sample }
606
593
  @sampler.expects(:add_random_sample_to).with([slower_sample])
607
- assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
594
+ assert_equal([slower_sample], @sampler.add_samples_to(result))
608
595
  end
609
-
596
+
610
597
  def test_keep_force_persist
611
598
  sample1 = mock('regular')
612
599
  sample1.stubs(:duration).returns(10)
613
600
  sample1.stubs(:force_persist).returns(false)
614
-
601
+
615
602
  sample2 = mock('force_persist')
616
603
  sample2.stubs(:duration).returns(1)
617
604
  sample2.stubs(:force_persist).returns(true)
618
-
619
- result = @sampler.add_samples_to([sample1,sample2], 2.0)
620
-
605
+
606
+ result = @sampler.add_samples_to([sample1,sample2])
607
+
621
608
  assert_equal 2, result.length
622
609
  assert_equal sample1, result[0]
623
610
  assert_equal sample2, result[1]
624
- end
611
+ end
625
612
 
626
613
  def test_start_builder_default
627
614
  Thread.current[:record_tt] = true
628
- @sampler.expects(:disabled).returns(false)
629
615
  NewRelic::Agent.expects(:is_execution_traced?).returns(true)
630
616
  @sampler.send(:start_builder)
631
- assert(Thread.current[:transaction_sample_builder].is_a?(NewRelic::Agent::TransactionSampleBuilder), "should set up a new builder by default")
617
+ assert(Thread.current[:transaction_sample_builder] \
618
+ .is_a?(NewRelic::Agent::TransactionSampleBuilder),
619
+ "should set up a new builder by default")
632
620
  end
633
621
 
634
622
  def test_start_builder_disabled
635
623
  Thread.current[:transaction_sample_builder] = 'not nil.'
636
- @sampler.expects(:disabled).returns(true)
637
- @sampler.send(:start_builder)
638
- assert_equal(nil, Thread.current[:transaction_sample_builder], "should clear the transaction builder when disabled")
624
+ with_config(:'transaction_tracer.enabled' => false,
625
+ :developer_mode => false) do
626
+ @sampler.send(:start_builder)
627
+ assert_equal(nil, Thread.current[:transaction_sample_builder],
628
+ "should clear the transaction builder when disabled")
629
+ end
639
630
  end
640
631
 
641
632
  def test_start_builder_dont_replace_existing_builder
642
633
  fake_builder = mock('transaction sample builder')
643
634
  Thread.current[:transaction_sample_builder] = fake_builder
644
- @sampler.expects(:disabled).returns(false)
645
635
  @sampler.send(:start_builder)
646
- assert_equal(fake_builder, Thread.current[:transaction_sample_builder], "should not overwrite an existing transaction sample builder")
636
+ assert_equal(fake_builder, Thread.current[:transaction_sample_builder],
637
+ "should not overwrite an existing transaction sample builder")
647
638
  Thread.current[:transaction_sample_builder] = nil
648
639
  end
649
640
 
650
641
  def test_builder
651
642
  Thread.current[:transaction_sample_builder] = 'shamalamadingdong, brother.'
652
- assert_equal('shamalamadingdong, brother.', @sampler.send(:builder), 'should return the value from the thread local variable')
643
+ assert_equal('shamalamadingdong, brother.', @sampler.send(:builder),
644
+ 'should return the value from the thread local variable')
653
645
  Thread.current[:transaction_sample_builder] = nil
654
646
  end
655
647
 
@@ -691,9 +683,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
691
683
 
692
684
  @sampler.notice_pop_scope "a"
693
685
  @sampler.notice_scope_empty
694
- sample = @sampler.harvest([],0.0).first
695
- assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
696
-
686
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
687
+ sample = @sampler.harvest([]).first
688
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
689
+ end
697
690
  end
698
691
 
699
692
  def test_sample__gc_stats
@@ -718,8 +711,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
718
711
  @sampler.notice_pop_scope "a"
719
712
  @sampler.notice_scope_empty
720
713
 
721
- sample = @sampler.harvest([],0.0).first
722
- assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
714
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
715
+ sample = @sampler.harvest([]).first
716
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
717
+ end
723
718
  ensure
724
719
  MockGCStats.mock_values = []
725
720
  end
@@ -741,25 +736,29 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
741
736
  run_sample_trace
742
737
  run_sample_trace
743
738
 
744
- slowest = @sampler.harvest(nil, 0)[0]
745
- first_duration = slowest.duration
746
- assert((first_duration >= 0.1),
747
- "expected sample duration >= 0.1, but was: #{slowest.duration.inspect}")
739
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
740
+ slowest = @sampler.harvest(nil)[0]
741
+ first_duration = slowest.duration
742
+ assert((first_duration >= 0.1),
743
+ "expected sample duration >= 0.1, but was: #{slowest.duration.inspect}")
748
744
 
749
- run_sample_trace { sleep 0.0001 }
750
- not_as_slow = @sampler.harvest(slowest, 0)[0]
751
- assert((not_as_slow == slowest), "Should re-harvest the same transaction since it should be slower than the new transaction - expected #{slowest.inspect} but got #{not_as_slow.inspect}")
745
+ run_sample_trace { sleep 0.0001 }
746
+ not_as_slow = @sampler.harvest(slowest)[0]
747
+ assert((not_as_slow == slowest), "Should re-harvest the same transaction since it should be slower than the new transaction - expected #{slowest.inspect} but got #{not_as_slow.inspect}")
752
748
 
753
- run_sample_trace { sleep(first_duration + 0.1) }
749
+ run_sample_trace { sleep(first_duration + 0.1) }
754
750
 
755
- new_slowest = @sampler.harvest(slowest, 0)[0]
756
- assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
757
- assert((new_slowest.duration >= first_duration + 0.1), "Slowest duration must be >= #{first_duration + 0.1}, but was: #{new_slowest.duration.inspect}")
751
+ new_slowest = @sampler.harvest(slowest)[0]
752
+ assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
753
+ assert((new_slowest.duration >= first_duration + 0.1), "Slowest duration must be >= #{first_duration + 0.1}, but was: #{new_slowest.duration.inspect}")
754
+ end
758
755
  end
759
756
 
760
757
  def test_prepare_to_send
761
758
  run_sample_trace { sleep 0.002 }
762
- sample = @sampler.harvest(nil, 0)[0]
759
+ sample = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
760
+ @sampler.harvest(nil)[0]
761
+ end
763
762
 
764
763
  ready_to_send = sample.prepare_to_send
765
764
  assert sample.duration == ready_to_send.duration
@@ -804,8 +803,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
804
803
  @sampler.notice_scope_empty
805
804
 
806
805
  assert_equal 0, @sampler.scope_depth
807
- sample = @sampler.harvest(nil, 0.0).first
808
- assert_equal "ROOT{a}", sample.to_s_compact
806
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
807
+ sample = @sampler.harvest(nil).first
808
+ assert_equal "ROOT{a}", sample.to_s_compact
809
+ end
809
810
  end
810
811
 
811
812
  def test_double_scope_stack_empty
@@ -818,7 +819,9 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
818
819
  @sampler.notice_scope_empty
819
820
  @sampler.notice_scope_empty
820
821
 
821
- assert_not_nil @sampler.harvest(nil, 0)[0]
822
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
823
+ assert_not_nil @sampler.harvest(nil)[0]
824
+ end
822
825
  end
823
826
 
824
827
 
@@ -844,7 +847,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
844
847
  assert segment[:backtrace]
845
848
  end
846
849
  end
847
-
850
+
848
851
  def test_stack_trace__scope
849
852
  with_config(:'transaction_tracer.stack_trace_threshold' => 0) do
850
853
  t = Time.now
@@ -907,7 +910,9 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
907
910
  @sampler.notice_transaction('/path', nil, {:param => 'hi'})
908
911
  @sampler.notice_scope_empty
909
912
 
910
- tt = @sampler.harvest(nil,0)[0]
913
+ tt = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
914
+ @sampler.harvest(nil)[0]
915
+ end
911
916
 
912
917
  assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
913
918
  end
@@ -916,7 +921,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
916
921
 
917
922
  def test_should_not_collect_segments_beyond_limit
918
923
  with_config(:'transaction_tracer.limit_segments' => 3) do
919
- @sampler.configure!
920
924
  run_sample_trace do
921
925
  @sampler.notice_push_scope 'a1'
922
926
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'hallah'", nil, 0)