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
@@ -84,7 +84,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
84
84
 
85
85
  def test_new_queue_integration
86
86
  # make this test deterministic
87
- Time.stubs(:now => Time.at(2))
87
+ Time.stubs(:now => Time.at(1360973845))
88
88
 
89
89
  NewRelic::Agent::AgentTestController.clear_headers
90
90
  engine.clear_stats
@@ -95,35 +95,9 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
95
95
  check_metric_time('WebFrontend/QueueTime', 1, 0.1)
96
96
  end
97
97
 
98
-
99
- def test_new_middleware_integration
100
- # make this test deterministic
101
- Time.stubs(:now => Time.at(2))
102
-
103
- engine.clear_stats
104
- start = ((Time.now - 1).to_f * 1_000_000).to_i
105
- NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_MIDDLEWARE_START'=> "t=#{start}"
106
- get :index
107
-
108
- check_metric_time('Middleware/all', 1, 0.1)
109
- end
110
-
111
- def test_new_server_time_integration
112
- # make this test deterministic
113
- Time.stubs(:now => Time.at(2))
114
-
115
- NewRelic::Agent::AgentTestController.clear_headers
116
- engine.clear_stats
117
- start = ((Time.now - 1).to_f * 1_000_000).to_i
118
- NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_REQUEST_START'=> "t=#{start}"
119
- get :index
120
-
121
- check_metric_time('WebFrontend/WebServer/all', 1, 0.1)
122
- end
123
-
124
98
  def test_new_frontend_work_integration
125
99
  # make this test deterministic
126
- Time.stubs(:now => Time.at(10))
100
+ Time.stubs(:now => Time.at(1360973845))
127
101
 
128
102
  engine.clear_stats
129
103
  times = [Time.now - 3, Time.now - 2, Time.now - 1]
@@ -132,10 +106,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
132
106
  'HTTP_X_REQUEST_START'=> "t=#{times[0]}", 'HTTP_X_QUEUE_START' => "t=#{times[1]}", 'HTTP_X_MIDDLEWARE_START' => "t=#{times[2]}"})
133
107
  get :index
134
108
 
135
-
136
- check_metric_time('WebFrontend/WebServer/all', 1, 0.1)
137
- check_metric_time('Middleware/all', 1, 0.1)
138
- check_metric_time('WebFrontend/QueueTime', 1, 0.1)
109
+ check_metric_time('WebFrontend/QueueTime', 3, 0.1)
139
110
  end
140
111
 
141
112
  def test_render_inline
@@ -161,20 +132,18 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
161
132
  'HttpDispatcher',
162
133
  'Controller/new_relic/agent/agent_test/action_with_error',
163
134
  'Errors/all',
164
- 'Middleware/all',
165
- 'WebFrontend/WebServer/all',
166
- 'WebFrontend/QueueTime']
135
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_error']
167
136
 
168
137
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
169
138
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
170
139
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
171
140
  apdex = engine.get_stats_no_scope("Apdex")
172
- score = apdex.get_apdex
173
- assert_equal 1, score[2], 'failing'
174
- assert_equal 0, score[1], 'tol'
175
- assert_equal 0, score[0], 'satisfied'
176
141
 
142
+ assert_equal 1, apdex.apdex_f, 'failing'
143
+ assert_equal 0, apdex.apdex_t, 'tol'
144
+ assert_equal 0, apdex.apdex_s, 'satisfied'
177
145
  end
146
+
178
147
  def test_controller_error
179
148
  engine.clear_stats
180
149
  assert_raise RuntimeError do
@@ -185,20 +154,18 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
185
154
  'HttpDispatcher',
186
155
  'Controller/new_relic/agent/agent_test/action_with_error',
187
156
  'Errors/all',
188
- 'WebFrontend/QueueTime',
189
- 'Middleware/all',
190
- 'WebFrontend/WebServer/all']
157
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_error']
191
158
 
192
159
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
193
160
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
194
161
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
195
162
  apdex = engine.get_stats_no_scope("Apdex")
196
- score = apdex.get_apdex
197
- assert_equal 1, score[2], 'failing'
198
- assert_equal 0, score[1], 'tol'
199
- assert_equal 0, score[0], 'satisfied'
200
163
 
164
+ assert_equal 1, apdex.apdex_f, 'failing'
165
+ assert_equal 0, apdex.apdex_t, 'tol'
166
+ assert_equal 0, apdex.apdex_s, 'satisfied'
201
167
  end
168
+
202
169
  def test_filter_error
203
170
  engine.clear_stats
204
171
  assert_raise RuntimeError do
@@ -209,28 +176,28 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
209
176
  'HttpDispatcher',
210
177
  'Controller/new_relic/agent/agent_test/action_with_before_filter_error',
211
178
  'Errors/all',
212
- 'WebFrontend/QueueTime',
213
- 'Middleware/all',
214
- 'WebFrontend/WebServer/all']
179
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_before_filter_error']
215
180
 
216
181
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0 || m.index('GC')==0}
217
182
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_before_filter_error").call_count
218
183
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
219
184
  apdex = engine.get_stats_no_scope("Apdex")
220
- score = apdex.get_apdex
221
- assert_equal 1, score[2], 'failing'
222
- assert_equal 0, score[1], 'tol'
223
- assert_equal 0, score[0], 'satisfied'
185
+
186
+ assert_equal 1, apdex.apdex_f, 'failing'
187
+ assert_equal 0, apdex.apdex_t, 'tol'
188
+ assert_equal 0, apdex.apdex_s, 'satisfied'
224
189
  end
190
+
225
191
  def test_metric__ignore_base
226
192
  engine.clear_stats
227
193
  get :base_action
228
194
  compare_metrics [], engine.metrics
229
195
  end
196
+
230
197
  def test_metric__no_ignore
231
198
  path = 'new_relic/agent/agent_test/index'
232
199
  index_stats = stats("Controller/#{path}")
233
- index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
200
+ index_apdex_stats = engine.get_stats_no_scope("Apdex/#{path}")
234
201
  assert_difference 'index_stats.call_count' do
235
202
  assert_difference 'index_apdex_stats.call_count' do
236
203
  get :index
@@ -243,7 +210,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
243
210
  path = 'new_relic/agent/agent_test/action_to_ignore_apdex'
244
211
  cpu_stats = stats("ControllerCPU/#{path}")
245
212
  index_stats = stats("Controller/#{path}")
246
- index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
213
+ index_apdex_stats = engine.get_stats_no_scope("Apdex/#{path}")
247
214
  assert_difference 'index_stats.call_count' do
248
215
  assert_no_difference 'index_apdex_stats.call_count' do
249
216
  get :action_to_ignore_apdex
@@ -279,9 +246,9 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
279
246
  end
280
247
 
281
248
  def test_controller_params
282
- agent.transaction_sampler.reset!
283
- get :index, 'number' => "001-555-1212"
284
249
  s = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
250
+ agent.transaction_sampler.reset!
251
+ get :index, 'number' => "001-555-1212"
285
252
  agent.transaction_sampler.harvest(nil)
286
253
  end
287
254
  assert_equal 1, s.size
@@ -344,7 +311,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
344
311
 
345
312
  def test_queue_headers_apache
346
313
  # make this test deterministic
347
- Time.stubs(:now => Time.at(10))
314
+ Time.stubs(:now => Time.at(1360973845))
348
315
 
349
316
  NewRelic::Agent::AgentTestController.clear_headers
350
317
  engine.clear_stats
@@ -363,7 +330,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
363
330
  end
364
331
  def test_queue_headers_heroku
365
332
  # make this test deterministic
366
- Time.stubs(:now => Time.at(10))
333
+ Time.stubs(:now => Time.at(1360973845))
367
334
 
368
335
  engine.clear_stats
369
336
  NewRelic::Agent::AgentTestController.clear_headers
@@ -383,7 +350,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
383
350
 
384
351
  def test_queue_headers_heroku_queue_length
385
352
  # make this test deterministic
386
- Time.stubs(:now => Time.at(10))
353
+ Time.stubs(:now => Time.at(1360973845))
387
354
 
388
355
  engine.clear_stats
389
356
  NewRelic::Agent::AgentTestController.clear_headers
@@ -6,7 +6,7 @@ class NewRelic::Agent::BusyCalculatorTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @now = Time.now.to_f
8
8
  NewRelic::Agent::BusyCalculator.reset
9
- @instance_busy = NewRelic::MethodTraceStats.new
9
+ @instance_busy = NewRelic::Agent::Stats.new
10
10
  NewRelic::Agent::BusyCalculator.stubs(:instance_busy_stats).returns(@instance_busy)
11
11
  end
12
12
 
@@ -11,9 +11,10 @@ module NewRelic::Agent::Configuration
11
11
  'transaction_tracer.record_sql' => 'raw',
12
12
  'error_collector.enabled' => true
13
13
  },
14
- 'apdex_t' => 1.0,
15
- 'collect_errors' => false,
16
- 'collect_traces' => true
14
+ 'apdex_t' => 1.0,
15
+ 'collect_errors' => false,
16
+ 'collect_traces' => true,
17
+ 'web_transactions_apdex' => { 'Controller/some/txn' => 1.5 }
17
18
  }
18
19
  @source = ServerSource.new(config)
19
20
  end
@@ -41,5 +42,9 @@ module NewRelic::Agent::Configuration
41
42
  def test_should_ignore_apdex_f_setting_for_transaction_threshold
42
43
  assert_equal nil, @source[:'transaction_tracer.transaction_threshold']
43
44
  end
45
+
46
+ def test_should_not_dot_the_web_transactions_apdex_hash
47
+ assert_equal 1.5, @source[:web_transactions_apdex]['Controller/some/txn']
48
+ end
44
49
  end
45
50
  end
@@ -0,0 +1,237 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ module NewRelic::Agent
4
+ class CrossAppMonitorTest < Test::Unit::TestCase
5
+ NEWRELIC_ID_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_ID_HEADER
6
+ NEWRELIC_TXN_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_TXN_HEADER
7
+
8
+ AGENT_CROSS_APP_ID = "qwerty"
9
+ REQUEST_CROSS_APP_ID = "42#1234"
10
+ TRANSACTION_GUID = '941B0E8001E444E8'
11
+ REF_TRANSACTION_GUID = '830092CDE59421D4'
12
+
13
+ TRANSACTION_NAME = 'transaction'
14
+ QUEUE_TIME = 1000
15
+ APP_TIME = 2000
16
+
17
+ ENCODING_KEY_NOOP = "\0"
18
+ TRUSTED_ACCOUNT_IDS = [42,13]
19
+
20
+ CROSS_APP_ID_POSITION = 0
21
+ TRANSACTION_NAME_POSITION = 1
22
+ QUEUE_TIME_POSITION = 2
23
+ APP_TIME_POSITION = 3
24
+ CONTENT_LENGTH_POSITION = 4
25
+
26
+ def setup
27
+ NewRelic::Agent.reset_config
28
+ NewRelic::Agent.instance.events.clear
29
+ @response = {}
30
+
31
+ @monitor = NewRelic::Agent::CrossAppMonitor.new()
32
+ @config = {
33
+ :cross_process_id => AGENT_CROSS_APP_ID,
34
+ :encoding_key => ENCODING_KEY_NOOP,
35
+ :trusted_account_ids => TRUSTED_ACCOUNT_IDS
36
+ }
37
+
38
+ NewRelic::Agent.config.apply_config( @config )
39
+ @monitor.register_event_listeners
40
+ NewRelic::Agent::TransactionInfo.get.guid = TRANSACTION_GUID
41
+
42
+ # NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new( {:log_level => 'debug'}, '', Logger.new($stderr) )
43
+ end
44
+
45
+ def teardown
46
+ NewRelic::Agent.config.remove_config( @config )
47
+ NewRelic::Agent.instance.events.clear
48
+ end
49
+
50
+
51
+ #
52
+ # Tests
53
+ #
54
+
55
+ def test_adds_response_header
56
+ with_default_timings
57
+
58
+ when_request_runs
59
+
60
+ assert_equal 'WyJxd2VydHkiLCJ0cmFuc2FjdGlvbiIsMTAwMC4wLDIwMDAuMCwtMSwiOTQxQjBFODAwMUU0NDRFOCJd', response_app_data
61
+ assert_equal [AGENT_CROSS_APP_ID, TRANSACTION_NAME, QUEUE_TIME, APP_TIME, -1, TRANSACTION_GUID], unpacked_response
62
+ end
63
+
64
+ def test_strips_bad_characters_in_transaction_name
65
+ NewRelic::Agent::BrowserMonitoring.stubs(:timings).returns(stub(
66
+ :transaction_name => "\"'goo",
67
+ :queue_time_in_seconds => QUEUE_TIME,
68
+ :app_time_in_seconds => APP_TIME))
69
+
70
+ when_request_runs
71
+
72
+ assert_equal "goo", unpacked_response[TRANSACTION_NAME_POSITION]
73
+ end
74
+
75
+ def test_doesnt_write_response_header_if_id_blank
76
+ with_default_timings
77
+
78
+ when_request_runs(for_id(''))
79
+ assert_nil response_app_data
80
+ end
81
+
82
+ def test_doesnt_write_response_header_if_untrusted_id
83
+ with_default_timings
84
+
85
+ when_request_runs(for_id("4#1234"))
86
+ assert_nil response_app_data
87
+ end
88
+
89
+ def test_doesnt_write_response_header_if_improperly_formatted_id
90
+ with_default_timings
91
+
92
+ when_request_runs(for_id("42"))
93
+ assert_nil response_app_data
94
+ end
95
+
96
+ def test_doesnt_add_header_if_no_id_in_request
97
+ when_request_runs({})
98
+ assert_nil response_app_data
99
+ end
100
+
101
+ def test_doesnt_add_header_if_no_id_on_agent
102
+ # Since a +nil+ will make it fall back to the config installed in setup,
103
+ # we need to remove that first in order to test the no-id case
104
+ newconfig = @config.merge( :cross_process_id => nil )
105
+ NewRelic::Agent.config.remove_config( @config )
106
+
107
+ with_config( newconfig ) do
108
+ when_request_runs
109
+ assert_nil response_app_data
110
+ end
111
+ end
112
+
113
+ def test_doesnt_add_header_if_config_disabled
114
+ with_config(:"cross_application_tracer.enabled" => false) do
115
+ when_request_runs
116
+ assert_nil response_app_data
117
+ end
118
+ end
119
+
120
+ def test_includes_content_length
121
+ with_default_timings
122
+
123
+ when_request_runs(for_id(REQUEST_CROSS_APP_ID).merge("Content-Length" => 3000))
124
+ assert_equal 3000, unpacked_response[CONTENT_LENGTH_POSITION]
125
+ end
126
+
127
+ def test_finds_content_length_from_headers
128
+ %w{Content-Length HTTP_CONTENT_LENGTH CONTENT_LENGTH cOnTeNt-LeNgTh}.each do |key|
129
+ request = { key => 42 }
130
+
131
+ assert_equal(42, @monitor.content_length_from_request(request), \
132
+ "Failed to find header on key #{key}")
133
+ end
134
+ end
135
+
136
+ def test_writes_custom_parameters
137
+ with_default_timings
138
+
139
+ NewRelic::Agent.expects(:add_custom_parameters).with(:client_cross_process_id => REQUEST_CROSS_APP_ID)
140
+ NewRelic::Agent.expects(:add_custom_parameters).with(:referring_transaction_guid => REF_TRANSACTION_GUID)
141
+
142
+ when_request_runs
143
+ end
144
+
145
+ def test_error_writes_custom_parameters
146
+ with_default_timings
147
+
148
+ options = when_request_has_error
149
+
150
+ assert_equal REQUEST_CROSS_APP_ID, options[:client_cross_process_id]
151
+ end
152
+
153
+ def test_error_doesnt_write_custom_parameters_if_no_id
154
+ with_default_timings
155
+
156
+ options = when_request_has_error(for_id(''))
157
+
158
+ assert_equal false, options.key?(:client_cross_process_id)
159
+ end
160
+
161
+ def test_writes_metric
162
+ with_default_timings
163
+
164
+ metric = mock()
165
+ metric.expects(:record_data_point).with(APP_TIME)
166
+ NewRelic::Agent.instance.stats_engine.stubs(:get_stats_no_scope).returns(metric)
167
+
168
+ when_request_runs
169
+ end
170
+
171
+ def test_doesnt_write_metric_if_id_blank
172
+ with_default_timings
173
+ NewRelic::Agent.instance.stats_engine.expects(:get_stats_no_scope).never
174
+
175
+ when_request_runs(for_id(''))
176
+ end
177
+
178
+ def test_decoding_blank
179
+ assert_equal "",
180
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions.decode_with_key( 'querty', "" )
181
+ end
182
+
183
+ def test_encode_with_nil_uses_empty_key
184
+ assert_equal "querty",
185
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions.encode_with_key( nil, 'querty' )
186
+ end
187
+
188
+
189
+ #
190
+ # Helpers
191
+ #
192
+
193
+ def when_request_runs(request=for_id(REQUEST_CROSS_APP_ID))
194
+ event_listener = NewRelic::Agent.instance.events
195
+ event_listener.notify(:before_call, request)
196
+ event_listener.notify(:start_transaction, 'a name')
197
+ event_listener.notify(:after_call, request, [200, @response, ''])
198
+ end
199
+
200
+ def when_request_has_error(request=for_id(REQUEST_CROSS_APP_ID))
201
+ options = {}
202
+ event_listener = NewRelic::Agent.instance.events
203
+ event_listener.notify(:before_call, request)
204
+ event_listener.notify(:notice_error, nil, options)
205
+ event_listener.notify(:after_call, request, [500, @response, ''])
206
+
207
+ options
208
+ end
209
+
210
+ def with_default_timings
211
+ NewRelic::Agent::BrowserMonitoring.stubs(:timings).returns(stub(
212
+ :transaction_name => TRANSACTION_NAME,
213
+ :queue_time_in_seconds => QUEUE_TIME,
214
+ :app_time_in_seconds => APP_TIME))
215
+ end
216
+
217
+ def for_id(id)
218
+ encoded_id = id == "" ? "" : Base64.encode64(id)
219
+ encoded_txn_info = Base64.encode64( NewRelic.json_dump([ REF_TRANSACTION_GUID, false ]) )
220
+
221
+ return {
222
+ NEWRELIC_ID_HEADER => encoded_id,
223
+ NEWRELIC_TXN_HEADER => encoded_txn_info,
224
+ }
225
+ end
226
+
227
+ def response_app_data
228
+ @response['X-NewRelic-App-Data']
229
+ end
230
+
231
+ def unpacked_response
232
+ return nil unless response_app_data
233
+ NewRelic.json_load(Base64.decode64(response_app_data))
234
+ end
235
+
236
+ end
237
+ end