newrelic_rpm 3.9.6.257 → 3.9.7.266

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG +45 -0
  3. data/lib/new_relic/agent.rb +0 -1
  4. data/lib/new_relic/agent/agent.rb +61 -22
  5. data/lib/new_relic/agent/agent_logger.rb +11 -9
  6. data/lib/new_relic/agent/configuration/default_source.rb +25 -2
  7. data/lib/new_relic/agent/cross_app_monitor.rb +15 -53
  8. data/lib/new_relic/agent/cross_app_tracing.rb +11 -3
  9. data/lib/new_relic/agent/custom_event_aggregator.rb +98 -0
  10. data/lib/new_relic/agent/event_buffer.rb +84 -0
  11. data/lib/new_relic/agent/harvester.rb +4 -15
  12. data/lib/new_relic/agent/hostname.rb +1 -9
  13. data/lib/new_relic/agent/inbound_request_monitor.rb +41 -0
  14. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +2 -2
  15. data/lib/new_relic/agent/instrumentation/queue_time.rb +0 -5
  16. data/lib/new_relic/agent/instrumentation/resque.rb +4 -9
  17. data/lib/new_relic/agent/javascript_instrumentor.rb +0 -12
  18. data/lib/new_relic/agent/method_tracer_helpers.rb +3 -1
  19. data/lib/new_relic/agent/new_relic_service.rb +1 -1
  20. data/lib/new_relic/agent/sampled_buffer.rb +13 -41
  21. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +52 -24
  22. data/lib/new_relic/agent/sized_buffer.rb +23 -0
  23. data/lib/new_relic/agent/synthetics_monitor.rb +50 -0
  24. data/lib/new_relic/agent/system_info.rb +28 -0
  25. data/lib/new_relic/agent/transaction.rb +78 -9
  26. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +25 -0
  27. data/lib/new_relic/agent/{request_sampler.rb → transaction_event_aggregator.rb} +53 -19
  28. data/lib/new_relic/agent/transaction_sample_builder.rb +3 -1
  29. data/lib/new_relic/agent/transaction_sampler.rb +10 -0
  30. data/lib/new_relic/agent/transaction_state.rb +1 -9
  31. data/lib/new_relic/coerce.rb +8 -2
  32. data/lib/new_relic/control.rb +0 -1
  33. data/lib/new_relic/local_environment.rb +12 -8
  34. data/lib/new_relic/metric_spec.rb +11 -2
  35. data/lib/new_relic/rack/developer_mode.rb +9 -1
  36. data/lib/new_relic/transaction_sample.rb +4 -10
  37. data/lib/new_relic/version.rb +1 -1
  38. data/newrelic_rpm.gemspec +1 -0
  39. data/test/agent_helper.rb +15 -2
  40. data/test/environments/norails/Gemfile +1 -0
  41. data/test/environments/rails21/Gemfile +1 -0
  42. data/test/environments/rails22/Gemfile +1 -0
  43. data/test/environments/rails23/Gemfile +1 -0
  44. data/test/environments/rails30/Gemfile +1 -0
  45. data/test/environments/rails31/Gemfile +1 -0
  46. data/test/environments/rails32/Gemfile +2 -1
  47. data/test/environments/rails40/Gemfile +2 -1
  48. data/test/environments/rails41/Gemfile +3 -1
  49. data/test/environments/rails42/Gemfile +3 -1
  50. data/test/fixtures/cross_agent_tests/README.md +6 -2
  51. data/test/fixtures/cross_agent_tests/docker_container_id/README.md +6 -0
  52. data/test/fixtures/cross_agent_tests/docker_container_id/cases.json +22 -0
  53. data/test/fixtures/cross_agent_tests/docker_container_id/docker-0.9.1.txt +10 -0
  54. data/test/fixtures/cross_agent_tests/docker_container_id/docker-1.0.0.txt +10 -0
  55. data/test/fixtures/cross_agent_tests/docker_container_id/docker-1.3.txt +9 -0
  56. data/test/fixtures/cross_agent_tests/docker_container_id/empty.txt +0 -0
  57. data/test/fixtures/cross_agent_tests/docker_container_id/lxc-containers-without-docker.txt +10 -0
  58. data/test/fixtures/cross_agent_tests/proc_meminfo/README.md +7 -0
  59. data/test/fixtures/cross_agent_tests/proc_meminfo/meminfo_4096MB.txt +47 -0
  60. data/test/fixtures/cross_agent_tests/rum_client_config.json +0 -80
  61. data/test/fixtures/cross_agent_tests/synthetics/synthetics.json +317 -0
  62. data/test/multiverse/lib/multiverse/runner.rb +1 -1
  63. data/test/multiverse/lib/multiverse/suite.rb +1 -0
  64. data/test/multiverse/suites/active_record/Envfile +42 -8
  65. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +8 -4
  66. data/test/multiverse/suites/agent_only/audit_log_test.rb +0 -2
  67. data/test/multiverse/suites/agent_only/collector_exception_handling_test.rb +1 -2
  68. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +5 -5
  69. data/test/multiverse/suites/agent_only/encoding_handling_test.rb +2 -0
  70. data/test/multiverse/suites/agent_only/harvest_timestamps_test.rb +8 -0
  71. data/test/multiverse/suites/agent_only/synthetics_test.rb +139 -0
  72. data/test/multiverse/suites/delayed_job/Envfile +79 -0
  73. data/test/multiverse/suites/delayed_job/before_suite.rb +33 -0
  74. data/test/multiverse/suites/delayed_job/config/newrelic.yml +18 -0
  75. data/test/multiverse/suites/delayed_job/delayed_job_sampler_test.rb +131 -0
  76. data/test/multiverse/suites/delayed_job/unsupported_backend_test.rb +24 -0
  77. data/test/multiverse/suites/high_security/high_security_test.rb +3 -10
  78. data/test/multiverse/suites/rails/Envfile +4 -4
  79. data/test/multiverse/suites/rails/request_statistics_test.rb +4 -4
  80. data/test/new_relic/agent/agent_test.rb +32 -4
  81. data/test/new_relic/agent/configuration/default_source_test.rb +6 -0
  82. data/test/new_relic/agent/cross_app_monitor_test.rb +23 -29
  83. data/test/new_relic/agent/custom_event_aggregator_test.rb +88 -0
  84. data/test/new_relic/agent/event_buffer_test_cases.rb +152 -0
  85. data/test/new_relic/agent/harvester_test.rb +5 -25
  86. data/test/new_relic/agent/hostname_test.rb +1 -1
  87. data/test/new_relic/agent/inbound_request_monitor_test.rb +49 -0
  88. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +9 -9
  89. data/test/new_relic/agent/instrumentation/queue_time_test.rb +0 -19
  90. data/test/new_relic/agent/javascript_instrumentor_test.rb +0 -45
  91. data/test/new_relic/agent/new_relic_service_test.rb +17 -12
  92. data/test/new_relic/agent/pipe_channel_manager_test.rb +4 -4
  93. data/test/new_relic/agent/sampled_buffer_test.rb +73 -145
  94. data/test/new_relic/agent/sized_buffer_test.rb +29 -0
  95. data/test/new_relic/agent/synthetics_monitor_test.rb +96 -0
  96. data/test/new_relic/agent/system_info_test.rb +19 -3
  97. data/test/new_relic/agent/transaction/developer_mode_sample_buffer_test.rb +14 -8
  98. data/test/new_relic/agent/transaction/synthetics_sample_buffer_test.rb +38 -0
  99. data/test/new_relic/agent/{request_sampler_test.rb → transaction_event_aggregator_test.rb} +55 -4
  100. data/test/new_relic/agent/transaction_sampler_test.rb +33 -11
  101. data/test/new_relic/agent/transaction_state_test.rb +0 -1
  102. data/test/new_relic/agent/transaction_test.rb +121 -19
  103. data/test/new_relic/coerce_test.rb +38 -3
  104. data/test/new_relic/control_test.rb +1 -0
  105. data/test/new_relic/fake_collector.rb +6 -2
  106. data/test/new_relic/fake_external_server.rb +2 -6
  107. data/test/new_relic/fake_server.rb +46 -29
  108. data/test/new_relic/http_client_test_cases.rb +71 -2
  109. data/test/new_relic/local_environment_test.rb +78 -0
  110. data/test/new_relic/metric_parser/metric_parser_test.rb +2 -0
  111. data/test/new_relic/metric_spec_test.rb +43 -0
  112. data/test/new_relic/multiverse_helpers.rb +11 -18
  113. data/test/new_relic/rack/browser_monitoring_test.rb +1 -18
  114. data/test/new_relic/rack/developer_mode_test.rb +1 -1
  115. data/test/new_relic/transaction_sample_test.rb +14 -4
  116. data/test/performance/suites/rack_middleware.rb +3 -1
  117. data/test/test_helper.rb +33 -0
  118. metadata +48 -7
  119. metadata.gz.sig +0 -0
  120. data/lib/new_relic/agent/browser_token.rb +0 -41
  121. data/test/multiverse/suites/agent_only/before_suite.rb +0 -7
  122. data/test/new_relic/agent/browser_token_test.rb +0 -56
@@ -0,0 +1,18 @@
1
+ ---
2
+ development:
3
+ error_collector:
4
+ enabled: true
5
+ apdex_t: 0.5
6
+ ssl: false
7
+ monitor_mode: true
8
+ license_key: bootstrap_newrelic_admin_license_key_000
9
+ developer_mode: false
10
+ app_name: test
11
+ host: 127.0.0.1
12
+ api_host: 127.0.0.1
13
+ transaction_trace:
14
+ record_sql: obfuscated
15
+ enabled: true
16
+ stack_trace_threshold: 0.5
17
+ transaction_threshold: 1.0
18
+ capture_params: false
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'multiverse_helpers'
6
+ require 'minitest/unit'
7
+
8
+ require 'new_relic/delayed_job_injection'
9
+
10
+ if NewRelic::Agent::Samplers::DelayedJobSampler.supported_backend?
11
+ class DelayedJobSamplerTest < Minitest::Test
12
+ include MultiverseHelpers
13
+
14
+ TestWorker = Struct.new(:name, :read_ahead)
15
+
16
+ setup_and_teardown_agent do
17
+ NewRelic::DelayedJobInjection.worker_name = "delayed"
18
+ @worker = TestWorker.new("delayed", 1)
19
+ @sampler = NewRelic::Agent::Samplers::DelayedJobSampler.new
20
+ end
21
+
22
+ def after_teardown
23
+ # This appears in the base delayed_job gem even after the adapter split
24
+ # so it appears to be safe to call across adapters
25
+ Delayed::Job.delete_all
26
+ end
27
+
28
+ class IWantToWait
29
+ def take_action
30
+ end
31
+ end
32
+
33
+ def test_cant_create_without_injection
34
+ NewRelic::DelayedJobInjection.worker_name = nil
35
+ assert_raises(NewRelic::Agent::Sampler::Unsupported) do
36
+ NewRelic::Agent::Samplers::DelayedJobSampler.new
37
+ end
38
+ end
39
+
40
+ def test_sampler_no_failures_or_locks
41
+ @sampler.poll
42
+
43
+ assert_metrics_recorded(
44
+ "Workers/DelayedJob/failed_jobs" => { :total_call_time => 0 },
45
+ "Workers/DelayedJob/locked_jobs" => { :total_call_time => 0 })
46
+ end
47
+
48
+ def test_sampler_sees_failures
49
+ job = IWantToWait.new.delay.take_action
50
+
51
+ return unless job.respond_to?(:fail!)
52
+ job.fail!
53
+
54
+ @sampler.poll
55
+
56
+ assert_metrics_recorded(
57
+ "Workers/DelayedJob/failed_jobs" => { :total_call_time => 1 })
58
+ end
59
+
60
+ def test_sampler_sees_locks
61
+ IWantToWait.new.delay.take_action
62
+ ::Delayed::Job.reserve(@worker)
63
+
64
+ @sampler.poll
65
+
66
+ assert_metrics_recorded(
67
+ "Workers/DelayedJob/locked_jobs" => { :total_call_time => 1 })
68
+ end
69
+
70
+ def test_sampler_queue_depth_with_job
71
+ IWantToWait.new.delay.take_action
72
+
73
+ @sampler.poll
74
+
75
+ assert_metrics_recorded(
76
+ "Workers/DelayedJob/queue_length/priority/0" => { :total_call_time => 1 },
77
+ "Workers/DelayedJob/queue_length/all" => { :total_call_time => 1 })
78
+ end
79
+
80
+ def test_sampler_queue_depth_with_alternate_priority
81
+ IWantToWait.new.delay(:priority => 7).take_action
82
+
83
+ @sampler.poll
84
+
85
+ assert_metrics_recorded(
86
+ "Workers/DelayedJob/queue_length/priority/7" => { :total_call_time => 1 },
87
+ "Workers/DelayedJob/queue_length/all" => { :total_call_time => 1 })
88
+ end
89
+
90
+ def test_sampler_queue_depth_with_default_queue
91
+ return unless Delayed::Job.instance_methods.include?(:queue)
92
+
93
+ IWantToWait.new.delay.take_action
94
+
95
+ @sampler.poll
96
+
97
+ assert_metrics_recorded(
98
+ "Workers/DelayedJob/queue_length/name/default" => { :total_call_time => 1 },
99
+ "Workers/DelayedJob/queue_length/all" => { :total_call_time => 1 })
100
+ end
101
+
102
+ def test_sampler_queue_depth_with_alternate_queues
103
+ return unless Delayed::Job.instance_methods.include?(:queue)
104
+
105
+ IWantToWait.new.delay(:queue => "cue").take_action
106
+ IWantToWait.new.delay(:queue => "cute").take_action
107
+
108
+ @sampler.poll
109
+
110
+ assert_metrics_recorded(
111
+ "Workers/DelayedJob/queue_length/name/cue" => { :total_call_time => 1 },
112
+ "Workers/DelayedJob/queue_length/name/cute" => { :total_call_time => 1 },
113
+ "Workers/DelayedJob/queue_length/all" => { :total_call_time => 2 })
114
+ end
115
+
116
+ def test_sampler_queue_depth_with_queues_and_priorities
117
+ return unless Delayed::Job.instance_methods.include?(:queue)
118
+
119
+ IWantToWait.new.delay(:priority => 1, :queue => "cue").take_action
120
+ IWantToWait.new.delay(:priority => 1, :queue => "cute").take_action
121
+
122
+ @sampler.poll
123
+
124
+ assert_metrics_recorded(
125
+ "Workers/DelayedJob/queue_length/name/cue" => { :total_call_time => 1 },
126
+ "Workers/DelayedJob/queue_length/name/cute" => { :total_call_time => 1 },
127
+ "Workers/DelayedJob/queue_length/priority/1" => { :total_call_time => 2 },
128
+ "Workers/DelayedJob/queue_length/all" => { :total_call_time => 2 })
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'multiverse_helpers'
6
+ require 'minitest/unit'
7
+
8
+ require 'new_relic/delayed_job_injection'
9
+
10
+ if !NewRelic::Agent::Samplers::DelayedJobSampler.supported_backend?
11
+ class UnsupportedBackendTest < Minitest::Test
12
+ include MultiverseHelpers
13
+
14
+ setup_and_teardown_agent do
15
+ NewRelic::DelayedJobInjection.worker_name = "delayed"
16
+ end
17
+
18
+ def test_unsupported_raises_on_instantiation
19
+ assert_raises(NewRelic::Agent::Sampler::Unsupported) do
20
+ NewRelic::Agent::Samplers::DelayedJobSampler.new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -13,14 +13,10 @@ class HighSecurityTest < Minitest::Test
13
13
 
14
14
  include MultiverseHelpers
15
15
 
16
- setup_and_teardown_agent do |_collector|
17
- $collector = nil
18
- setup_collector(true)
19
-
20
- $collector.stub('connect', {
16
+ setup_and_teardown_agent do |collector|
17
+ collector.use_ssl = true
18
+ collector.stub('connect', {
21
19
  "agent_run_id" => 1,
22
- "listen_to_server_config" => true,
23
-
24
20
  "agent_config" => {
25
21
  "capture_params" => true,
26
22
  }
@@ -29,9 +25,7 @@ class HighSecurityTest < Minitest::Test
29
25
 
30
26
  def test_connects_via_ssl_no_matter_what
31
27
  assert_equal 1, $collector.calls_for('connect').size
32
-
33
28
  trigger_agent_reconnect(:ssl => false)
34
-
35
29
  assert_equal 2, $collector.calls_for('connect').size
36
30
  end
37
31
 
@@ -62,5 +56,4 @@ class HighSecurityTest < Minitest::Test
62
56
  refute_nil last_transaction_trace.params[:custom_params][:'nr.trip_id']
63
57
  refute_nil last_transaction_trace.params[:custom_params][:'nr.path_hash']
64
58
  end
65
-
66
59
  end
@@ -1,12 +1,12 @@
1
1
  if RUBY_VERSION >= '1.9.3'
2
2
  gemfile <<-RB
3
- gem 'rails', '~>4.2.0.beta2'
3
+ gem 'rails', '~>4.2.0.beta4'
4
4
  gem 'haml', :require => false
5
5
  gem 'minitest', '5.2.3'
6
6
  RB
7
7
 
8
8
  gemfile <<-RB
9
- gem 'rails', '~>4.1.6'
9
+ gem 'rails', '~>4.1.7'
10
10
  # Multiverse has an incompatibility with Minitest 5.3.0, so lock here for
11
11
  # now
12
12
  gem 'minitest', '5.2.3'
@@ -16,13 +16,13 @@ if RUBY_VERSION >= '1.9.3'
16
16
  RB
17
17
 
18
18
  gemfile <<-RB
19
- gem 'rails', '~>4.0.10'
19
+ gem 'rails', '~>4.0.11'
20
20
  gem 'haml', '4.0.2' # Getting load issues with haml 4.0.3
21
21
  RB
22
22
  end
23
23
 
24
24
  gemfile <<-RB
25
- gem 'rails', '~>3.2.16'
25
+ gem 'rails', '~>3.2.20'
26
26
  gem 'haml', '4.0.2' # Getting load issues with haml 4.0.3
27
27
  gem 'minitest_tu_shim', :require => false
28
28
  RB
@@ -93,11 +93,11 @@ class RequestStatsTest < RailsMultiverseTest
93
93
  :'cross_process_id' => 'boo',
94
94
  :'encoding_key' => "\0",
95
95
  :'trusted_account_ids' => [1]) do
96
- request_headers = {
97
- 'X-NewRelic-ID' => Base64.encode64('1#234'),
98
- 'X-NewRelic-Transaction' => Base64.encode64('["8badf00d",1]')
96
+ rack_env = {
97
+ 'HTTP_X_NEWRELIC_ID' => Base64.encode64('1#234'),
98
+ 'HTTP_X_NEWRELIC_TRANSACTION' => Base64.encode64('["8badf00d",1]')
99
99
  }
100
- get '/request_stats/cross_app_action', {}, request_headers
100
+ get '/request_stats/cross_app_action', {}, rack_env
101
101
 
102
102
  NewRelic::Agent.agent.send(:harvest_and_send_analytic_event_data)
103
103
 
@@ -97,6 +97,34 @@ module NewRelic
97
97
  end
98
98
  end
99
99
 
100
+ def test_after_fork_should_not_start_agent_multiple_times
101
+ with_config(:monitor_mode => true) do
102
+ $queue = Queue.new
103
+ def @agent.setup_and_start_agent(*_)
104
+ $queue << 'started!'
105
+ end
106
+
107
+ # This is a hack to make the race condition that we're trying to test
108
+ # for more likely for the purposes of this test.
109
+ harvester = @agent.harvester
110
+ def harvester.mark_started
111
+ sleep 0.01
112
+ super
113
+ end
114
+
115
+ threads = []
116
+ 100.times do
117
+ threads << Thread.new do
118
+ @agent.after_fork
119
+ end
120
+ end
121
+
122
+ threads.each(&:join)
123
+
124
+ assert_equal(1, $queue.size)
125
+ end
126
+ end
127
+
100
128
  def test_transmit_data_should_emit_before_harvest_event
101
129
  got_it = false
102
130
  @agent.events.subscribe(:before_harvest) { got_it = true }
@@ -199,7 +227,7 @@ module NewRelic
199
227
  @agent.stats_engine.expects(:merge!).never
200
228
  @agent.error_collector.expects(:merge!).never
201
229
  @agent.transaction_sampler.expects(:merge!).never
202
- @agent.instance_variable_get(:@request_sampler).expects(:merge!).never
230
+ @agent.instance_variable_get(:@transaction_event_aggregator).expects(:merge!).never
203
231
  @agent.sql_sampler.expects(:merge!).never
204
232
  @agent.merge_data_for_endpoint(:metric_data, [])
205
233
  @agent.merge_data_for_endpoint(:transaction_sample_data, [])
@@ -232,11 +260,11 @@ module NewRelic
232
260
 
233
261
  def test_harvest_and_send_analytic_event_data_merges_in_samples_on_failure
234
262
  service = @agent.service
235
- request_sampler = @agent.instance_variable_get(:@request_sampler)
263
+ transaction_event_aggregator = @agent.instance_variable_get(:@transaction_event_aggregator)
236
264
  samples = [mock('some analytics event')]
237
265
 
238
- request_sampler.expects(:harvest!).returns(samples)
239
- request_sampler.expects(:merge!).with(samples)
266
+ transaction_event_aggregator.expects(:harvest!).returns(samples)
267
+ transaction_event_aggregator.expects(:merge!).with(samples)
240
268
 
241
269
  # simulate a failure in transmitting analytics events
242
270
  service.stubs(:analytic_event_data).raises(StandardError.new)
@@ -98,6 +98,12 @@ module NewRelic::Agent::Configuration
98
98
  assert_equal ['Foo'], result
99
99
  end
100
100
 
101
+ def test_convert_to_list_raises_on_totally_wrong_object
102
+ assert_raises(ArgumentError) do
103
+ DefaultSource.convert_to_list(Object.new)
104
+ end
105
+ end
106
+
101
107
  def test_rules_ignore_converts_comma_delimited_string_to_array
102
108
  with_config(:'rules.ignore_url_regexes' => 'Foo,Bar,Baz') do
103
109
  assert_equal [/Foo/, /Bar/, /Baz/], NewRelic::Agent.config[:'rules.ignore_url_regexes']
@@ -6,8 +6,9 @@ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper
6
6
 
7
7
  module NewRelic::Agent
8
8
  class CrossAppMonitorTest < Minitest::Test
9
- NEWRELIC_ID_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_ID_HEADER
10
- NEWRELIC_TXN_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_TXN_HEADER
9
+ NEWRELIC_ID_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_ID_HEADER_KEY
10
+ NEWRELIC_TXN_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_TXN_HEADER_KEY
11
+ CONTENT_LENGTH_KEY = "HTTP_CONTENT_LENGTH"
11
12
 
12
13
  AGENT_CROSS_APP_ID = "qwerty"
13
14
  REQUEST_CROSS_APP_ID = "42#1234"
@@ -29,19 +30,20 @@ module NewRelic::Agent
29
30
 
30
31
  def setup
31
32
  NewRelic::Agent.reset_config
32
- NewRelic::Agent.instance.stats_engine.clear_stats
33
- NewRelic::Agent.instance.events.clear
33
+ NewRelic::Agent.drop_buffered_data
34
+ @events = NewRelic::Agent::EventListener.new
34
35
  @response = {}
35
36
 
36
- @monitor = NewRelic::Agent::CrossAppMonitor.new
37
+ @monitor = NewRelic::Agent::CrossAppMonitor.new(@events)
37
38
  @config = {
38
- :cross_process_id => AGENT_CROSS_APP_ID,
39
- :encoding_key => ENCODING_KEY_NOOP,
40
- :trusted_account_ids => TRUSTED_ACCOUNT_IDS
39
+ :cross_process_id => AGENT_CROSS_APP_ID,
40
+ :encoding_key => ENCODING_KEY_NOOP,
41
+ :trusted_account_ids => TRUSTED_ACCOUNT_IDS,
42
+ :disable_harvest_thread => true
41
43
  }
42
44
 
43
45
  NewRelic::Agent.config.add_config_for_testing(@config)
44
- @monitor.on_finished_configuring
46
+ @events.notify(:finished_configuring)
45
47
  end
46
48
 
47
49
  def teardown
@@ -124,17 +126,13 @@ module NewRelic::Agent
124
126
  def test_includes_content_length
125
127
  with_default_timings
126
128
 
127
- when_request_runs(for_id(REQUEST_CROSS_APP_ID).merge("Content-Length" => 3000))
129
+ when_request_runs(for_id(REQUEST_CROSS_APP_ID).merge(CONTENT_LENGTH_KEY => 3000))
128
130
  assert_equal 3000, unpacked_response[CONTENT_LENGTH_POSITION]
129
131
  end
130
132
 
131
133
  def test_finds_content_length_from_headers
132
- %w{Content-Length HTTP_CONTENT_LENGTH CONTENT_LENGTH cOnTeNt-LeNgTh}.each do |key|
133
- request = { key => 42 }
134
-
135
- assert_equal(42, @monitor.content_length_from_request(request), \
136
- "Failed to find header on key #{key}")
137
- end
134
+ request = { 'HTTP_CONTENT_LENGTH' => 42 }
135
+ assert_equal(42, @monitor.content_length_from_request(request))
138
136
  end
139
137
 
140
138
  def test_writes_custom_parameters
@@ -181,25 +179,23 @@ module NewRelic::Agent
181
179
  def test_setting_response_headers_freezes_transaction_name
182
180
  in_transaction do
183
181
  request = for_id(REQUEST_CROSS_APP_ID)
184
- event_listener = NewRelic::Agent.instance.events
185
- event_listener.notify(:before_call, request)
182
+ @events.notify(:before_call, request)
186
183
 
187
184
  assert !NewRelic::Agent::Transaction.tl_current.name_frozen?
188
- event_listener.notify(:after_call, request, [200, @response, ''])
185
+ @events.notify(:after_call, request, [200, @response, ''])
189
186
  assert NewRelic::Agent::Transaction.tl_current.name_frozen?
190
187
  end
191
188
  end
192
189
 
193
190
  def test_listener_in_other_thread_has_correct_txn_state
194
191
  t = Thread.new do
195
- event_listener = NewRelic::Agent.instance.events
196
192
  in_transaction('transaction') do
197
193
  request = for_id(REQUEST_CROSS_APP_ID)
198
194
 
199
- event_listener.notify(:before_call, request)
195
+ @events.notify(:before_call, request)
200
196
  # Fake out our GUID for easier comparison in tests
201
197
  NewRelic::Agent::Transaction.tl_current.stubs(:guid).returns(TRANSACTION_GUID)
202
- event_listener.notify(:after_call, request, [200, @response, ''])
198
+ @events.notify(:after_call, request, [200, @response, ''])
203
199
  end
204
200
  end
205
201
 
@@ -229,21 +225,19 @@ module NewRelic::Agent
229
225
  #
230
226
 
231
227
  def when_request_runs(request=for_id(REQUEST_CROSS_APP_ID))
232
- event_listener = NewRelic::Agent.instance.events
233
228
  in_transaction('transaction') do
234
- event_listener.notify(:before_call, request)
229
+ @events.notify(:before_call, request)
235
230
  # Fake out our GUID for easier comparison in tests
236
231
  NewRelic::Agent::Transaction.tl_current.stubs(:guid).returns(TRANSACTION_GUID)
237
- event_listener.notify(:after_call, request, [200, @response, ''])
232
+ @events.notify(:after_call, request, [200, @response, ''])
238
233
  end
239
234
  end
240
235
 
241
236
  def when_request_has_error(request=for_id(REQUEST_CROSS_APP_ID))
242
237
  options = {}
243
- event_listener = NewRelic::Agent.instance.events
244
- event_listener.notify(:before_call, request)
245
- event_listener.notify(:notice_error, nil, options)
246
- event_listener.notify(:after_call, request, [500, @response, ''])
238
+ @events.notify(:before_call, request)
239
+ @events.notify(:notice_error, nil, options)
240
+ @events.notify(:after_call, request, [500, @response, ''])
247
241
 
248
242
  options
249
243
  end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','data_container_tests'))
7
+
8
+ require 'new_relic/agent/custom_event_aggregator'
9
+
10
+ module NewRelic::Agent
11
+ class CustomEventAggregatorTest < Minitest::Test
12
+ def setup
13
+ freeze_time
14
+ @aggregator = NewRelic::Agent::CustomEventAggregator.new
15
+ end
16
+
17
+ # Helpers for DataContainerTests
18
+
19
+ def create_container
20
+ @aggregator
21
+ end
22
+
23
+ def populate_container(container, n)
24
+ n.times do |i|
25
+ container.record(:atype, { :number => i })
26
+ end
27
+ end
28
+
29
+ include NewRelic::DataContainerTests
30
+
31
+ def test_record_by_default_limit
32
+ max_samples = NewRelic::Agent.config[:'custom_insights_events.max_samples_stored']
33
+ n = max_samples + 1
34
+ n.times do |i|
35
+ @aggregator.record(:footype, :number => i)
36
+ end
37
+
38
+ results = @aggregator.harvest!
39
+ assert_equal(max_samples, results.size)
40
+ end
41
+
42
+ def test_lowering_limit_truncates_buffer
43
+ orig_max_samples = NewRelic::Agent.config[:'custom_insights_events.max_samples_stored']
44
+
45
+ orig_max_samples.times do |i|
46
+ @aggregator.record(:footype, :number => i)
47
+ end
48
+
49
+ new_max_samples = orig_max_samples - 10
50
+ with_config(:'custom_insights_events.max_samples_stored' => new_max_samples) do
51
+ results = @aggregator.harvest!
52
+ assert_equal(new_max_samples, results.size)
53
+ end
54
+ end
55
+
56
+ def test_merge_respects_event_limits_by_type
57
+ with_config(:'custom_insights_events.max_samples_stored' => 10) do
58
+ 11.times do |i|
59
+ @aggregator.record(:t, :foo => :bar)
60
+ end
61
+ old_events = @aggregator.harvest!
62
+
63
+ 3.times do |i|
64
+ @aggregator.record(:t, :foo => :bar)
65
+ end
66
+
67
+ @aggregator.merge!(old_events)
68
+
69
+ events = @aggregator.harvest!
70
+
71
+ assert_equal(10, events.size)
72
+ end
73
+ end
74
+
75
+ def test_record_adds_type_and_timestamp_and_source
76
+ t0 = Time.now
77
+ @aggregator.record(:type_a, :foo => :bar, :baz => :qux)
78
+
79
+ events = @aggregator.harvest!
80
+
81
+ assert_equal(1, events.size)
82
+ event = events.first
83
+
84
+ assert_equal({ 'type' => 'type_a', 'timestamp' => t0.to_i, 'source' => 'Agent' }, event[0])
85
+ assert_equal({ 'foo' => 'bar' , 'baz' => 'qux' }, event[1])
86
+ end
87
+ end
88
+ end