wd_newrelic_rpm 3.5.6 → 3.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/CHANGELOG +43 -3
  2. data/Gemfile +6 -2
  3. data/LICENSE +23 -0
  4. data/lib/new_relic/agent.rb +50 -3
  5. data/lib/new_relic/agent/agent.rb +40 -60
  6. data/lib/new_relic/agent/configuration/defaults.rb +9 -3
  7. data/lib/new_relic/agent/configuration/server_source.rb +4 -0
  8. data/lib/new_relic/agent/cross_app_monitor.rb +239 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +281 -0
  10. data/lib/new_relic/agent/database.rb +28 -10
  11. data/lib/new_relic/agent/error_collector.rb +5 -0
  12. data/lib/new_relic/agent/event_listener.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +58 -39
  14. data/lib/new_relic/agent/instrumentation/metric_frame.rb +16 -3
  15. data/lib/new_relic/agent/instrumentation/net.rb +13 -11
  16. data/lib/new_relic/agent/instrumentation/queue_time.rb +50 -192
  17. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +145 -0
  18. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +45 -0
  19. data/lib/new_relic/agent/instrumentation/resque.rb +10 -10
  20. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  21. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  22. data/lib/new_relic/agent/pipe_service.rb +8 -12
  23. data/lib/new_relic/agent/rules_engine.rb +72 -0
  24. data/lib/new_relic/agent/shim_agent.rb +0 -1
  25. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  26. data/lib/new_relic/agent/stats.rb +149 -0
  27. data/lib/new_relic/agent/stats_engine.rb +9 -0
  28. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  29. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  30. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  31. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  32. data/lib/new_relic/agent/transaction_info.rb +31 -6
  33. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  34. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  35. data/lib/new_relic/helper.rb +32 -0
  36. data/lib/new_relic/local_environment.rb +24 -32
  37. data/lib/new_relic/okjson.rb +599 -0
  38. data/lib/new_relic/transaction_sample.rb +2 -1
  39. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  40. data/lib/new_relic/version.rb +1 -1
  41. data/newrelic.yml +27 -41
  42. data/test/multiverse/suites/agent_only/Envfile +5 -1
  43. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  44. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  45. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  46. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  47. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  48. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  49. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  50. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  51. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  52. data/test/multiverse/suites/rails/queue_time_test.rb +2 -2
  53. data/test/multiverse/suites/resque/instrumentation_test.rb +4 -3
  54. data/test/multiverse/suites/sinatra/Envfile +2 -0
  55. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  56. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  57. data/test/multiverse/suites/sinatra/sinatra_test.rb +77 -10
  58. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  59. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  60. data/test/new_relic/agent/agent_test.rb +20 -40
  61. data/test/new_relic/agent/agent_test_controller_test.rb +27 -60
  62. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  63. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  64. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  65. data/test/new_relic/agent/database_test.rb +60 -16
  66. data/test/new_relic/agent/error_collector_test.rb +28 -4
  67. data/test/new_relic/agent/event_listener_test.rb +23 -2
  68. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +1 -1
  69. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +85 -12
  70. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  71. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +436 -59
  72. data/test/new_relic/agent/instrumentation/queue_time_test.rb +58 -357
  73. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  74. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  75. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  76. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  77. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  78. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  79. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  80. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  81. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  82. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  83. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  84. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  85. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  86. data/test/new_relic/agent/stats_test.rb +197 -0
  87. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  88. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  89. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  90. data/test/new_relic/agent/worker_loop_test.rb +1 -1
  91. data/test/new_relic/agent_test.rb +35 -5
  92. data/test/new_relic/control_test.rb +1 -1
  93. data/test/new_relic/fake_collector.rb +87 -9
  94. data/test/new_relic/helper_test.rb +24 -0
  95. data/test/new_relic/metric_data_test.rb +11 -11
  96. data/test/new_relic/metric_spec_test.rb +1 -1
  97. data/test/script/ci.sh +1 -1
  98. data/test/test_contexts.rb +0 -1
  99. data/test/test_helper.rb +21 -3
  100. metadata +34 -41
  101. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  102. data/lib/new_relic/stats.rb +0 -337
  103. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  104. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  105. data/test/new_relic/fakes_sending_data.rb +0 -30
  106. data/test/new_relic/stats_test.rb +0 -421
@@ -16,7 +16,7 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
16
16
  expected_but_missing = stat_names - @agent.stats_engine.metrics
17
17
  assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
18
18
  stat = @agent.stats_engine.get_stats_no_scope(n)
19
- "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
19
+ "#{'%-26s' % n}: #{stat.call_count} calls"
20
20
  }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
21
21
  assert_equal 0, @agent.stats_engine.get_stats_no_scope('Controller').call_count
22
22
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/NewRelic::Agent::Instrumentation::TaskInstrumentationTest/inner_task_0').call_count
@@ -44,7 +44,6 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
44
44
  run_task_outer(3)
45
45
  @agent.stats_engine.metrics.sort.each do |n|
46
46
  stat = @agent.stats_engine.get_stats_no_scope(n)
47
- # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
48
47
  end
49
48
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/NewRelic::Agent::Instrumentation::TaskInstrumentationTest/outer_task').call_count
50
49
  assert_equal 2, @agent.stats_engine.get_stats_no_scope('Controller/NewRelic::Agent::Instrumentation::TaskInstrumentationTest/inner_task_0').call_count
@@ -70,7 +69,6 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
70
69
  assert_equal 0, @agent.transaction_sampler.scope_depth, "existing unfinished sample"
71
70
  @agent.stats_engine.metrics.sort.each do |n|
72
71
  stat = @agent.stats_engine.get_stats_no_scope(n)
73
- # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
74
72
  end
75
73
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/NewRelic::Agent::Instrumentation::TaskInstrumentationTest/outer_task').call_count
76
74
  assert_equal 0, @agent.stats_engine.get_stats_no_scope('Controller').call_count
@@ -101,7 +99,6 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
101
99
  end
102
100
  @agent.stats_engine.metrics.sort.each do |n|
103
101
  stat = @agent.stats_engine.get_stats_no_scope(n)
104
- #puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
105
102
  end
106
103
  assert_equal @agent, NewRelic::Agent.instance
107
104
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller/NewRelic::Agent::Instrumentation::TaskInstrumentationTest/hello').call_count
@@ -146,7 +143,7 @@ class NewRelic::Agent::Instrumentation::TaskInstrumentationTest < Test::Unit::Te
146
143
  expected_but_missing = stat_names - @agent.stats_engine.metrics
147
144
  assert_equal 0, expected_but_missing.size, @agent.stats_engine.metrics.map { |n|
148
145
  stat = @agent.stats_engine.get_stats_no_scope(n)
149
- "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
146
+ "#{'%-26s' % n}: #{stat.call_count} calls"
150
147
  }.join("\n ") + "\nmissing: #{expected_but_missing.inspect}"
151
148
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/all').call_count
152
149
  assert_equal 1, @agent.stats_engine.get_stats_no_scope('OtherTransaction/Background/all').call_count
@@ -59,6 +59,8 @@ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
59
59
  attr_reader :stats_engine
60
60
 
61
61
  def setup
62
+ Thread::current[:newrelic_scope_stack] = nil
63
+
62
64
  NewRelic::Agent.manual_start
63
65
  @stats_engine = NewRelic::Agent.instance.stats_engine
64
66
  @stats_engine.clear_stats
@@ -288,7 +290,7 @@ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
288
290
  self.class.trace_execution_scoped metrics do
289
291
  sleep 0.05
290
292
  end
291
- elapsed = @stats_engine.get_stats('first').average_call_time
293
+ elapsed = @stats_engine.get_stats('first').total_call_time
292
294
  metrics.map{|name| @stats_engine.get_stats name}.each do | m |
293
295
  assert_equal 1, m.call_count
294
296
  assert_equal elapsed, m.total_call_time
@@ -299,7 +301,7 @@ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
299
301
  self.class.trace_execution_unscoped metrics do
300
302
  sleep 0.05
301
303
  end
302
- elapsed = @stats_engine.get_stats('first').average_call_time
304
+ elapsed = @stats_engine.get_stats('first').total_call_time
303
305
  metrics.map{|name| @stats_engine.get_stats name}.each do | m |
304
306
  assert_equal 1, m.call_count
305
307
  assert_equal elapsed, m.total_call_time
@@ -197,9 +197,44 @@ class NewRelicServiceTest < Test::Unit::TestCase
197
197
  end
198
198
 
199
199
  def test_metric_data
200
- @http_handle.respond_to(:metric_data, 'met rick date uhhh')
201
- response = @service.metric_data((Time.now - 60).to_f, Time.now.to_f, {})
202
- assert_equal 'met rick date uhhh', response
200
+ dummy_rsp = 'met rick date uhh'
201
+ @http_handle.respond_to(:metric_data, dummy_rsp)
202
+ stats_hash = NewRelic::Agent::StatsHash.new
203
+ @service.expects(:fill_metric_id_cache).with(dummy_rsp)
204
+ response = @service.metric_data((Time.now - 60).to_f, Time.now.to_f, stats_hash)
205
+ assert_equal dummy_rsp, response
206
+ end
207
+
208
+ def test_fill_metric_id_cache_from_collect_response
209
+ response = [[{"scope"=>"Controller/blogs/index", "name"=>"Database/SQL/other"}, 1328],
210
+ [{"scope"=>"", "name"=>"WebFrontend/QueueTime"}, 10],
211
+ [{"scope"=>"", "name"=>"ActiveRecord/Blog/find"}, 1017]]
212
+
213
+ @service.send(:fill_metric_id_cache, response)
214
+
215
+ cache = @service.metric_id_cache
216
+ assert_equal 1328, cache[NewRelic::MetricSpec.new('Database/SQL/other', 'Controller/blogs/index')]
217
+ assert_equal 10, cache[NewRelic::MetricSpec.new('WebFrontend/QueueTime')]
218
+ assert_equal 1017, cache[NewRelic::MetricSpec.new('ActiveRecord/Blog/find')]
219
+ end
220
+
221
+ def test_caches_metric_ids_for_future_use
222
+ dummy_rsp = [[{ 'name' => 'a', 'scope' => '' }, 42]]
223
+ @http_handle.respond_to(:metric_data, dummy_rsp)
224
+
225
+ hash = NewRelic::Agent::StatsHash.new
226
+ hash.record(NewRelic::MetricSpec.new('a'), 1)
227
+
228
+ @service.metric_data((Time.now - 60).to_f, Time.now.to_f, hash)
229
+
230
+ hash = NewRelic::Agent::StatsHash.new
231
+ hash.record(NewRelic::MetricSpec.new('a'), 1)
232
+ stats = hash[NewRelic::MetricSpec.new('a')]
233
+
234
+ results = @service.build_metric_data_array(hash)
235
+ assert_nil(results.first.metric_spec)
236
+ assert_equal(stats, results.first.stats)
237
+ assert_equal(42, results.first.metric_id)
203
238
  end
204
239
 
205
240
  def test_error_data
@@ -276,7 +311,9 @@ end
276
311
 
277
312
  @service.connect
278
313
  @http_handle.respond_to(:metric_data, 0)
279
- @service.metric_data((Time.now - 60).to_f, Time.now.to_f, {})
314
+ @service.stubs(:fill_metric_id_cache)
315
+ stats_hash = NewRelic::Agent::StatsHash.new
316
+ @service.metric_data((Time.now - 60).to_f, Time.now.to_f, stats_hash)
280
317
 
281
318
  @http_handle.respond_to(:transaction_sample_data, 1)
282
319
  @service.transaction_sample_data([])
@@ -298,7 +335,8 @@ end
298
335
  def test_should_raise_exception_on_413
299
336
  @http_handle.respond_to(:metric_data, 'too big', :code => 413)
300
337
  assert_raise NewRelic::Agent::UnrecoverableServerException do
301
- @service.metric_data((Time.now - 60).to_f, Time.now.to_f, {})
338
+ stats_hash = NewRelic::Agent::StatsHash.new
339
+ @service.metric_data((Time.now - 60).to_f, Time.now.to_f, stats_hash)
302
340
  end
303
341
  end
304
342
 
@@ -306,7 +344,8 @@ end
306
344
  def test_should_raise_exception_on_415
307
345
  @http_handle.respond_to(:metric_data, 'too big', :code => 415)
308
346
  assert_raise NewRelic::Agent::UnrecoverableServerException do
309
- @service.metric_data((Time.now - 60).to_f, Time.now.to_f, {})
347
+ stats_hash = NewRelic::Agent::StatsHash.new
348
+ @service.metric_data((Time.now - 60).to_f, Time.now.to_f, stats_hash)
310
349
  end
311
350
  end
312
351
 
@@ -323,6 +362,18 @@ end
323
362
  marshaller.load('{"exception": {"message": "error message", "error_type": "JavaCrash"}}')
324
363
  end
325
364
  end
365
+
366
+ def test_use_pruby_marshaller_if_error_using_json
367
+ json_marshaller = NewRelic::Agent::NewRelicService::JsonMarshaller.new
368
+ @service.instance_variable_set(:@marshaller, json_marshaller)
369
+ JSON.stubs(:dump).raises(RuntimeError.new('blah'))
370
+ @http_handle.respond_to(:transaction_sample_data, 'ok', :format => :pruby)
371
+
372
+ @service.transaction_sample_data([])
373
+
374
+ assert_equal('NewRelic::Agent::NewRelicService::PrubyMarshaller',
375
+ @service.marshaller.class.name)
376
+ end
326
377
  end
327
378
 
328
379
  def test_pruby_marshaller_handles_errors_from_collector
@@ -392,6 +443,57 @@ end
392
443
  assert_equal 'OogBooga: test', error.message
393
444
  end
394
445
 
446
+ def test_build_metric_data_array
447
+ hash = NewRelic::Agent::StatsHash.new
448
+
449
+ spec1 = NewRelic::MetricSpec.new('foo')
450
+ spec2 = NewRelic::MetricSpec.new('bar')
451
+ hash.record(spec1, 1)
452
+ hash.record(spec2, 2)
453
+
454
+ metric_data_array = @service.build_metric_data_array(hash)
455
+
456
+ assert_equal(2, metric_data_array.size)
457
+ metric_data_1 = metric_data_array.find { |md| md.metric_spec == spec1 }
458
+ metric_data_2 = metric_data_array.find { |md| md.metric_spec == spec2 }
459
+ assert_equal(hash[spec1], metric_data_1.stats)
460
+ assert_equal(hash[spec2], metric_data_2.stats)
461
+ end
462
+
463
+ def test_build_metric_data_array_uses_metric_id_cache_if_possible
464
+ hash = NewRelic::Agent::StatsHash.new
465
+
466
+ spec1 = NewRelic::MetricSpec.new('foo')
467
+ spec2 = NewRelic::MetricSpec.new('bar')
468
+ hash.record(spec1, 1)
469
+ hash.record(spec2, 1)
470
+
471
+ @service.stubs(:metric_id_cache).returns({ spec1 => 42 })
472
+ metric_data_array = @service.build_metric_data_array(hash)
473
+
474
+ assert_equal(2, metric_data_array.size)
475
+
476
+ metric_data1 = metric_data_array.find { |md| md.metric_id == 42 }
477
+ metric_data2 = metric_data_array.find { |md| md.metric_spec == spec2 }
478
+ assert_nil(metric_data1.metric_spec)
479
+ assert_nil(metric_data2.metric_id)
480
+ end
481
+
482
+ def test_build_metric_data_array_omits_empty_stats
483
+ hash = NewRelic::Agent::StatsHash.new
484
+
485
+ spec1 = NewRelic::MetricSpec.new('foo')
486
+ spec2 = NewRelic::MetricSpec.new('bar')
487
+ hash.record(spec1, 1)
488
+ hash[spec2] = NewRelic::Agent::Stats.new()
489
+
490
+ metric_data_array = @service.build_metric_data_array(hash)
491
+ assert_equal(1, metric_data_array.size)
492
+
493
+ metric_data = metric_data_array.first
494
+ assert_equal(spec1, metric_data.metric_spec)
495
+ end
496
+
395
497
  class HTTPHandle
396
498
  attr_accessor :read_timeout, :route_table
397
499
 
@@ -39,7 +39,7 @@ class NewRelic::Agent::PipeChannelManagerTest < Test::Unit::TestCase
39
39
  NewRelic::Agent.after_fork
40
40
  new_engine = NewRelic::Agent::StatsEngine.new
41
41
  new_engine.get_stats_no_scope(metric).record_data_point(2.0)
42
- listener.pipes[666].write(:stats => new_engine.harvest_timeslice_data({}, {}))
42
+ listener.pipes[666].write(:stats => new_engine.harvest_timeslice_data({}))
43
43
  end
44
44
  Process.wait(pid)
45
45
  listener.stop
@@ -2,15 +2,15 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_hel
2
2
 
3
3
  class PipeServiceTest < Test::Unit::TestCase
4
4
  def setup
5
- NewRelic::Agent::PipeChannelManager.listener.stop
5
+ NewRelic::Agent::PipeChannelManager.listener.stop
6
6
  NewRelic::Agent::PipeChannelManager.register_report_channel(:pipe_service_test)
7
7
  @service = NewRelic::Agent::PipeService.new(:pipe_service_test)
8
8
  end
9
-
9
+
10
10
  def test_constructor
11
11
  assert_equal :pipe_service_test, @service.channel_id
12
12
  end
13
-
13
+
14
14
  def test_connect_returns_nil
15
15
  assert_nil @service.connect({})
16
16
  end
@@ -75,13 +75,13 @@ class PipeServiceTest < Test::Unit::TestCase
75
75
  @service.metric_data(0.0, 0.1, metric_data0)
76
76
  @service.transaction_sample_data(['txn0'])
77
77
  @service.error_data(['err0'])
78
- @service.sql_trace_data(['sql0'])
78
+ @service.sql_trace_data(['sql0'])
79
79
  @service.shutdown(Time.now)
80
80
  end
81
81
  Process.wait(pid)
82
-
82
+
83
83
  received_data = read_from_pipe
84
-
84
+
85
85
  assert_equal 'Custom/something', received_data[:stats].keys.sort[0].name
86
86
  assert_equal ['txn0'], received_data[:transaction_traces]
87
87
  assert_equal ['err0'], received_data[:error_traces].sort
@@ -93,7 +93,7 @@ class PipeServiceTest < Test::Unit::TestCase
93
93
  end
94
94
  assert_equal 'EOF', received_data[:EOF]
95
95
  end
96
-
96
+
97
97
  def test_shutdown_closes_pipe
98
98
  data_from_forked_process do
99
99
  @service.shutdown(Time.now)
@@ -102,11 +102,11 @@ class PipeServiceTest < Test::Unit::TestCase
102
102
  end
103
103
  end
104
104
  end
105
-
105
+
106
106
  def generate_metric_data(metric_name, data=1.0)
107
107
  engine = NewRelic::Agent::StatsEngine.new
108
108
  engine.get_stats_no_scope(metric_name).record_data_point(data)
109
- engine.harvest_timeslice_data({}, {}).values
109
+ engine.harvest_timeslice_data({})
110
110
  end
111
111
 
112
112
  def read_from_pipe
@@ -83,17 +83,6 @@ class NewRelic::Agent::RpmAgentTest < Test::Unit::TestCase # ActiveSupport::Test
83
83
  NewRelic::Agent.shutdown
84
84
  end
85
85
 
86
- should "send_timeslice_data" do
87
- # this test fails due to a rubinius bug
88
- return if NewRelic::LanguageSupport.using_engine?('rbx')
89
- @agent.service.expects(:metric_data).returns([ [{'name' => '/A/b/c'}, 1],
90
- [{'name' => '/A/b/c', 'scope' => '/X'}, 2],
91
- [{'name' => '/A/b/d'}, 3] ])
92
- @agent.send :harvest_and_send_timeslice_data
93
- assert_equal 3, @agent.metric_ids.size
94
- assert_equal(3, @agent.metric_ids[NewRelic::MetricSpec.new('/A/b/d')],
95
- @agent.metric_ids.inspect)
96
- end
97
86
  should "set_record_sql" do
98
87
  @agent.set_record_sql(false)
99
88
  assert !NewRelic::Agent.is_sql_recorded?
@@ -0,0 +1,82 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class RulesEngineTest < Test::Unit::TestCase
4
+ def setup
5
+ @engine = NewRelic::Agent::RulesEngine.new
6
+ end
7
+
8
+ def test_rule_defaults
9
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '.*',
10
+ 'replacement' => '*')
11
+ assert !rule.terminate_chain
12
+ assert !rule.each_segment
13
+ assert !rule.ignore
14
+ assert !rule.replace_all
15
+ assert_equal 0, rule.eval_order
16
+ end
17
+
18
+ def test_rule_applies_regex_rename
19
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+',
20
+ 'replacement' => '*')
21
+ assert_equal(['foo/*/bar/22', true], rule.apply('foo/1/bar/22'))
22
+ end
23
+
24
+ def test_rule_applies_grouping_with_replacements
25
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '([0-9]+)',
26
+ 'replacement' => '\\1\\1')
27
+ assert_equal(['foo/11/bar/22', true], rule.apply('foo/1/bar/22'))
28
+ end
29
+
30
+ def test_rule_renames_all_matches_when_replace_all_is_true
31
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+',
32
+ 'replacement' => '*',
33
+ 'replace_all' => true)
34
+ assert_equal(['foo/*/bar/*', true], rule.apply('foo/1/bar/22'))
35
+ end
36
+
37
+ def test_rule_with_no_match
38
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => 'QQ',
39
+ 'replacement' => 'qq')
40
+ assert_equal(['foo/1/bar/22', false], rule.apply('foo/1/bar/22'))
41
+ end
42
+
43
+ def test_applies_rules_in_order
44
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+',
45
+ 'replacement' => '*',
46
+ 'replace_all' => true,
47
+ 'eval_order' => 0)
48
+ rerule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '\*',
49
+ 'replacement' => 'x',
50
+ 'replace_all' => true,
51
+ 'eval_order' => 1)
52
+ @engine << rerule
53
+ @engine << rule
54
+
55
+ assert_equal('foo/x/bar/x', @engine.rename('foo/1/bar/22'))
56
+ end
57
+
58
+ def test_can_apply_rules_to_all_segments
59
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+.*',
60
+ 'replacement' => '*',
61
+ 'each_segment' => true)
62
+ @engine << rule
63
+
64
+ assert_equal('foo/*/bar/*', @engine.rename('foo/1a/bar/22b'))
65
+ end
66
+
67
+ def test_stops_after_terminate_chain
68
+ rule0 = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+',
69
+ 'replacement' => '*',
70
+ 'each_segment' => true,
71
+ 'eval_order' => 0,
72
+ 'terminate_chain' => true)
73
+ rule1 = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '.*',
74
+ 'replacement' => 'X',
75
+ 'replace_all' => true,
76
+ 'eval_order' => 1)
77
+ @engine << rule0
78
+ @engine << rule1
79
+
80
+ assert_equal('foo/*/bar/*', @engine.rename('foo/1/bar/22'))
81
+ end
82
+ end
@@ -8,10 +8,6 @@ module NewRelic
8
8
  @agent = NewRelic::Agent::ShimAgent.new
9
9
  end
10
10
 
11
- def test_serialize
12
- assert_equal(nil, @agent.serialize, "should return nil when shut down")
13
- end
14
-
15
11
  def test_merge_data_from
16
12
  assert_equal(nil, @agent.merge_data_from(mock('metric data')))
17
13
  end
@@ -33,6 +33,13 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
33
33
  assert_equal 2, @sampler.transaction_data.sql_data.size
34
34
  end
35
35
 
36
+ def test_notice_sql_truncates_query
37
+ @sampler.notice_first_scope_push nil
38
+ message = 'a' * 17_000
39
+ @sampler.notice_sql message, "Database/test/select", nil, 1.5
40
+ assert_equal('a' * 16_381 + '...', @sampler.transaction_data.sql_data[0].sql)
41
+ end
42
+
36
43
  def test_harvest_slow_sql
37
44
  data = NewRelic::Agent::TransactionSqlData.new
38
45
  data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::StatsEngine::GCProfilerTest < Test::Unit::TestCase
4
+ def test_init_profiler_for_rails_bench
5
+ ::GC.stubs(:time)
6
+ ::GC.stubs(:collections)
7
+
8
+ assert_equal(NewRelic::Agent::StatsEngine::GCProfiler::RailsBench,
9
+ NewRelic::Agent::StatsEngine::GCProfiler.init.class)
10
+ end
11
+
12
+ def test_init_profiler_for_ruby_19
13
+ defined = defined?(::GC::Profiler)
14
+ if !defined
15
+ ::GC.const_set(:Profiler, Module.new)
16
+ end
17
+ ::GC::Profiler.stubs(:enabled?).returns(true)
18
+ ::GC::Profiler.stubs(:total_time).returns(0)
19
+ ::GC.stubs(:count).returns(0)
20
+
21
+ assert_equal(NewRelic::Agent::StatsEngine::GCProfiler::Ruby19,
22
+ NewRelic::Agent::StatsEngine::GCProfiler.init.class)
23
+ ensure
24
+ ::GC.send(:remove_const, :Profiler) unless defined
25
+ end
26
+
27
+ def test_init_profiler_for_rbx
28
+ defined = defined?(::Rubinius::GC)
29
+ if !defined
30
+ Object.const_set(:Rubinius, Module.new)
31
+ ::Rubinius.const_set(:GC, Module.new)
32
+ end
33
+ ::Rubinius::GC.stubs(:count).returns(0)
34
+ ::Rubinius::GC.stubs(:time).returns(0)
35
+
36
+ assert_equal(NewRelic::Agent::StatsEngine::GCProfiler::Rubinius,
37
+ NewRelic::Agent::StatsEngine::GCProfiler.init.class)
38
+ ensure
39
+ Object.send(:remove_const, :Rubinius) unless defined
40
+ end
41
+
42
+ def test_collect_gc_data
43
+ GC.disable unless NewRelic::LanguageSupport.using_engine?('jruby')
44
+ if NewRelic::LanguageSupport.using_engine?('rbx')
45
+ agent = ::Rubinius::Agent.loopback
46
+ agent.stubs(:get).with('system.gc.young.total_wallclock') \
47
+ .returns([:value, 1000], [:value, 2500])
48
+ agent.stubs(:get).with('system.gc.full.total_wallclock') \
49
+ .returns([:value, 2000], [:value, 3500])
50
+ agent.stubs(:get).with('system.gc.young.count') \
51
+ .returns([:value, 1], [:value, 2])
52
+ agent.stubs(:get).with('system.gc.full.count') \
53
+ .returns([:value, 1], [:value, 2])
54
+ ::Rubinius::Agent.stubs(:loopback).returns(agent)
55
+ elsif NewRelic::LanguageSupport.using_version?('1.9')
56
+ ::GC::Profiler.stubs(:enabled?).returns(true)
57
+ ::GC::Profiler.stubs(:total_time).returns(1.0, 4.0)
58
+ ::GC.stubs(:count).returns(1, 3)
59
+ ::GC::Profiler.stubs(:clear).returns(nil)
60
+ elsif NewRelic::LanguageSupport.using_version?('1.8.7') &&
61
+ RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/
62
+ ::GC.stubs(:time).returns(1000000, 4000000)
63
+ ::GC.stubs(:collections).returns(1, 3)
64
+ else
65
+ return true # no need to test if we're not collecting GC metrics
66
+ end
67
+
68
+ engine = NewRelic::Agent.instance.stats_engine
69
+ tracer = NewRelic::Agent::TransactionSampler.new
70
+ tracer.instance_variable_set(:@last_sample,
71
+ NewRelic::TransactionSample.new)
72
+ engine.transaction_sampler = tracer
73
+ engine.start_transaction
74
+ scope = engine.push_scope "scope"
75
+ engine.pop_scope scope, 0.01
76
+ engine.end_transaction
77
+
78
+ gc_stats = engine.get_stats('GC/cumulative')
79
+ assert_equal 2, gc_stats.call_count
80
+ assert_equal 3.0, gc_stats.total_call_time
81
+ assert_equal(3.0, tracer.last_sample.params[:custom_params][:gc_time])
82
+ ensure
83
+ GC.enable unless NewRelic::LanguageSupport.using_engine?('jruby')
84
+ end
85
+ end