newrelic_rpm 3.5.7.59 → 3.5.8.64.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data.tar.gz.sig +3 -2
  2. data/CHANGELOG +34 -3
  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 +230 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +274 -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 +53 -34
  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/resque.rb +10 -10
  17. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  18. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  19. data/lib/new_relic/agent/pipe_service.rb +8 -12
  20. data/lib/new_relic/agent/rules_engine.rb +72 -0
  21. data/lib/new_relic/agent/shim_agent.rb +0 -1
  22. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  23. data/lib/new_relic/agent/stats.rb +149 -0
  24. data/lib/new_relic/agent/stats_engine.rb +9 -0
  25. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  26. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  27. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  28. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  29. data/lib/new_relic/agent/transaction_info.rb +31 -6
  30. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  31. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  32. data/lib/new_relic/helper.rb +32 -0
  33. data/lib/new_relic/local_environment.rb +24 -32
  34. data/lib/new_relic/okjson.rb +599 -0
  35. data/lib/new_relic/transaction_sample.rb +2 -1
  36. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  37. data/lib/new_relic/version.rb +1 -1
  38. data/newrelic.yml +27 -41
  39. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  40. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  41. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  42. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  43. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  44. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  45. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  46. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  47. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  48. data/test/multiverse/suites/resque/instrumentation_test.rb +2 -2
  49. data/test/multiverse/suites/sinatra/Envfile +2 -0
  50. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  51. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  52. data/test/multiverse/suites/sinatra/sinatra_test.rb +75 -4
  53. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  54. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  55. data/test/new_relic/agent/agent_test.rb +20 -40
  56. data/test/new_relic/agent/agent_test_controller_test.rb +24 -19
  57. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  58. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  59. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  60. data/test/new_relic/agent/database_test.rb +60 -16
  61. data/test/new_relic/agent/error_collector_test.rb +28 -4
  62. data/test/new_relic/agent/event_listener_test.rb +23 -2
  63. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +53 -0
  64. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  65. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +414 -59
  66. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  67. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  68. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  69. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  70. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  71. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  72. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  73. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  74. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  75. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  76. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  77. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  78. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  79. data/test/new_relic/agent/stats_test.rb +197 -0
  80. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  81. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  82. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  83. data/test/new_relic/agent_test.rb +35 -5
  84. data/test/new_relic/control_test.rb +1 -1
  85. data/test/new_relic/fake_collector.rb +87 -9
  86. data/test/new_relic/helper_test.rb +24 -0
  87. data/test/new_relic/metric_data_test.rb +11 -11
  88. data/test/new_relic/metric_spec_test.rb +1 -1
  89. data/test/script/ci.sh +1 -1
  90. data/test/test_contexts.rb +0 -1
  91. data/test/test_helper.rb +21 -3
  92. metadata +32 -16
  93. metadata.gz.sig +0 -0
  94. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  95. data/lib/new_relic/stats.rb +0 -337
  96. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  97. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  98. data/test/new_relic/fakes_sending_data.rb +0 -30
  99. data/test/new_relic/stats_test.rb +0 -421
@@ -162,10 +162,10 @@ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
162
162
 
163
163
  def test_exclude_block
164
164
  @error_collector.ignore_error_filter &wrapped_filter_proc
165
-
165
+
166
166
  @error_collector.notice_error(IOError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
167
167
  @error_collector.notice_error(StandardError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
168
-
168
+
169
169
  errors = @error_collector.harvest_errors([])
170
170
 
171
171
  assert_equal 1, errors.length
@@ -192,6 +192,30 @@ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
192
192
  end
193
193
  end
194
194
 
195
+ def test_increment_error_count_record_summary_and_txn_metric
196
+ txn_info = NewRelic::Agent::TransactionInfo.new
197
+ txn_info.transaction_name = 'Controller/class/method'
198
+ NewRelic::Agent::TransactionInfo.stubs(:get).returns(txn_info)
199
+
200
+ stats_engine = NewRelic::Agent.instance.stats_engine
201
+ stats_engine.reset_stats
202
+ @error_collector.increment_error_count!(StandardError.new('Boo'))
203
+ assert_equal(1, stats_engine.get_stats('Errors/all').call_count,
204
+ 'Missing Errors/all metric')
205
+ assert_equal(1, stats_engine.get_stats('Errors/Controller/class/method').call_count,
206
+ 'Missing Errors/Controller/class/method metric')
207
+ end
208
+
209
+ def test_doesnt_increment_error_count_on_transaction_if_nameless
210
+ txn_info = NewRelic::Agent::TransactionInfo.new
211
+ NewRelic::Agent::TransactionInfo.stubs(:get).returns(txn_info)
212
+
213
+ stats_engine = NewRelic::Agent.instance.stats_engine
214
+ stats_engine.reset_stats
215
+ @error_collector.increment_error_count!(StandardError.new('Boo'))
216
+ assert_equal(0, stats_engine.get_stats('Errors/(unknown)').call_count)
217
+ end
218
+
195
219
  private
196
220
 
197
221
  def expects_error_count_increase(increase)
@@ -203,7 +227,7 @@ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
203
227
  def get_error_stats
204
228
  NewRelic::Agent.get_stats("Errors/all").call_count
205
229
  end
206
-
230
+
207
231
  def wrapped_filter_proc
208
232
  Proc.new do |e|
209
233
  if e.is_a? IOError
@@ -213,7 +237,7 @@ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
213
237
  end
214
238
  end
215
239
  end
216
-
240
+
217
241
  def silence_stream(*args)
218
242
  super
219
243
  rescue NoMethodError
@@ -14,6 +14,24 @@ class EventListenerTest < Test::Unit::TestCase
14
14
  end
15
15
  end
16
16
 
17
+
18
+ #
19
+ # Helpers
20
+ #
21
+
22
+ def assert_was_called
23
+ assert @called, "Event wasn't called"
24
+ end
25
+
26
+ def assert_was_not_called
27
+ assert !@called, "Event was called"
28
+ end
29
+
30
+
31
+ #
32
+ # Tests
33
+ #
34
+
17
35
  def test_notifies
18
36
  @events.subscribe(:before_call, &@check_method)
19
37
  @events.notify(:before_call, :env => "env")
@@ -37,9 +55,12 @@ class EventListenerTest < Test::Unit::TestCase
37
55
  @events.subscribe(:my_event) {}
38
56
  end
39
57
 
58
+ def test_clear
59
+ @events.subscribe(:after_call, &@check_method)
60
+ @events.clear
61
+ @events.notify(:after_call)
40
62
 
41
- def assert_was_called
42
- assert @called, "Event wasn't called"
63
+ assert_was_not_called
43
64
  end
44
65
 
45
66
  end
@@ -36,4 +36,57 @@ class NewRelic::Agent::Instrumentation::ControllerInstrumentationTest < Test::Un
36
36
  .with({:request => 'headers'}, start_time).raises("an error")
37
37
  assert_equal(start_time, @object.send(:_detect_upstream_wait, start_time))
38
38
  end
39
+
40
+ def test_transaction_name_calls_newrelic_metric_path
41
+ @object.stubs(:newrelic_metric_path).returns('some/wacky/path')
42
+ assert_equal('Controller/some/wacky/path', @object.send(:transaction_name))
43
+ end
44
+
45
+ def test_transaction_name_applies_category_and_path
46
+ assert_equal('Controller/metric/path',
47
+ @object.send(:transaction_name,
48
+ :category => :controller,
49
+ :path => 'metric/path'))
50
+ assert_equal('OtherTransaction/Background/metric/path',
51
+ @object.send(:transaction_name,
52
+ :category => :task, :path => 'metric/path'))
53
+ assert_equal('Controller/Rack/metric/path',
54
+ @object.send(:transaction_name,
55
+ :category => :rack, :path => 'metric/path'))
56
+ assert_equal('Controller/metric/path',
57
+ @object.send(:transaction_name,
58
+ :category => :uri, :path => 'metric/path'))
59
+ assert_equal('Controller/Sinatra/metric/path',
60
+ @object.send(:transaction_name,
61
+ :category => :sinatra,
62
+ :path => 'metric/path'))
63
+ assert_equal('Blarg/metric/path',
64
+ @object.send(:transaction_name,
65
+ :category => 'Blarg', :path => 'metric/path'))
66
+
67
+ end
68
+
69
+ def test_transaction_name_uses_class_name_if_path_not_specified
70
+ assert_equal('Controller/NewRelic::Agent::Instrumentation::ControllerInstrumentationTest::TestObject',
71
+ @object.send(:transaction_name, :category => :controller))
72
+ end
73
+
74
+ def test_transaction_name_applies_action_name_if_specified_and_not_path
75
+ assert_equal('Controller/NewRelic::Agent::Instrumentation::ControllerInstrumentationTest::TestObject/action',
76
+ @object.send(:transaction_name, :category => :controller,
77
+ :name => 'action'))
78
+ end
79
+
80
+ def test_transaction_name_applies_name_rules
81
+ rule = NewRelic::Agent::RulesEngine::Rule.new('match_expression' => '[0-9]+',
82
+ 'replacement' => '*',
83
+ 'replace_all' => true)
84
+ NewRelic::Agent.instance.transaction_rules << rule
85
+ assert_equal('foo/*/bar/*',
86
+ @object.send(:transaction_name, :category => 'foo',
87
+ :path => '1/bar/22'))
88
+ ensure
89
+ NewRelic::Agent.instance.instance_variable_set(:@transaction_rules,
90
+ NewRelic::Agent::RulesEngine.new)
91
+ end
39
92
  end
@@ -53,4 +53,99 @@ class NewRelic::Agent::Instrumentation::MetricFrameTest < Test::Unit::TestCase
53
53
  f.start = 1500
54
54
  assert_equal 500, f.queue_time
55
55
  end
56
+
57
+ def test_update_apdex_records_failed_when_specified
58
+ stats = NewRelic::Agent::Stats.new
59
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.1, true)
60
+ assert_equal 0, stats.apdex_s
61
+ assert_equal 0, stats.apdex_t
62
+ assert_equal 1, stats.apdex_f
63
+ end
64
+
65
+ def test_update_apdex_records_satisfying
66
+ stats = NewRelic::Agent::Stats.new
67
+ with_config(:apdex_t => 1) do
68
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.5, false)
69
+ end
70
+ assert_equal 1, stats.apdex_s
71
+ assert_equal 0, stats.apdex_t
72
+ assert_equal 0, stats.apdex_f
73
+ end
74
+
75
+ def test_update_apdex_records_tolerating
76
+ stats = NewRelic::Agent::Stats.new
77
+ with_config(:apdex_t => 1) do
78
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 1.5, false)
79
+ end
80
+ assert_equal 0, stats.apdex_s
81
+ assert_equal 1, stats.apdex_t
82
+ assert_equal 0, stats.apdex_f
83
+ end
84
+
85
+ def test_update_apdex_records_failing
86
+ stats = NewRelic::Agent::Stats.new
87
+ with_config(:apdex_t => 1) do
88
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 4.5, false)
89
+ end
90
+ assert_equal 0, stats.apdex_s
91
+ assert_equal 0, stats.apdex_t
92
+ assert_equal 1, stats.apdex_f
93
+ end
94
+
95
+ def test_update_apdex_records_correct_apdex_for_key_transaction
96
+ txn_info = NewRelic::Agent::TransactionInfo.get
97
+ stats = NewRelic::Agent::Stats.new
98
+ config = {
99
+ :web_transactions_apdex => {
100
+ 'Controller/slow/txn' => 4,
101
+ 'Controller/fast/txn' => 0.1,
102
+ },
103
+ :apdex => 1
104
+ }
105
+
106
+ txn_info.transaction_name = 'Controller/slow/txn'
107
+ with_config(config, :do_not_cast => true) do
108
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 3.5, false)
109
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 5.5, false)
110
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 16.5, false)
111
+ end
112
+ assert_equal 1, stats.apdex_s
113
+ assert_equal 1, stats.apdex_t
114
+ assert_equal 1, stats.apdex_f
115
+
116
+ txn_info.transaction_name = 'Controller/fast/txn'
117
+ with_config(config, :do_not_cast => true) do
118
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.05, false)
119
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.2, false)
120
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.5, false)
121
+ end
122
+ assert_equal 2, stats.apdex_s
123
+ assert_equal 2, stats.apdex_t
124
+ assert_equal 2, stats.apdex_f
125
+
126
+ txn_info.transaction_name = 'Controller/other/txn'
127
+ with_config(config, :do_not_cast => true) do
128
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 0.5, false)
129
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 2, false)
130
+ NewRelic::Agent::Instrumentation::MetricFrame.update_apdex(stats, 5, false)
131
+ end
132
+ assert_equal 3, stats.apdex_s
133
+ assert_equal 3, stats.apdex_t
134
+ assert_equal 3, stats.apdex_f
135
+ end
136
+
137
+ def test_record_apdex_stores_apdex_t_in_min_and_max
138
+ stats_engine = NewRelic::Agent.instance.stats_engine
139
+ stats_engine.reset_stats
140
+ metric = stub(:apdex_metric_path => 'Apdex/Controller/some/txn')
141
+ NewRelic::Agent.instance.instance_variable_set(:@stats_engine, stats_engine)
142
+
143
+ with_config(:apdex_t => 2.5) do
144
+ NewRelic::Agent::Instrumentation::MetricFrame.record_apdex(metric, 1, 1, false)
145
+ end
146
+ assert_equal 2.5, stats_engine.lookup_stats('Apdex').min_call_time
147
+ assert_equal 2.5, stats_engine.lookup_stats('Apdex').max_call_time
148
+ assert_equal 2.5, stats_engine.lookup_stats('Apdex/Controller/some/txn').min_call_time
149
+ assert_equal 2.5, stats_engine.lookup_stats('Apdex/Controller/some/txn').max_call_time
150
+ end
56
151
  end
@@ -1,84 +1,439 @@
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
+
35
+ class NewRelic::Agent::Instrumentation::NetInstrumentationTest < Test::Unit::TestCase
36
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation,
37
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions,
38
+ NewRelic::Agent::CrossAppTracing
39
+
40
+ CANNED_RESPONSE = Net::HTTPOK.new( '1.1', '200', 'OK' )
41
+ CANNED_RESPONSE.body =
42
+ '<html><head><title>Canned Response</title></head><body>Canned response.</body></html>'
43
+ CANNED_RESPONSE['content-type'] = 'text/html; charset=UTF-8'
44
+ CANNED_RESPONSE['date'] = 'Tue, 29 Jan 2013 21:52:04 GMT'
45
+ CANNED_RESPONSE['expires'] = '-1'
46
+ CANNED_RESPONSE['server'] = 'gws'
47
+ CANNED_RESPONSE.freeze
48
+
49
+ TRANSACTION_GUID = 'BEC1BC64675138B9'
50
+
51
+
52
+ def setup
53
+ NewRelic::Agent.manual_start(
54
+ :"cross_application_tracer.enabled" => false,
55
+ :"transaction_tracer.enabled" => true,
56
+ :cross_process_id => '269975#22824',
57
+ :encoding_key => 'gringletoes'
58
+ )
59
+
60
+ # $stderr.puts '', '---', ''
61
+ # NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new( {:log_level => 'debug'}, '', Logger.new($stderr) )
62
+
63
+ @engine = NewRelic::Agent.instance.stats_engine
64
+ @engine.clear_stats
65
+
66
+ @engine.start_transaction( 'test' )
67
+ NewRelic::Agent::TransactionInfo.get.guid = TRANSACTION_GUID
68
+
69
+ @sampler = NewRelic::Agent.instance.transaction_sampler
70
+ @sampler.notice_first_scope_push( Time.now.to_f )
71
+ @sampler.notice_transaction( '/path', '/path', {} )
72
+ @sampler.notice_push_scope "Controller/sandwiches/index"
73
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
74
+ @sampler.notice_push_scope "ab"
75
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
76
+ @sampler.notice_push_scope "fetch_external_service"
77
+
78
+ @response = CANNED_RESPONSE.clone
79
+ @socket = fixture_tcp_socket( @response )
80
+ end
81
+
82
+ def teardown
83
+ NewRelic::Agent.instance.transaction_sampler.reset!
84
+ Thread::current[:newrelic_scope_stack] = nil
85
+ NewRelic::Agent.instance.stats_engine.end_transaction
86
+ end
87
+
88
+
89
+ #
90
+ # Helpers
91
+ #
92
+
93
+ def fixture_tcp_socket( response )
94
+
95
+ # Don't actually talk to Google.
96
+ socket = stub("socket") do
97
+ stubs(:closed?).returns(false)
98
+ stubs(:close)
99
+
100
+ # Simulate a bunch of socket-ey stuff since Mocha doesn't really
101
+ # provide any other way to do it
102
+ class << self
103
+ attr_accessor :response, :write_checker
104
+ end
105
+
106
+ def self.check_write
107
+ self.write_checker = Proc.new
108
+ end
109
+
110
+ def self.write( buf )
111
+ self.write_checker.call( buf ) if self.write_checker
112
+ buf.length
113
+ end
114
+
115
+ def self.sysread( size, buf='' )
116
+ @data ||= response.to_s
117
+ raise EOFError if @data.empty?
118
+ buf.replace @data.slice!( 0, size )
119
+ buf
120
+ end
121
+ class << self
122
+ alias_method :read_nonblock, :sysread
123
+ end
11
124
 
12
- def metrics_without_gc
13
- @engine.metrics - ['GC/cumulative']
14
125
  end
15
126
 
16
- private :metrics_without_gc
127
+ socket.response = response
128
+ TCPSocket.stubs( :open ).returns( socket )
129
+
130
+ return socket
131
+ end
132
+
133
+
134
+ def make_app_data_payload( *args )
135
+ return obfuscate_with_key( 'gringletoes', args.to_json ).gsub( /\n/, '' ) + "\n"
136
+ end
137
+
138
+
139
+ def find_last_segment
140
+ builder = NewRelic::Agent.agent.transaction_sampler.builder
141
+ last_segment = nil
142
+ builder.current_segment.each_segment {|s| last_segment = s }
17
143
 
18
- def test_get
144
+ return last_segment
145
+ end
146
+
147
+
148
+
149
+ #
150
+ # Tests
151
+ #
152
+
153
+ def test_get
154
+ url = URI.parse('http://www.google.com/index.html')
155
+ res = Net::HTTP.start(url.host, url.port) {|http|
156
+ http.get('/index.html')
157
+ }
158
+
159
+ assert_match %r/<head>/i, res.body
160
+ assert_includes @engine.metrics, 'External/all'
161
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
162
+ assert_includes @engine.metrics, 'External/allOther'
163
+ assert_includes @engine.metrics, 'External/www.google.com/all'
164
+
165
+ assert_not_includes @engine.metrics, 'External/allWeb'
166
+ end
167
+
168
+ def test_background
169
+ res = nil
170
+
171
+ perform_action_with_newrelic_trace("task", :category => :task) do
19
172
  url = URI.parse('http://www.google.com/index.html')
20
173
  res = Net::HTTP.start(url.host, url.port) {|http|
21
174
  http.get('/index.html')
22
175
  }
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
176
  end
39
177
 
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
178
+ assert_match %r/<head>/i, res.body
179
+
180
+ assert_includes @engine.metrics, 'External/all'
181
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
182
+ assert_includes @engine.metrics, 'External/allOther'
183
+ assert_includes @engine.metrics, 'External/www.google.com/all'
184
+ assert_includes @engine.metrics,
185
+ 'OtherTransaction/Background/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task'
186
+
187
+ assert_not_includes @engine.metrics, 'External/allWeb'
188
+ end
189
+
190
+ def test_transactional
191
+ res = nil
192
+
193
+ perform_action_with_newrelic_trace("task") do
66
194
  url = URI.parse('http://www.google.com/index.html')
67
195
  res = Net::HTTP.start(url.host, url.port) {|http|
68
- http.head('/index.html')
196
+ http.get('/index.html')
69
197
  }
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
198
  end
73
199
 
74
- def test_post
200
+ assert_match %r/<head>/i, res.body
201
+
202
+ assert_includes @engine.metrics, 'External/all'
203
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
204
+ assert_includes @engine.metrics, 'External/allWeb'
205
+ assert_includes @engine.metrics, 'External/www.google.com/all'
206
+ assert_includes @engine.metrics,
207
+ 'Controller/NewRelic::Agent::Instrumentation::NetInstrumentationTest/task'
208
+
209
+ assert_not_includes @engine.metrics, 'External/allOther'
210
+ end
211
+
212
+ def test_get__simple
213
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
214
+
215
+ assert_includes @engine.metrics, 'External/all'
216
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
217
+ assert_includes @engine.metrics, 'External/allOther'
218
+ assert_includes @engine.metrics, 'External/www.google.com/all'
219
+
220
+ assert_not_includes @engine.metrics, 'External/allWeb'
221
+ end
222
+
223
+ def test_ignore
224
+ NewRelic::Agent.disable_all_tracing do
75
225
  url = URI.parse('http://www.google.com/index.html')
76
226
  res = Net::HTTP.start(url.host, url.port) {|http|
77
227
  http.post('/index.html','data')
78
228
  }
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
229
  end
82
230
 
231
+ assert_not_includes @engine.metrics, 'External/all'
232
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
233
+ assert_not_includes @engine.metrics, 'External/allOther'
234
+ assert_not_includes @engine.metrics, 'External/www.google.com/all'
235
+
236
+ assert_not_includes @engine.metrics, 'External/allWeb'
237
+ end
238
+
239
+ def test_head
240
+ url = URI.parse('http://www.google.com/index.html')
241
+ res = Net::HTTP.start(url.host, url.port) {|http|
242
+ http.head('/index.html')
243
+ }
244
+
245
+ assert_includes @engine.metrics, 'External/all'
246
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/HEAD'
247
+ assert_includes @engine.metrics, 'External/allOther'
248
+ assert_includes @engine.metrics, 'External/www.google.com/all'
249
+
250
+ assert_not_includes @engine.metrics, 'External/allWeb'
251
+ end
252
+
253
+ def test_post
254
+ url = URI.parse('http://www.google.com/index.html')
255
+ res = Net::HTTP.start(url.host, url.port) {|http|
256
+ http.post('/index.html','data')
257
+ }
258
+
259
+ assert_includes @engine.metrics, 'External/all'
260
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/POST'
261
+ assert_includes @engine.metrics, 'External/allOther'
262
+ assert_includes @engine.metrics, 'External/www.google.com/all'
263
+
264
+ assert_not_includes @engine.metrics, 'External/allWeb'
83
265
  end
266
+
267
+ # When an http call is made, the agent should add a request header named
268
+ # X-NewRelic-ID with a value equal to the encoded cross_app_id.
269
+
270
+ def test_adds_a_request_header_to_outgoing_requests_if_xp_enabled
271
+ @socket.check_write do |data|
272
+ assert_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
273
+ end
274
+
275
+ with_config(:"cross_application_tracer.enabled" => true) do
276
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
277
+ end
278
+ end
279
+
280
+ def test_adds_a_request_header_to_outgoing_requests_if_old_xp_config_is_present
281
+ @socket.check_write do |data|
282
+ assert_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
283
+ end
284
+
285
+ with_config(:cross_application_tracing => true) do
286
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
287
+ end
288
+ end
289
+
290
+ def test_agent_doesnt_add_a_request_header_to_outgoing_requests_if_xp_disabled
291
+ @socket.check_write do |data|
292
+ assert_no_match /(?i:x-newrelic-id): VURQV1BZRkZdXUFT/, data
293
+ end
294
+
295
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
296
+ end
297
+
298
+
299
+ def test_instrumentation_with_crossapp_enabled_records_normal_metrics_if_no_header_present
300
+ with_config(:"cross_application_tracer.enabled" => true) do
301
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
302
+ end
303
+
304
+ assert_equal 5, @engine.metrics.length
305
+
306
+ assert_includes @engine.metrics, 'External/all'
307
+ assert_includes @engine.metrics, 'External/allOther'
308
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
309
+ assert_includes @engine.metrics, 'External/www.google.com/all'
310
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
311
+
312
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
313
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
314
+ assert_not_includes @engine.metrics, 'External/allWeb'
315
+ end
316
+
317
+
318
+ def test_instrumentation_with_crossapp_disabled_records_normal_metrics_even_if_header_is_present
319
+ @response[ NR_APPDATA_HEADER ] =
320
+ make_app_data_payload( '18#1884', 'txn-name', 2, 8, 0, TRANSACTION_GUID )
321
+
322
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
323
+
324
+ assert_equal 5, @engine.metrics.length
325
+
326
+ assert_includes @engine.metrics, 'External/all'
327
+ assert_includes @engine.metrics, 'External/allOther'
328
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
329
+ assert_includes @engine.metrics, 'External/www.google.com/all'
330
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
331
+
332
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
333
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
334
+ assert_not_includes @engine.metrics, 'External/allWeb'
335
+ end
336
+
337
+
338
+ def test_instrumentation_with_crossapp_enabled_records_crossapp_metrics_if_header_present
339
+ @response[ NR_APPDATA_HEADER ] =
340
+ make_app_data_payload( '18#1884', 'txn-name', 2, 8, 0, TRANSACTION_GUID )
341
+
342
+ with_config(:"cross_application_tracer.enabled" => true) do
343
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
344
+ end
345
+
346
+ assert_equal 6, @engine.metrics.length
347
+
348
+ assert_includes @engine.metrics, 'External/all'
349
+ assert_includes @engine.metrics, 'External/allOther'
350
+ assert_includes @engine.metrics, 'ExternalApp/www.google.com/18#1884/all'
351
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name'
352
+ assert_includes @engine.metrics, 'External/www.google.com/all'
353
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/18#1884/txn-name:test'
354
+
355
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
356
+ assert_not_includes @engine.metrics, 'External/allWeb'
357
+
358
+ last_segment = find_last_segment()
359
+
360
+ assert_includes last_segment.params.keys, :transaction_guid
361
+ assert_equal TRANSACTION_GUID, last_segment.params[:transaction_guid]
362
+ end
363
+
364
+ def test_crossapp_metrics_allow_valid_utf8_characters
365
+ @response[ NR_APPDATA_HEADER ] =
366
+ make_app_data_payload( '12#1114', '世界線航跡蔵', 18.0, 88.1, 4096, TRANSACTION_GUID )
367
+
368
+ with_config(:"cross_application_tracer.enabled" => true) do
369
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
370
+ end
371
+
372
+ assert_equal 6, @engine.metrics.length
373
+
374
+ assert_includes @engine.metrics, 'External/all'
375
+ assert_includes @engine.metrics, 'External/allOther'
376
+ assert_includes @engine.metrics, 'ExternalApp/www.google.com/12#1114/all'
377
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/12#1114/世界線航跡蔵'
378
+ assert_includes @engine.metrics, 'External/www.google.com/all'
379
+ assert_includes @engine.metrics, 'ExternalTransaction/www.google.com/12#1114/世界線航跡蔵:test'
380
+
381
+ assert_not_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
382
+ assert_not_includes @engine.metrics, 'External/allWeb'
383
+
384
+ last_segment = find_last_segment()
385
+
386
+ assert_includes last_segment.params.keys, :transaction_guid
387
+ assert_equal TRANSACTION_GUID, last_segment.params[:transaction_guid]
388
+ end
389
+
390
+ def test_crossapp_metrics_ignores_crossapp_header_with_malformed_crossprocess_id
391
+ @response[ NR_APPDATA_HEADER ] =
392
+ make_app_data_payload( '88#88#88', 'invalid', 1, 2, 4096, TRANSACTION_GUID )
393
+
394
+ with_config(:"cross_application_tracer.enabled" => true) do
395
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
396
+ end
397
+
398
+ assert_equal 5, @engine.metrics.length
399
+
400
+ assert_includes @engine.metrics, 'External/all'
401
+ assert_includes @engine.metrics, 'External/allOther'
402
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET'
403
+ assert_includes @engine.metrics, 'External/www.google.com/all'
404
+ assert_includes @engine.metrics, 'External/www.google.com/Net::HTTP/GET:test'
405
+
406
+ assert_not_includes @engine.metrics, 'ExternalApp/www.google.com/88#88#88/all'
407
+ assert_not_includes @engine.metrics, 'ExternalTransaction/www.google.com/88#88#88/invalid'
408
+ assert_not_includes @engine.metrics, 'External/allWeb'
409
+ end
410
+
411
+ def test_doesnt_affect_the_request_if_an_exception_is_raised_while_setting_up_tracing
412
+ res = nil
413
+ NewRelic::Agent.instance.stats_engine.stubs( :push_scope ).
414
+ raises( NoMethodError, "undefined method `push_scope'" )
415
+
416
+ with_config(:"cross_application_tracer.enabled" => true) do
417
+ assert_nothing_raised do
418
+ res = Net::HTTP.get URI.parse('http://www.google.com/index.html')
419
+ end
420
+ end
421
+
422
+ assert_equal res, CANNED_RESPONSE.instance_variable_get( :@body )
423
+ end
424
+
425
+ def test_doesnt_affect_the_request_if_an_exception_is_raised_while_finishing_tracing
426
+ res = nil
427
+ NewRelic::Agent.instance.stats_engine.stubs( :pop_scope ).
428
+ raises( NoMethodError, "undefined method `pop_scope'" )
429
+
430
+ with_config(:"cross_application_tracer.enabled" => true) do
431
+ assert_nothing_raised do
432
+ res = Net::HTTP.get URI.parse('http://www.google.com/index.html')
433
+ end
434
+ end
435
+
436
+ assert_equal res, CANNED_RESPONSE.instance_variable_get( :@body )
437
+ end
438
+
84
439
  end