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
@@ -1,84 +1,461 @@
1
- unless ENV['FAST_TESTS']
2
- require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
3
 
4
- class NewRelic::Agent::Instrumentation::NetInstrumentationTest < Test::Unit::TestCase
5
- include NewRelic::Agent::Instrumentation::ControllerInstrumentation
6
- def setup
7
- NewRelic::Agent.manual_start
8
- @engine = NewRelic::Agent.instance.stats_engine
9
- @engine.clear_stats
4
+ require 'net/http'
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+ require 'new_relic/agent/cross_app_tracing'
7
+
8
+ # Add some stuff to Net::HTTP::HTTPResponse to facilitate building response data
9
+ class Net::HTTPResponse
10
+ def to_s
11
+ buf = ''
12
+ buf << "HTTP/%s %d %s\r\n" % [ self.http_version, self.code, self.message ]
13
+ self.each_capitalized {|k,v| buf << k << ': ' << v << "\r\n" }
14
+ buf << "\r\n"
15
+ buf << @body if @body
16
+ return buf
17
+ end
18
+ def initialize_copy( original )
19
+ @http_version = @http_version.dup
20
+ @code = @code.dup
21
+ @message = @message.dup
22
+ @body = @body.dup
23
+ @read = false
24
+ @header = @header.dup
25
+ end
26
+
27
+ unless instance_methods.map {|name| name.to_sym }.include?( :body= )
28
+ def body=( newbody )
29
+ @body = newbody
10
30
  end
31
+ end
32
+ end
33
+
34
+ class NewRelic::Agent::Instrumentation::NetInstrumentationTest < Test::Unit::TestCase
35
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation,
36
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions,
37
+ NewRelic::Agent::CrossAppTracing
38
+
39
+ CANNED_RESPONSE = Net::HTTPOK.new( '1.1', '200', 'OK' )
40
+ CANNED_RESPONSE.body =
41
+ '<html><head><title>Canned Response</title></head><body>Canned response.</body></html>'
42
+ CANNED_RESPONSE['content-type'] = 'text/html; charset=UTF-8'
43
+ CANNED_RESPONSE['date'] = 'Tue, 29 Jan 2013 21:52:04 GMT'
44
+ CANNED_RESPONSE['expires'] = '-1'
45
+ CANNED_RESPONSE['server'] = 'gws'
46
+ CANNED_RESPONSE.freeze
47
+
48
+ TRANSACTION_GUID = 'BEC1BC64675138B9'
49
+
50
+
51
+ def setup
52
+ NewRelic::Agent.manual_start(
53
+ :"cross_application_tracer.enabled" => false,
54
+ :"transaction_tracer.enabled" => true,
55
+ :cross_process_id => '269975#22824',
56
+ :encoding_key => 'gringletoes'
57
+ )
58
+
59
+ # $stderr.puts '', '---', ''
60
+ # NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new( {:log_level => 'debug'}, '', Logger.new($stderr) )
61
+
62
+ @engine = NewRelic::Agent.instance.stats_engine
63
+ @engine.clear_stats
64
+
65
+ @engine.start_transaction( 'test' )
66
+ NewRelic::Agent::TransactionInfo.get.guid = TRANSACTION_GUID
67
+
68
+ @sampler = NewRelic::Agent.instance.transaction_sampler
69
+ @sampler.notice_first_scope_push( Time.now.to_f )
70
+ @sampler.notice_transaction( '/path', '/path', {} )
71
+ @sampler.notice_push_scope "Controller/sandwiches/index"
72
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
73
+ @sampler.notice_push_scope "ab"
74
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
75
+ @sampler.notice_push_scope "fetch_external_service"
76
+
77
+ @response = CANNED_RESPONSE.clone
78
+ @socket = fixture_tcp_socket( @response )
79
+ end
80
+
81
+ def teardown
82
+ NewRelic::Agent.instance.transaction_sampler.reset!
83
+ Thread::current[:newrelic_scope_stack] = nil
84
+ NewRelic::Agent.instance.stats_engine.end_transaction
85
+ end
86
+
87
+
88
+ #
89
+ # Helpers
90
+ #
91
+
92
+ def fixture_tcp_socket( response )
93
+
94
+ # Don't actually talk to Google.
95
+ socket = stub("socket") do
96
+ stubs(:closed?).returns(false)
97
+ stubs(:close)
98
+
99
+ # Simulate a bunch of socket-ey stuff since Mocha doesn't really
100
+ # provide any other way to do it
101
+ class << self
102
+ attr_accessor :response, :write_checker
103
+ end
104
+
105
+ def self.check_write
106
+ self.write_checker = Proc.new
107
+ end
108
+
109
+ def self.write( buf )
110
+ self.write_checker.call( buf ) if self.write_checker
111
+ buf.length
112
+ end
113
+
114
+ def self.sysread( size, buf='' )
115
+ @data ||= response.to_s
116
+ raise EOFError if @data.empty?
117
+ buf.replace @data.slice!( 0, size )
118
+ buf
119
+ end
120
+ class << self
121
+ alias_method :read_nonblock, :sysread
122
+ end
11
123
 
12
- def metrics_without_gc
13
- @engine.metrics - ['GC/cumulative']
14
124
  end
15
125
 
16
- private :metrics_without_gc
126
+ socket.response = response
127
+ TCPSocket.stubs( :open ).returns( socket )
128
+
129
+ return socket
130
+ end
17
131
 
18
- def test_get
132
+
133
+ def make_app_data_payload( *args )
134
+ return obfuscate_with_key( 'gringletoes', args.to_json ).gsub( /\n/, '' ) + "\n"
135
+ end
136
+
137
+
138
+ def find_last_segment
139
+ builder = NewRelic::Agent.agent.transaction_sampler.builder
140
+ last_segment = nil
141
+ builder.current_segment.each_segment {|s| last_segment = s }
142
+
143
+ return last_segment
144
+ end
145
+
146
+
147
+
148
+ #
149
+ # Tests
150
+ #
151
+
152
+ def test_get
153
+ url = URI.parse('http://www.google.com/index.html')
154
+ res = Net::HTTP.start(url.host, url.port) {|http|
155
+ http.get('/index.html')
156
+ }
157
+
158
+ assert_match %r/<head>/i, res.body
159
+ assert_includes @engine.metrics, 'External/all'
160
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
161
+ assert_includes @engine.metrics, 'External/allOther'
162
+ assert_includes @engine.metrics, 'External/www.google.com/all'
163
+
164
+ assert_not_includes @engine.metrics, 'External/allWeb'
165
+ end
166
+
167
+ def test_background
168
+ res = nil
169
+
170
+ perform_action_with_newrelic_trace("task", :category => :task) do
19
171
  url = URI.parse('http://www.google.com/index.html')
20
172
  res = Net::HTTP.start(url.host, url.port) {|http|
21
173
  http.get('/index.html')
22
174
  }
23
- assert_match /<head>/i, res.body
24
- assert_equal %w[External/all External/www.google.com/Net::HTTP/GET External/allOther External/www.google.com/all].sort,
25
- metrics_without_gc.sort
26
- end
27
-
28
- def test_background
29
- perform_action_with_newrelic_trace("task", :category => :task) do
30
- url = URI.parse('http://www.google.com/index.html')
31
- res = Net::HTTP.start(url.host, url.port) {|http|
32
- http.get('/index.html')
33
- }
34
- assert_match /<head>/i, res.body
35
- end
36
- assert_equal %w[External/all External/www.google.com/Net::HTTP/GET External/allOther External/www.google.com/all
37
- External/www.google.com/Net::HTTP/GET:OtherTransaction/Background/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task].sort, metrics_without_gc.select{|m| m =~ /^External/}.sort
38
175
  end
39
176
 
40
- def test_transactional
41
- perform_action_with_newrelic_trace("task") do
42
- url = URI.parse('http://www.google.com/index.html')
43
- res = Net::HTTP.start(url.host, url.port) {|http|
44
- http.get('/index.html')
45
- }
46
- assert_match /<head>/i, res.body
47
- end
48
- assert_equal %w[External/all External/www.google.com/Net::HTTP/GET External/allWeb External/www.google.com/all
49
- External/www.google.com/Net::HTTP/GET:Controller/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task].sort, metrics_without_gc.select{|m| m =~ /^External/}.sort
50
- end
51
- def test_get__simple
52
- Net::HTTP.get URI.parse('http://www.google.com/index.html')
53
- assert_equal metrics_without_gc.sort,
54
- %w[External/all External/www.google.com/Net::HTTP/GET External/allOther External/www.google.com/all].sort
55
- end
56
- def test_ignore
57
- NewRelic::Agent.disable_all_tracing do
58
- url = URI.parse('http://www.google.com/index.html')
59
- res = Net::HTTP.start(url.host, url.port) {|http|
60
- http.post('/index.html','data')
61
- }
62
- end
63
- assert_equal 0, metrics_without_gc.size
64
- end
65
- def test_head
177
+ assert_match %r/<head>/i, res.body
178
+
179
+ assert_includes @engine.metrics, 'External/all'
180
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
181
+ assert_includes @engine.metrics, 'External/allOther'
182
+ assert_includes @engine.metrics, 'External/www.google.com/all'
183
+ assert_includes @engine.metrics,
184
+ 'OtherTransaction/Background/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task'
185
+
186
+ assert_not_includes @engine.metrics, 'External/allWeb'
187
+ end
188
+
189
+ def test_transactional
190
+ res = nil
191
+
192
+ perform_action_with_newrelic_trace("task") do
66
193
  url = URI.parse('http://www.google.com/index.html')
67
194
  res = Net::HTTP.start(url.host, url.port) {|http|
68
- http.head('/index.html')
195
+ http.get('/index.html')
69
196
  }
70
- assert_equal %w[External/all External/www.google.com/Net::HTTP/HEAD External/allOther External/www.google.com/all].sort,
71
- metrics_without_gc.sort
72
197
  end
73
198
 
74
- def test_post
199
+ assert_match %r/<head>/i, res.body
200
+
201
+ assert_includes @engine.metrics, 'External/all'
202
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
203
+ assert_includes @engine.metrics, 'External/allWeb'
204
+ assert_includes @engine.metrics, 'External/www.google.com/all'
205
+ assert_includes @engine.metrics,
206
+ 'Controller/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task'
207
+
208
+ assert_not_includes @engine.metrics, 'External/allOther'
209
+ end
210
+
211
+ def test_get__simple
212
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
213
+
214
+ assert_includes @engine.metrics, 'External/all'
215
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
216
+ assert_includes @engine.metrics, 'External/allOther'
217
+ assert_includes @engine.metrics, 'External/www.google.com/all'
218
+
219
+ assert_not_includes @engine.metrics, 'External/allWeb'
220
+ end
221
+
222
+ def test_ignore
223
+ NewRelic::Agent.disable_all_tracing do
75
224
  url = URI.parse('http://www.google.com/index.html')
76
225
  res = Net::HTTP.start(url.host, url.port) {|http|
77
226
  http.post('/index.html','data')
78
227
  }
79
- assert_equal %w[External/all External/www.google.com/Net::HTTP/POST External/allOther External/www.google.com/all].sort,
80
- metrics_without_gc.sort
81
228
  end
82
229
 
230
+ assert_not_includes @engine.metrics, 'External/all'
231
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
232
+ assert_not_includes @engine.metrics, 'External/allOther'
233
+ assert_not_includes @engine.metrics, 'External/www.google.com/all'
234
+
235
+ assert_not_includes @engine.metrics, 'External/allWeb'
236
+ end
237
+
238
+ def test_head
239
+ url = URI.parse('http://www.google.com/index.html')
240
+ res = Net::HTTP.start(url.host, url.port) {|http|
241
+ http.head('/index.html')
242
+ }
243
+
244
+ assert_includes @engine.metrics, 'External/all'
245
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/HEAD'
246
+ assert_includes @engine.metrics, 'External/allOther'
247
+ assert_includes @engine.metrics, 'External/www.google.com/all'
248
+
249
+ assert_not_includes @engine.metrics, 'External/allWeb'
250
+ end
251
+
252
+ def test_post
253
+ url = URI.parse('http://www.google.com/index.html')
254
+ res = Net::HTTP.start(url.host, url.port) {|http|
255
+ http.post('/index.html','data')
256
+ }
257
+
258
+ assert_includes @engine.metrics, 'External/all'
259
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/POST'
260
+ assert_includes @engine.metrics, 'External/allOther'
261
+ assert_includes @engine.metrics, 'External/www.google.com/all'
262
+
263
+ assert_not_includes @engine.metrics, 'External/allWeb'
264
+ end
265
+
266
+ # When an http call is made, the agent should add a request header named
267
+ # X-NewRelic-ID with a value equal to the encoded cross_app_id.
268
+
269
+ def test_adds_a_request_header_to_outgoing_requests_if_xp_enabled
270
+ @socket.check_write do |data|
271
+
272
+ # assert_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
273
+ # The above assertion won't work in Ruby 2.0.0-p0 because of a bug in the
274
+ # regexp engine. Until that's fixed we'll check the header name case
275
+ # sensitively.
276
+ assert_match /X-Newrelic-Id: VURQV1BZRkZdXUFT/, data
277
+ end
278
+
279
+ with_config(:"cross_application_tracer.enabled" => true) do
280
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
281
+ end
282
+ end
283
+
284
+ def test_adds_a_request_header_to_outgoing_requests_if_old_xp_config_is_present
285
+ @socket.check_write do |data|
286
+ # assert_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
287
+ # The above assertion won't work in Ruby 2.0.0-p0 because of a bug in the
288
+ # regexp engine. Until that's fixed we'll check the header name case
289
+ # sensitively.
290
+ assert_match /X-Newrelic-Id: VURQV1BZRkZdXUFT/, data
291
+ end
292
+
293
+ with_config(:cross_application_tracing => true) do
294
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
295
+ end
296
+ end
297
+
298
+ def test_agent_doesnt_add_a_request_header_to_outgoing_requests_if_xp_disabled
299
+ @socket.check_write do |data|
300
+ # assert_no_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
301
+ # The above assertion won't work in Ruby 2.0.0-p0 because of a bug in the
302
+ # regexp engine. Until that's fixed we'll check the header name case
303
+ # sensitively.
304
+ assert_no_match /X-Newrelic-Id: VURQV1BZRkZdXUFT/, data
305
+ end
306
+
307
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
308
+ end
309
+
310
+
311
+ def test_instrumentation_with_crossapp_enabled_records_normal_metrics_if_no_header_present
312
+ with_config(:"cross_application_tracer.enabled" => true) do
313
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
314
+ end
315
+
316
+ assert_equal 5, @engine.metrics.length
317
+
318
+ assert_includes @engine.metrics, 'External/all'
319
+ assert_includes @engine.metrics, 'External/allOther'
320
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
321
+ assert_includes @engine.metrics, 'External/www.google.com/all'
322
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
323
+
324
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
325
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
326
+ assert_not_includes @engine.metrics, 'External/allWeb'
327
+ end
328
+
329
+
330
+ def test_instrumentation_with_crossapp_disabled_records_normal_metrics_even_if_header_is_present
331
+ @response[ NR_APPDATA_HEADER ] =
332
+ make_app_data_payload( '18#1884', 'txn-name', 2, 8, 0, TRANSACTION_GUID )
333
+
334
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
335
+
336
+ assert_equal 5, @engine.metrics.length
337
+
338
+ assert_includes @engine.metrics, 'External/all'
339
+ assert_includes @engine.metrics, 'External/allOther'
340
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
341
+ assert_includes @engine.metrics, 'External/www.google.com/all'
342
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
343
+
344
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
345
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
346
+ assert_not_includes @engine.metrics, 'External/allWeb'
347
+ end
348
+
349
+
350
+ def test_instrumentation_with_crossapp_enabled_records_crossapp_metrics_if_header_present
351
+ @response[ NR_APPDATA_HEADER ] =
352
+ make_app_data_payload( '18#1884', 'txn-name', 2, 8, 0, TRANSACTION_GUID )
353
+
354
+ with_config(:"cross_application_tracer.enabled" => true) do
355
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
356
+ end
357
+
358
+ assert_equal 6, @engine.metrics.length
359
+
360
+ assert_includes @engine.metrics, 'External/all'
361
+ assert_includes @engine.metrics, 'External/allOther'
362
+ assert_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
363
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
364
+ assert_includes @engine.metrics, 'External/www.google.com/all'
365
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name:test'
366
+
367
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
368
+ assert_not_includes @engine.metrics, 'External/allWeb'
369
+
370
+ last_segment = find_last_segment()
371
+
372
+ assert_includes last_segment.params.keys, :transaction_guid
373
+ assert_equal TRANSACTION_GUID, last_segment.params[:transaction_guid]
374
+ end
375
+
376
+ def test_crossapp_metrics_allow_valid_utf8_characters
377
+ @response[ NR_APPDATA_HEADER ] =
378
+ make_app_data_payload( '12#1114', '世界線航跡蔵', 18.0, 88.1, 4096, TRANSACTION_GUID )
379
+
380
+ with_config(:"cross_application_tracer.enabled" => true) do
381
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
382
+ end
383
+
384
+ assert_equal 6, @engine.metrics.length
385
+
386
+ assert_includes @engine.metrics, 'External/all'
387
+ assert_includes @engine.metrics, 'External/allOther'
388
+ assert_includes @engine.metrics, 'ExternalApp/www.google.com/12#1114/all'
389
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/12#1114/世界線航跡蔵'
390
+ assert_includes @engine.metrics, 'External/www.google.com/all'
391
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/12#1114/世界線航跡蔵:test'
392
+
393
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
394
+ assert_not_includes @engine.metrics, 'External/allWeb'
395
+
396
+ last_segment = find_last_segment()
397
+
398
+ assert_includes last_segment.params.keys, :transaction_guid
399
+ assert_equal TRANSACTION_GUID, last_segment.params[:transaction_guid]
400
+ end
401
+
402
+ def test_crossapp_metrics_ignores_crossapp_header_with_malformed_crossprocess_id
403
+ @response[ NR_APPDATA_HEADER ] =
404
+ make_app_data_payload( '88#88#88', 'invalid', 1, 2, 4096, TRANSACTION_GUID )
405
+
406
+ with_config(:"cross_application_tracer.enabled" => true) do
407
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
408
+ end
409
+
410
+ assert_equal 5, @engine.metrics.length
411
+
412
+ assert_includes @engine.metrics, 'External/all'
413
+ assert_includes @engine.metrics, 'External/allOther'
414
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
415
+ assert_includes @engine.metrics, 'External/www.google.com/all'
416
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
417
+
418
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/88#88#88/all'
419
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/88#88#88/invalid'
420
+ assert_not_includes @engine.metrics, 'External/allWeb'
421
+ end
422
+
423
+ def test_doesnt_affect_the_request_if_an_exception_is_raised_while_setting_up_tracing
424
+ res = nil
425
+ NewRelic::Agent.instance.stats_engine.stubs( :push_scope ).
426
+ raises( NoMethodError, "undefined method `push_scope'" )
427
+
428
+ with_config(:"cross_application_tracer.enabled" => true) do
429
+ assert_nothing_raised do
430
+ res = Net::HTTP.get URI.parse('http://www.google.com/index.html')
431
+ end
432
+ end
433
+
434
+ assert_equal res, CANNED_RESPONSE.instance_variable_get( :@body )
435
+ end
436
+
437
+ def test_doesnt_affect_the_request_if_an_exception_is_raised_while_finishing_tracing
438
+ res = nil
439
+ NewRelic::Agent.instance.stats_engine.stubs( :pop_scope ).
440
+ raises( NoMethodError, "undefined method `pop_scope'" )
441
+
442
+ with_config(:"cross_application_tracer.enabled" => true) do
443
+ assert_nothing_raised do
444
+ res = Net::HTTP.get URI.parse('http://www.google.com/index.html')
445
+ end
446
+ end
447
+
448
+ assert_equal res, CANNED_RESPONSE.instance_variable_get( :@body )
449
+ end
450
+
451
+ def test_scope_stack_integrity_maintained_on_request_failure
452
+ @socket.stubs(:write).raises('fake network error')
453
+ with_config(:"cross_application_tracer.enabled" => true) do
454
+ assert_nothing_raised do
455
+ expected = @engine.push_scope('dummy')
456
+ Net::HTTP.get(URI.parse('http://www.google.com/index.html')) rescue nil
457
+ @engine.pop_scope(expected, 42)
458
+ end
459
+ end
83
460
  end
84
461
  end