newrelic_rpm 3.6.2.96 → 3.6.3.103.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +33 -1
  3. data/README.md +7 -7
  4. data/lib/new_relic/agent/agent.rb +51 -22
  5. data/lib/new_relic/agent/agent_logger.rb +22 -11
  6. data/lib/new_relic/agent/configuration/defaults.rb +6 -1
  7. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -6
  8. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +1 -1
  9. data/lib/new_relic/agent/instrumentation/active_record.rb +8 -48
  10. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +9 -1
  11. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +4 -3
  12. data/lib/new_relic/agent/instrumentation/data_mapper.rb +4 -4
  13. data/lib/new_relic/agent/instrumentation/metric_frame.rb +10 -8
  14. data/lib/new_relic/agent/instrumentation/padrino.rb +32 -0
  15. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +52 -0
  16. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +56 -0
  17. data/lib/new_relic/agent/instrumentation/sinatra.rb +113 -48
  18. data/lib/new_relic/agent/new_relic_service.rb +6 -0
  19. data/lib/new_relic/agent/pipe_channel_manager.rb +13 -8
  20. data/lib/new_relic/agent/request_sampler.rb +205 -0
  21. data/lib/new_relic/agent/sampler.rb +0 -1
  22. data/lib/new_relic/agent/stats_engine/samplers.rb +0 -1
  23. data/lib/new_relic/agent/stats_engine/transactions.rb +12 -16
  24. data/lib/new_relic/agent/transaction.rb +27 -4
  25. data/lib/new_relic/agent/transaction_sample_builder.rb +47 -6
  26. data/lib/new_relic/agent/transaction_sampler.rb +0 -5
  27. data/lib/new_relic/agent.rb +17 -0
  28. data/lib/new_relic/build.rb +2 -2
  29. data/lib/new_relic/coerce.rb +3 -1
  30. data/lib/new_relic/rack/agent_hooks.rb +17 -3
  31. data/lib/new_relic/rack/browser_monitoring.rb +8 -3
  32. data/lib/new_relic/rack/error_collector.rb +2 -0
  33. data/lib/new_relic/transaction_sample/segment.rb +0 -23
  34. data/lib/new_relic/transaction_sample.rb +0 -9
  35. data/lib/new_relic/version.rb +1 -1
  36. data/test/agent_helper.rb +204 -0
  37. data/test/config/newrelic.yml +0 -1
  38. data/test/config/test_control.rb +3 -1
  39. data/test/multiverse/suites/agent_only/key_transactions_test.rb +8 -5
  40. data/test/multiverse/suites/agent_only/logging_test.rb +1 -1
  41. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +7 -8
  42. data/test/multiverse/suites/datamapper/Envfile +7 -0
  43. data/test/multiverse/suites/datamapper/datamapper_test.rb +105 -0
  44. data/test/multiverse/suites/padrino/Envfile +16 -0
  45. data/test/multiverse/suites/padrino/config/newrelic.yml +24 -0
  46. data/test/multiverse/suites/padrino/padrino_test.rb +54 -0
  47. data/test/multiverse/suites/rails/Envfile +5 -5
  48. data/test/multiverse/suites/rails/app.rb +1 -0
  49. data/test/multiverse/suites/rails/request_statistics_test.rb +118 -0
  50. data/test/multiverse/suites/sinatra/Envfile +8 -2
  51. data/test/multiverse/suites/sinatra/ignoring_test.rb +185 -0
  52. data/test/multiverse/suites/sinatra/sinatra_classic_test.rb +92 -0
  53. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +0 -3
  54. data/test/multiverse/suites/sinatra/sinatra_modular_test.rb +89 -0
  55. data/test/multiverse/suites/sinatra/sinatra_test_cases.rb +120 -0
  56. data/test/new_relic/agent/agent_logger_test.rb +149 -56
  57. data/test/new_relic/agent/agent_test.rb +23 -0
  58. data/test/new_relic/agent/agent_test_controller_test.rb +8 -1
  59. data/test/new_relic/agent/autostart_test.rb +10 -6
  60. data/test/new_relic/agent/instrumentation/action_controller_subscriber_test.rb +36 -31
  61. data/test/new_relic/agent/instrumentation/action_view_subscriber_test.rb +7 -0
  62. data/test/new_relic/agent/instrumentation/active_record_helper_test.rb +20 -4
  63. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +20 -9
  64. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +23 -19
  65. data/test/new_relic/agent/instrumentation/sequel_test.rb +118 -79
  66. data/test/new_relic/agent/instrumentation/sinatra/transaction_namer_test.rb +55 -0
  67. data/test/new_relic/agent/instrumentation/sinatra_test.rb +60 -11
  68. data/test/new_relic/agent/method_tracer_test.rb +7 -4
  69. data/test/new_relic/agent/new_relic_service_test.rb +6 -0
  70. data/test/new_relic/agent/pipe_channel_manager_test.rb +6 -2
  71. data/test/new_relic/agent/request_sampler_test.rb +159 -0
  72. data/test/new_relic/agent/stats_engine/samplers_test.rb +1 -5
  73. data/test/new_relic/agent/stats_engine_test.rb +14 -0
  74. data/test/new_relic/agent/transaction_sample_builder_test.rb +43 -6
  75. data/test/new_relic/agent/transaction_sampler_test.rb +31 -1
  76. data/test/new_relic/agent/transaction_test.rb +29 -0
  77. data/test/new_relic/agent_test.rb +7 -0
  78. data/test/new_relic/coerce_test.rb +13 -0
  79. data/test/new_relic/fake_collector.rb +31 -1
  80. data/test/new_relic/metric_spec_test.rb +14 -10
  81. data/test/new_relic/rack/agent_hooks_test.rb +9 -2
  82. data/test/new_relic/rack/browser_monitoring_test.rb +16 -7
  83. data/test/new_relic/rack/developer_mode_test.rb +7 -0
  84. data/test/new_relic/rack/error_collector_test.rb +10 -6
  85. data/test/new_relic/transaction_sample/segment_test.rb +0 -61
  86. data/test/new_relic/transaction_sample_subtest_test.rb +0 -19
  87. data/test/script/ci.sh +14 -0
  88. data/test/test_helper.rb +79 -203
  89. data.tar.gz.sig +0 -0
  90. metadata +50 -18
  91. metadata.gz.sig +0 -0
  92. data/test/multiverse/suites/datamapper/encoding_test.rb +0 -40
  93. data/test/multiverse/suites/sinatra/sinatra_test.rb +0 -143
@@ -54,6 +54,17 @@ module NewRelic
54
54
  assert(@agent.disconnected?)
55
55
  end
56
56
 
57
+ def test_after_fork_should_replace_stats_engine
58
+ with_config(:monitor_mode => true) do
59
+ @agent.stubs(:connected?).returns(true)
60
+ old_engine = @agent.stats_engine
61
+
62
+ @agent.after_fork(:report_to_channel => 123)
63
+
64
+ assert old_engine != @agent.stats_engine, "Still got our old engine around!"
65
+ end
66
+ end
67
+
57
68
  def test_transmit_data_should_transmit
58
69
  @agent.service.expects(:metric_data).at_least_once
59
70
  @agent.instance_eval { transmit_data }
@@ -303,6 +314,18 @@ module NewRelic
303
314
  assert @agent.started?
304
315
  end
305
316
 
317
+ def test_defer_start_if_no_application_name_configured
318
+ logdev = with_array_logger( :error ) do
319
+ with_config( :app_name => false ) do
320
+ @agent.start
321
+ end
322
+ end
323
+ logmsg = logdev.array.first.gsub(/\n/, '')
324
+
325
+ assert !@agent.started?, "agent was started"
326
+ assert_match( /No application name configured/i, logmsg )
327
+ end
328
+
306
329
  end
307
330
  end
308
331
  end
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ if defined?(::Rails)
6
+
4
7
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
5
8
  require 'action_controller/test_case'
6
9
 
@@ -337,4 +340,8 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
337
340
  engine.get_stats_no_scope(name)
338
341
  end
339
342
 
340
- end if defined? Rails
343
+ end
344
+
345
+ else
346
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
347
+ end
@@ -11,12 +11,16 @@ class AutostartTest < Test::Unit::TestCase
11
11
  assert ::NewRelic::Agent::Autostart.agent_should_start?
12
12
  end
13
13
 
14
- def test_agent_wont_autostart_if_RAILS_CONSOLE_constant_is_defined
15
- assert !defined?(::Rails::Console), "precondition: Rails::Console shouldn't be defined"
16
- Rails.const_set(:Console, true)
17
- assert ! ::NewRelic::Agent::Autostart.agent_should_start?, "Agent shouldn't autostart in Rails Console session"
18
- ensure
19
- Rails.send(:remove_const, :Console)
14
+ if defined?(::Rails)
15
+ def test_agent_wont_autostart_if_RAILS_CONSOLE_constant_is_defined
16
+ assert !defined?(::Rails::Console), "precondition: Rails::Console shouldn't be defined"
17
+ Rails.const_set(:Console, true)
18
+ assert ! ::NewRelic::Agent::Autostart.agent_should_start?, "Agent shouldn't autostart in Rails Console session"
19
+ ensure
20
+ Rails.send(:remove_const, :Console)
21
+ end
22
+ else
23
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
20
24
  end
21
25
 
22
26
  def test_agent_wont_start_if_dollar_0_is_irb
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ if defined?(::Rails)
6
+
4
7
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
5
8
  require 'new_relic/agent/instrumentation/action_controller_subscriber'
6
9
 
@@ -45,33 +48,29 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
45
48
  end
46
49
 
47
50
  def test_record_controller_metrics
48
- t0 = Time.now
49
- Time.stubs(:now).returns(t0, t0 + 2)
50
-
51
+ freeze_time
51
52
  @subscriber.start('process_action.action_controller', :id, @entry_payload)
53
+ advance_time(2)
52
54
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
53
55
 
54
- assert_equal 1, @stats_engine.lookup_stats('Controller/test/index').call_count
55
- assert_equal 1, @stats_engine.lookup_stats('HttpDispatcher').call_count
56
- assert_equal 2.0, @stats_engine.lookup_stats('Controller/test/index').total_call_time
57
- assert_equal 2.0, @stats_engine.lookup_stats('HttpDispatcher').total_call_time
56
+ expected_values = { :call_count => 1, :total_call_time => 2.0 }
57
+ assert_metrics_recorded(
58
+ 'Controller/test/index' => expected_values,
59
+ 'HttpDispatcher' => expected_values
60
+ )
58
61
  end
59
62
 
60
63
  def test_record_apdex_metrics
61
- t0 = Time.now
62
- Time.stubs(:now).returns(t0, t0 + 1.5)
63
-
64
+ freeze_time
64
65
  @subscriber.start('process_action.action_controller', :id, @entry_payload)
66
+ advance_time(1.5)
65
67
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
66
68
 
67
- apdex_metric = @stats_engine.lookup_stats('Apdex/test/index')
68
- apdex_rollup_metric = @stats_engine.lookup_stats('Apdex')
69
- assert_equal 0, apdex_metric.apdex_f
70
- assert_equal 0, apdex_rollup_metric.apdex_f
71
- assert_equal 1, apdex_metric.apdex_t
72
- assert_equal 1, apdex_rollup_metric.apdex_t
73
- assert_equal 0, apdex_metric.apdex_s
74
- assert_equal 0, apdex_rollup_metric.apdex_s
69
+ expected_values = { :apdex_f => 0, :apdex_t => 1, :apdex_s => 0 }
70
+ assert_metrics_recorded(
71
+ 'Apdex/test/index' => expected_values,
72
+ 'Apdex' => expected_values
73
+ )
75
74
  end
76
75
 
77
76
  def test_records_scoped_metrics_for_evented_child_txn
@@ -82,8 +81,9 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
82
81
  .merge(:action => 'child', :path => '/child'))
83
82
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
84
83
 
85
- assert_equal(1, @stats_engine.lookup_stats('Controller/test/child',
86
- 'Controller/test/index').call_count)
84
+ assert_metrics_recorded(
85
+ ['Controller/test/child', 'Controller/test/index'] => { :call_count => 1 }
86
+ )
87
87
  end
88
88
 
89
89
  def test_records_scoped_metrics_for_traced_child_txn
@@ -97,8 +97,9 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
97
97
  .merge(:action => 'child', :path => '/child'))
98
98
  end
99
99
 
100
- assert_equal 1, @stats_engine.lookup_stats('Controller/test/child',
101
- 'Controller/test/index').call_count
100
+ assert_metrics_recorded(
101
+ ['Controller/test/child', 'Controller/test/index'] => { :call_count => 1 }
102
+ )
102
103
  end
103
104
 
104
105
  def test_record_nothing_for_ignored_action
@@ -107,10 +108,12 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
107
108
  @subscriber.start('process_action.action_controller', :id, @entry_payload)
108
109
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
109
110
 
110
- assert_nil @stats_engine.lookup_stats('Controller/test/ignored_action')
111
- assert_nil @stats_engine.lookup_stats('Apdex/test/ignored_action')
112
- assert_nil @stats_engine.lookup_stats('Apdex')
113
- assert_nil @stats_engine.lookup_stats('HttpDispatcher')
111
+ assert_metrics_not_recorded([
112
+ 'Controller/test/ignored_action',
113
+ 'Apdex/test/ignored_action',
114
+ 'Apdex',
115
+ 'HttpDispatcher'
116
+ ])
114
117
  end
115
118
 
116
119
  def test_record_no_apdex_metric_for_ignored_apdex_action
@@ -119,10 +122,8 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
119
122
  @subscriber.start('process_action.action_controller', :id, @entry_payload)
120
123
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
121
124
 
122
- assert @stats_engine.lookup_stats('Controller/test/ignored_apdex')
123
- assert_nil @stats_engine.lookup_stats('Apdex/test/ignored_apdex')
124
- assert_nil @stats_engine.lookup_stats('Apdex')
125
- assert @stats_engine.lookup_stats('HttpDispatcher')
125
+ assert_metrics_recorded(['Controller/test/ignored_apdex', 'HttpDispatcher'])
126
+ assert_metrics_not_recorded(['Apdex', 'Apdex/test/ignored_apdex'])
126
127
  end
127
128
 
128
129
  def test_ignore_end_user
@@ -139,7 +140,7 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
139
140
  @subscriber.finish('process_action.action_controller', :id, @exit_payload)
140
141
  NewRelic::Agent::BusyCalculator.harvest_busy
141
142
 
142
- assert_equal 1, @stats_engine.lookup_stats('Instance/Busy').call_count
143
+ assert_metrics_recorded('Instance/Busy' => { :call_count => 1 })
143
144
  end
144
145
 
145
146
  def test_creates_transaction
@@ -226,3 +227,7 @@ class NewRelic::Agent::Instrumentation::ActionControllerSubscriberTest < Test::U
226
227
  .last_sample.params[:custom_params]['number'])
227
228
  end
228
229
  end if ::Rails::VERSION::MAJOR.to_i >= 4
230
+
231
+ else
232
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
233
+ end
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ if defined?(::Rails)
6
+
4
7
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
5
8
  require 'new_relic/agent/instrumentation/action_view_subscriber'
6
9
 
@@ -236,3 +239,7 @@ class NewRelic::Agent::Instrumentation::ActionViewSubscriberTest < Test::Unit::T
236
239
  partial_segments[0].metric_name)
237
240
  end
238
241
  end if ::Rails::VERSION::MAJOR.to_i >= 4
242
+
243
+ else
244
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
245
+ end
@@ -39,10 +39,26 @@ class NewRelic::Agent::Instrumentation::ActiveRecordHelperTest < Test::Unit::Tes
39
39
  assert_nil ActiveRecordHelper.metric_for_name('Model Columns')
40
40
  end
41
41
 
42
- def test_rollup_metric_for_lists_rollups
43
- rollup_metrics = ActiveRecordHelper.rollup_metrics_for('ActiveRecord/Model/find')
44
- assert rollup_metrics.include?('ActiveRecord/find')
45
- assert rollup_metrics.include?('ActiveRecord/all')
42
+ def test_rollup_metrics_for_lists_rollups
43
+ NewRelic::Agent::Transaction.stubs(:recording_web_transaction?).returns(true)
44
+ base_metric = 'ActiveRecord/Namespace::Model/find'
45
+ rollup_metrics = ActiveRecordHelper.rollup_metrics_for(base_metric)
46
+ expected_metrics = ['ActiveRecord/all', 'ActiveRecord/find']
47
+ assert_equal(expected_metrics.sort, rollup_metrics.sort)
48
+ end
49
+
50
+ def test_rollup_metrics_for_skips_operation_rollup_given_metric_without_model
51
+ NewRelic::Agent::Transaction.stubs(:recording_web_transaction?).returns(true)
52
+ rollup_metrics = ActiveRecordHelper.rollup_metrics_for('ActiveRecord/find')
53
+ expected_metrics = ['ActiveRecord/all']
54
+ assert_equal(expected_metrics, rollup_metrics)
55
+ end
56
+
57
+ def test_rollup_metrics_for_omits_database_all_outside_web_transaction
58
+ NewRelic::Agent::Transaction.stubs(:recording_web_transaction?).returns(false)
59
+ base_metric = 'ActiveRecord/Namespace::Model/find'
60
+ rollup_metrics = ActiveRecordHelper.rollup_metrics_for(base_metric)
61
+ assert_equal(['ActiveRecord/find'], rollup_metrics)
46
62
  end
47
63
 
48
64
  def test_remote_service_metric
@@ -2,6 +2,8 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
+ if defined?(::Rails)
6
+
5
7
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
8
 
7
9
  class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::Unit::TestCase
@@ -152,10 +154,16 @@ class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::
152
154
  if NewRelic::Control.instance.rails_version < '2.1.0'
153
155
  expected += ['ActiveRecord/save',
154
156
  'ActiveRecord/ActiveRecordFixtures::Order/save']
155
- elsif NewRelic::Control.instance.rails_version >= '3.0.0'
157
+ end
158
+
159
+ if NewRelic::Control.instance.rails_version >= '3.0.0'
156
160
  expected << 'Database/SQL/insert'
157
161
  end
158
162
 
163
+ if NewRelic::Control.instance.rails_version >= '4.0'
164
+ expected << 'Database/SQL/update'
165
+ end
166
+
159
167
  assert_calls_metrics(*expected) do
160
168
  m = ActiveRecordFixtures::Order.create :id => 1, :name => 'donkey'
161
169
  m = ActiveRecordFixtures::Order.find(m.id)
@@ -499,9 +507,7 @@ class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::
499
507
  true
500
508
  end
501
509
 
502
- expected_metrics = %W[ActiveRecord/all Database/SQL/select RemoteService/sql/#{adapter}/localhost]
503
-
504
- assert_calls_metrics(*expected_metrics) do
510
+ in_web_transaction do
505
511
  begin
506
512
  ActiveRecordFixtures::Order.connection.select_rows "select * from #{ActiveRecordFixtures::Order.table_name}"
507
513
  rescue RuntimeError => e
@@ -509,11 +515,12 @@ class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::
509
515
  raise unless e.message == 'Error'
510
516
  end
511
517
  end
512
- metrics = NewRelic::Agent.instance.stats_engine.metrics
513
- compare_metrics expected_metrics, metrics
514
- check_metric_count('Database/SQL/select', 1)
515
- check_metric_count('ActiveRecord/all', 1)
516
- check_metric_count("RemoteService/sql/#{adapter}/localhost", 1)
518
+
519
+ assert_metrics_recorded(
520
+ 'ActiveRecord/all' => { :call_count => 1 },
521
+ 'Database/SQL/select' => { :call_count => 1 },
522
+ "RemoteService/sql/#{adapter}/localhost" => { :call_count => 1 }
523
+ )
517
524
  end
518
525
 
519
526
  def test_rescue_handling
@@ -589,3 +596,7 @@ class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::
589
596
  last
590
597
  end
591
598
  end
599
+
600
+ else
601
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
602
+ end
@@ -1,6 +1,9 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i >= 4 && !NewRelic::LanguageSupport.using_engine?('jruby')
6
+
4
7
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
5
8
  require 'new_relic/agent/instrumentation/active_record_subscriber'
6
9
 
@@ -46,10 +49,9 @@ class NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest < Test::Unit:
46
49
  end
47
50
 
48
51
  metric_name = 'ActiveRecord/NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest::Order/find'
49
-
50
- scoped_metric = @stats_engine.lookup_stats(metric_name, 'test_txn')
51
- assert_equal(1, scoped_metric.call_count)
52
- assert_equal(2.0, scoped_metric.total_call_time)
52
+ assert_metrics_recorded(
53
+ [metric_name, 'test_txn'] => { :call_count => 1, :total_call_time => 2 }
54
+ )
53
55
  end
54
56
 
55
57
  def test_records_nothing_if_tracing_disabled
@@ -60,24 +62,22 @@ class NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest < Test::Unit:
60
62
  @subscriber.call('sql.active_record', t0, t1, :id, @params)
61
63
  end
62
64
 
63
- metric = @stats_engine \
64
- .lookup_stats('ActiveRecord/NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest::Order/find')
65
- assert_nil metric
65
+ metric_name = 'ActiveRecord/NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest::Order/find'
66
+ assert_metrics_not_recorded([metric_name])
66
67
  end
67
68
 
68
69
  def test_records_rollup_metrics
69
70
  t1 = Time.now
70
71
  t0 = t1 - 2
71
72
 
72
- @subscriber.call('sql.active_record', t0, t1, :id, @params)
73
-
74
- ['ActiveRecord/find', 'ActiveRecord/all'].each do |metric_name|
75
- metric = @stats_engine.lookup_stats(metric_name)
76
- assert_equal(1, metric.call_count,
77
- "Incorrect call count for #{metric_name}")
78
- assert_equal(2.0, metric.total_call_time,
79
- "Incorrect call time for #{metric_name}")
73
+ in_web_transaction do
74
+ @subscriber.call('sql.active_record', t0, t1, :id, @params)
80
75
  end
76
+
77
+ assert_metrics_recorded(
78
+ 'ActiveRecord/find' => { :call_count => 1, :total_call_time => 2 },
79
+ 'ActiveRecord/all' => { :call_count => 1, :total_call_time => 2 }
80
+ )
81
81
  end
82
82
 
83
83
  def test_records_remote_service_metric
@@ -86,9 +86,9 @@ class NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest < Test::Unit:
86
86
 
87
87
  @subscriber.call('sql.active_record', t0, t1, :id, @params)
88
88
 
89
- metric = @stats_engine.lookup_stats('RemoteService/sql/mysql/server')
90
- assert_equal(1, metric.call_count)
91
- assert_equal(2.0, metric.total_call_time)
89
+ assert_metrics_recorded(
90
+ 'RemoteService/sql/mysql/server' => { :call_count => 1, :total_call_time => 2.0 }
91
+ )
92
92
  end
93
93
 
94
94
  def test_creates_txn_segment
@@ -128,4 +128,8 @@ class NewRelic::Agent::Instrumentation::ActiveRecordSubscriberTest < Test::Unit:
128
128
  ensure
129
129
  NewRelic::Agent.shutdown
130
130
  end
131
- end if ::Rails::VERSION::MAJOR.to_i >= 4 && !NewRelic::LanguageSupport.using_engine?('jruby')
131
+ end
132
+
133
+ else
134
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
135
+ end
@@ -2,7 +2,7 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
- if RUBY_VERSION >= "1.8.7"
5
+ if defined?(::Rails) && RUBY_VERSION >= "1.8.7"
6
6
 
7
7
  require 'sequel'
8
8
 
@@ -55,22 +55,13 @@ class NewRelic::Agent::Instrumentation::SequelInstrumentationTest < Test::Unit::
55
55
  super
56
56
 
57
57
  NewRelic::Agent.manual_start
58
-
59
- @agent = NewRelic::Agent.instance
60
- @agent.transaction_sampler.reset!
61
-
62
- @engine = @agent.stats_engine
63
- @engine.clear_stats
64
- @engine.start_transaction
65
-
66
- @sampler = NewRelic::Agent.instance.transaction_sampler
58
+ NewRelic::Agent.instance.transaction_sampler.reset!
59
+ NewRelic::Agent.instance.stats_engine.clear_stats
67
60
  end
68
61
 
69
62
  def teardown
70
63
  super
71
- @engine.end_transaction
72
- NewRelic::Agent::TransactionInfo.reset
73
- Thread::current[:newrelic_scope_name] = nil
64
+
74
65
  NewRelic::Agent.shutdown
75
66
  end
76
67
 
@@ -88,121 +79,165 @@ class NewRelic::Agent::Instrumentation::SequelInstrumentationTest < Test::Unit::
88
79
  end
89
80
 
90
81
  def test_model_enumerator_generates_metrics
91
- Post.all
82
+ in_web_transaction { Post.all }
92
83
 
93
84
  assert_remote_service_metrics
94
- assert_includes @engine.metrics, "Database/SQL/select"
95
- assert_includes @engine.metrics, "ActiveRecord/all"
96
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/all"
85
+ assert_metrics_recorded([
86
+ "Database/SQL/select",
87
+ "ActiveRecord/all",
88
+ "ActiveRecord/#{Post.name}/all"
89
+ ])
97
90
  end
98
91
 
99
92
  def test_model_index_operator_generates_metrics
100
- Post[11]
93
+ in_web_transaction { Post[11] }
101
94
 
102
95
  assert_remote_service_metrics
103
- assert_includes @engine.metrics, "Database/SQL/select"
104
- assert_includes @engine.metrics, "ActiveRecord/all"
105
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/get"
96
+ assert_metrics_recorded([
97
+ "Database/SQL/select",
98
+ "ActiveRecord/all",
99
+ "ActiveRecord/#{Post.name}/get"
100
+ ])
106
101
  end
107
102
 
108
103
  def test_model_create_method_generates_metrics
109
- post = Post.create( :title => 'The Thing', :content => 'A wicked short story.' )
104
+ in_web_transaction do
105
+ Post.create( :title => 'The Thing', :content => 'A wicked short story.' )
106
+ end
110
107
 
111
108
  assert_remote_service_metrics
112
- assert_includes @engine.metrics, "Database/SQL/insert"
113
- assert_includes @engine.metrics, "ActiveRecord/all"
114
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/create"
109
+ assert_metrics_recorded([
110
+ 'Database/SQL/insert',
111
+ 'ActiveRecord/all',
112
+ "ActiveRecord/#{Post.name}/create"
113
+ ])
115
114
  end
116
115
 
117
116
  def test_model_update_method_generates_metrics
118
- post = Post.create( :title => 'All The Things', :content => 'A story about beans.' )
119
- post.update( :title => 'A Lot of the Things' )
117
+ in_web_transaction do
118
+ post = Post.create( :title => 'All The Things', :content => 'A story about beans.' )
119
+ post.update( :title => 'A Lot of the Things' )
120
+ end
120
121
 
121
122
  assert_remote_service_metrics
122
- assert_includes @engine.metrics, "Database/SQL/update"
123
- assert_includes @engine.metrics, "ActiveRecord/all"
124
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/update"
123
+ assert_metrics_recorded([
124
+ 'Database/SQL/update',
125
+ 'ActiveRecord/all',
126
+ "ActiveRecord/#{Post.name}/update"
127
+ ])
125
128
  end
126
129
 
127
130
  def test_model_update_all_method_generates_metrics
128
- post = Post.create( :title => 'All The Things', :content => 'A nicer story than yours.' )
129
- post.update_all( :title => 'A Whole Hell of a Lot of the Things' )
131
+ in_web_transaction do
132
+ post = Post.create( :title => 'All The Things', :content => 'A nicer story than yours.' )
133
+ post.update_all( :title => 'A Whole Hell of a Lot of the Things' )
134
+ end
130
135
 
131
136
  assert_remote_service_metrics
132
- assert_includes @engine.metrics, "Database/SQL/update"
133
- assert_includes @engine.metrics, "ActiveRecord/all"
134
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/update_all"
137
+ assert_metrics_recorded([
138
+ "Database/SQL/update",
139
+ "ActiveRecord/all",
140
+ "ActiveRecord/#{Post.name}/update_all"
141
+ ])
135
142
  end
136
143
 
137
144
  def test_model_update_except_method_generates_metrics
138
- post = Post.create( :title => 'All The Things', :content => 'A story.' )
139
- post.update_except( {:title => 'A Bit More of the Things'}, :created_at )
145
+ in_web_transaction do
146
+ post = Post.create( :title => 'All The Things', :content => 'A story.' )
147
+ post.update_except( {:title => 'A Bit More of the Things'}, :created_at )
148
+ end
140
149
 
141
150
  assert_remote_service_metrics
142
- assert_includes @engine.metrics, "Database/SQL/update"
143
- assert_includes @engine.metrics, "ActiveRecord/all"
144
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/update_except"
151
+ assert_metrics_recorded([
152
+ "Database/SQL/update",
153
+ "ActiveRecord/all",
154
+ "ActiveRecord/#{Post.name}/update_except"
155
+ ])
145
156
  end
146
157
 
147
158
  def test_model_update_fields_method_generates_metrics
148
- post = Post.create( :title => 'All The Things', :content => 'A venal short story.' )
149
- post.update_fields( {:title => 'A Plethora of Things'}, [:title] )
159
+ in_web_transaction do
160
+ post = Post.create( :title => 'All The Things', :content => 'A venal short story.' )
161
+ post.update_fields( {:title => 'A Plethora of Things'}, [:title] )
162
+ end
150
163
 
151
164
  assert_remote_service_metrics
152
- assert_includes @engine.metrics, "Database/SQL/update"
153
- assert_includes @engine.metrics, "ActiveRecord/all"
154
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/update_fields"
165
+ assert_metrics_recorded([
166
+ "Database/SQL/update",
167
+ "ActiveRecord/all",
168
+ "ActiveRecord/#{Post.name}/update_fields"
169
+ ])
155
170
  end
156
171
 
157
172
  def test_model_update_only_method_generates_metrics
158
- post = Post.create( :title => 'All The Things', :content => 'A meandering short story.' )
159
- post.update_only( {:title => 'A Lot of the Things'}, :title )
173
+ in_web_transaction do
174
+ post = Post.create( :title => 'All The Things', :content => 'A meandering short story.' )
175
+ post.update_only( {:title => 'A Lot of the Things'}, :title )
176
+ end
160
177
 
161
178
  assert_remote_service_metrics
162
- assert_includes @engine.metrics, "Database/SQL/update"
163
- assert_includes @engine.metrics, "ActiveRecord/all"
164
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/update_only"
179
+ assert_metrics_recorded([
180
+ "Database/SQL/update",
181
+ "ActiveRecord/all",
182
+ "ActiveRecord/#{Post.name}/update_only"
183
+ ])
165
184
  end
166
185
 
167
186
  def test_model_save_method_generates_metrics
168
- post = Post.new( :title => 'An Endless Lot Full of Things',
169
- :content => 'A lingering long story.' )
170
- post.save
187
+ in_web_transaction do
188
+ post = Post.new( :title => 'An Endless Lot Full of Things',
189
+ :content => 'A lingering long story.' )
190
+ post.save
191
+ end
171
192
 
172
193
  assert_remote_service_metrics
173
- assert_includes @engine.metrics, "Database/SQL/insert"
174
- assert_includes @engine.metrics, "ActiveRecord/all"
175
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/save"
194
+ assert_metrics_recorded([
195
+ "Database/SQL/insert",
196
+ "ActiveRecord/all",
197
+ "ActiveRecord/#{Post.name}/save"
198
+ ])
176
199
  end
177
200
 
178
201
  def test_model_delete_method_generates_metrics
179
- post = Post.create( :title => 'All The Things', :content => 'A nice short story.' )
180
- post.delete
202
+ in_web_transaction do
203
+ post = Post.create( :title => 'All The Things', :content => 'A nice short story.' )
204
+ post.delete
205
+ end
181
206
 
182
207
  assert_remote_service_metrics
183
- assert_includes @engine.metrics, "Database/SQL/delete"
184
- assert_includes @engine.metrics, "ActiveRecord/all"
185
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/delete"
208
+ assert_metrics_recorded([
209
+ "Database/SQL/delete",
210
+ "ActiveRecord/all",
211
+ "ActiveRecord/#{Post.name}/delete"
212
+ ])
186
213
  end
187
214
 
188
215
  def test_model_destroy_method_generates_metrics
189
- post = Post.create( :title => 'Most of the Things', :content => 'Another short story.' )
190
- post.destroy
216
+ in_web_transaction do
217
+ post = Post.create( :title => 'Most of the Things', :content => 'Another short story.' )
218
+ post.destroy
219
+ end
191
220
 
192
221
  assert_remote_service_metrics
193
- assert_includes @engine.metrics, "Database/SQL/delete"
194
- assert_includes @engine.metrics, "ActiveRecord/all"
195
- assert_includes @engine.metrics, "ActiveRecord/#{Post.name}/destroy"
222
+ assert_metrics_recorded([
223
+ "Database/SQL/delete",
224
+ "ActiveRecord/all",
225
+ "ActiveRecord/#{Post.name}/destroy"
226
+ ])
196
227
  end
197
228
 
198
229
  def test_model_destroy_uses_the_class_name_for_the_metric
199
- author = Author.create( :name => 'Marlon Forswytthe', :login => 'mfors' )
200
- author.destroy
230
+ in_web_transaction do
231
+ author = Author.create( :name => 'Marlon Forswytthe', :login => 'mfors' )
232
+ author.destroy
233
+ end
201
234
 
202
235
  assert_remote_service_metrics
203
- assert_includes @engine.metrics, "Database/SQL/delete"
204
- assert_includes @engine.metrics, "ActiveRecord/all"
205
- assert_includes @engine.metrics, "ActiveRecord/#{Author.name}/destroy"
236
+ assert_metrics_recorded([
237
+ "Database/SQL/delete",
238
+ "ActiveRecord/all",
239
+ "ActiveRecord/#{Author.name}/destroy"
240
+ ])
206
241
  end
207
242
 
208
243
  def test_slow_queries_get_an_explain_plan
@@ -242,23 +277,25 @@ class NewRelic::Agent::Instrumentation::SequelInstrumentationTest < Test::Unit::
242
277
  end
243
278
 
244
279
  def assert_remote_service_metrics
280
+ engine = NewRelic::Agent.instance.stats_engine
245
281
  if (jruby?)
246
- assert @engine.metrics.none? {|s| s.start_with?("RemoteService/")}, "Sqlite on JRuby doesn't report adapter right for this metric. Why's it here?"
282
+ assert engine.metrics.none? {|s| s.start_with?("RemoteService/")}, "Sqlite on JRuby doesn't report adapter right for this metric. Why's it here?"
247
283
  else
248
- assert_includes @engine.metrics, "RemoteService/sql/sqlite/localhost"
284
+ assert_includes engine.metrics, "RemoteService/sql/sqlite/localhost"
249
285
  end
250
286
  end
251
287
 
252
288
  def with_controller_scope
253
- @sampler.notice_first_scope_push Time.now.to_f
254
- @sampler.notice_transaction('/', {})
255
- @sampler.notice_push_scope "Controller/sandwiches/index"
289
+ sampler = NewRelic::Agent.instance.transaction_sampler
290
+ sampler.notice_first_scope_push Time.now.to_f
291
+ sampler.notice_transaction('/', {})
292
+ sampler.notice_push_scope "Controller/sandwiches/index"
256
293
 
257
294
  yield if block_given?
258
295
 
259
- @sampler.notice_pop_scope "Controller/sandwiches/index"
260
- @sampler.notice_scope_empty(stub('txn', :name => '/', :custom_parameters => {}))
261
- @sampler.samples
296
+ sampler.notice_pop_scope "Controller/sandwiches/index"
297
+ sampler.notice_scope_empty(stub('txn', :name => '/', :custom_parameters => {}))
298
+ sampler.samples
262
299
  end
263
300
 
264
301
  def last_segment_for(options={})
@@ -282,4 +319,6 @@ class NewRelic::Agent::Instrumentation::SequelInstrumentationTest < Test::Unit::
282
319
 
283
320
  end
284
321
 
322
+ else
323
+ puts "Skipping tests in #{__FILE__} because Rails is unavailable"
285
324
  end