newrelic_rpm 3.5.2.17 → 3.5.3.24
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of newrelic_rpm might be problematic. Click here for more details.
- data/.travis.yml +3 -1
- data/CHANGELOG +39 -23
- data/GUIDELINES_FOR_CONTRIBUTING.md +23 -2
- data/Rakefile +32 -0
- data/lib/new_relic/agent.rb +1 -0
- data/lib/new_relic/agent/agent.rb +45 -6
- data/lib/new_relic/agent/browser_monitoring.rb +36 -20
- data/lib/new_relic/agent/busy_calculator.rb +12 -4
- data/lib/new_relic/agent/configuration/defaults.rb +6 -1
- data/lib/new_relic/agent/configuration/environment_source.rb +14 -0
- data/lib/new_relic/agent/instrumentation/sinatra.rb +14 -10
- data/lib/new_relic/agent/new_relic_service.rb +192 -34
- data/lib/new_relic/agent/pipe_channel_manager.rb +1 -2
- data/lib/new_relic/agent/pipe_service.rb +5 -1
- data/lib/new_relic/agent/samplers/memory_sampler.rb +1 -1
- data/lib/new_relic/agent/sql_sampler.rb +29 -10
- data/lib/new_relic/agent/stats_engine/metric_stats.rb +40 -0
- data/lib/new_relic/agent/stats_engine/samplers.rb +1 -2
- data/lib/new_relic/agent/thread.rb +27 -0
- data/lib/new_relic/agent/thread_profiler.rb +295 -0
- data/lib/new_relic/agent/worker_loop.rb +29 -15
- data/lib/new_relic/control/frameworks/rails.rb +4 -4
- data/lib/new_relic/control/frameworks/rails3.rb +1 -1
- data/lib/new_relic/helper.rb +3 -0
- data/lib/new_relic/metric_data.rb +10 -2
- data/lib/new_relic/noticed_error.rb +5 -0
- data/lib/new_relic/transaction_sample.rb +23 -13
- data/lib/new_relic/transaction_sample/segment.rb +13 -15
- data/lib/new_relic/version.rb +1 -1
- data/lib/tasks/tests.rake +5 -11
- data/test/multiverse/.gitignore +10 -0
- data/test/multiverse/README.md +90 -0
- data/test/multiverse/Rakefile +17 -0
- data/test/multiverse/lib/multiverse/color.rb +13 -0
- data/test/multiverse/lib/multiverse/envfile.rb +66 -0
- data/test/multiverse/lib/multiverse/environment.rb +16 -0
- data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
- data/test/multiverse/lib/multiverse/runner.rb +44 -0
- data/test/multiverse/lib/multiverse/suite.rb +162 -0
- data/test/multiverse/script/run_one +3 -0
- data/test/multiverse/script/runner +9 -0
- data/test/multiverse/suites/active_record/Envfile +13 -0
- data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
- data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
- data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
- data/test/multiverse/suites/agent_only/Envfile +3 -0
- data/test/multiverse/suites/agent_only/config/newrelic.yml +22 -0
- data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
- data/test/multiverse/suites/agent_only/marshaling_test.rb +109 -0
- data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
- data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
- data/test/multiverse/suites/agent_only/service_timeout_test.rb +29 -0
- data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
- data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
- data/test/multiverse/suites/agent_only/thread_profiling_test.rb +80 -0
- data/test/multiverse/suites/datamapper/Envfile +8 -0
- data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
- data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
- data/test/multiverse/suites/monitor_mode_false/Envfile +2 -0
- data/test/multiverse/suites/monitor_mode_false/config/newrelic.yml +25 -0
- data/test/multiverse/suites/monitor_mode_false/no_dns_resolv.rb +29 -0
- data/test/multiverse/suites/no_load/Envfile +2 -0
- data/test/multiverse/suites/no_load/config/newrelic.yml +23 -0
- data/test/multiverse/suites/no_load/start_up_test.rb +14 -0
- data/test/multiverse/suites/rails_3_error_tracing/Envfile +15 -0
- data/test/multiverse/suites/rails_3_error_tracing/config/newrelic.yml +165 -0
- data/test/multiverse/suites/rails_3_error_tracing/error_tracing_test.rb +236 -0
- data/test/multiverse/suites/rails_3_gc/Envfile +8 -0
- data/test/multiverse/suites/rails_3_gc/config/newrelic.yml +167 -0
- data/test/multiverse/suites/rails_3_gc/instrumentation_test.rb +92 -0
- data/test/multiverse/suites/rails_3_queue_time/Envfile +15 -0
- data/test/multiverse/suites/rails_3_queue_time/config/newrelic.yml +165 -0
- data/test/multiverse/suites/rails_3_queue_time/queue_time_test.rb +75 -0
- data/test/multiverse/suites/rails_3_views/.gitignore +3 -0
- data/test/multiverse/suites/rails_3_views/Envfile +16 -0
- data/test/multiverse/suites/rails_3_views/app/views/foos/_foo.html.haml +1 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/_a_partial.html.erb +1 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/_mid_partial.html.erb +1 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/_top_partial.html.erb +3 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/deep_partial.html.erb +3 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/haml_view.html.haml +6 -0
- data/test/multiverse/suites/rails_3_views/app/views/test/index.html.erb +4 -0
- data/test/multiverse/suites/rails_3_views/config/newrelic.yml +164 -0
- data/test/multiverse/suites/rails_3_views/view_instrumentation_test.rb +245 -0
- data/test/multiverse/suites/resque/Envfile +21 -0
- data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
- data/test/multiverse/suites/resque/dump.rdb +0 -0
- data/test/multiverse/suites/resque/instrumentation_test.rb +73 -0
- data/test/multiverse/suites/rum_auto_instrumentation/Envfile +4 -0
- data/test/multiverse/suites/rum_auto_instrumentation/config/newrelic.yml +24 -0
- data/test/multiverse/suites/rum_auto_instrumentation/problem_response.html +422 -0
- data/test/multiverse/suites/rum_auto_instrumentation/responses/worst_case_small.html +5000 -0
- data/test/multiverse/suites/rum_auto_instrumentation/sanity_test.rb +115 -0
- data/test/multiverse/suites/sinatra/Envfile +13 -0
- data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
- data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +76 -0
- data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
- data/test/multiverse/test/multiverse_test.rb +55 -0
- data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
- data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
- data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
- data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
- data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
- data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
- data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
- data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
- data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
- data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
- data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
- data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
- data/test/new_relic/agent/agent_test.rb +54 -2
- data/test/new_relic/agent/agent_test_controller.rb +1 -1
- data/test/new_relic/agent/agent_test_controller_test.rb +35 -5
- data/test/new_relic/agent/browser_monitoring_test.rb +8 -8
- data/test/new_relic/agent/configuration/environment_source_test.rb +16 -0
- data/test/new_relic/agent/method_tracer_test.rb +6 -6
- data/test/new_relic/agent/new_relic_service_test.rb +137 -20
- data/test/new_relic/agent/sql_sampler_test.rb +26 -0
- data/test/new_relic/agent/stats_engine/metric_stats_test.rb +1 -1
- data/test/new_relic/agent/stats_engine_test.rb +1 -0
- data/test/new_relic/agent/thread_profiler_test.rb +536 -0
- data/test/new_relic/agent/thread_test.rb +76 -0
- data/test/new_relic/agent/threaded_test.rb +65 -0
- data/test/new_relic/agent/transaction_sampler_test.rb +16 -13
- data/test/new_relic/agent/worker_loop_test.rb +20 -0
- data/test/new_relic/fake_collector.rb +103 -31
- data/test/new_relic/fake_service.rb +7 -1
- data/test/new_relic/metric_data_test.rb +45 -16
- data/test/new_relic/noticed_error_test.rb +14 -0
- data/test/new_relic/transaction_sample/segment_test.rb +23 -4
- data/test/new_relic/transaction_sample_test.rb +39 -0
- data/ui/views/layouts/newrelic_default.rhtml +1 -0
- data/ui/views/newrelic/threads.rhtml +2 -10
- metadata +88 -2
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
|
2
|
+
require 'new_relic/agent/thread'
|
3
|
+
|
4
|
+
class ThreadTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_sets_label
|
7
|
+
t = NewRelic::Agent::Thread.new("labelled") {}
|
8
|
+
assert_equal "labelled", t[:newrelic_label]
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_bucket_thread_as_agent_when_profiling
|
12
|
+
t = NewRelic::Agent::Thread.new("labelled") {}
|
13
|
+
assert_equal :agent, NewRelic::Agent::Thread.bucket_thread(t, true)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_bucket_thread_as_agent_when_not_profiling
|
17
|
+
t = NewRelic::Agent::Thread.new("labelled") {}
|
18
|
+
assert_equal :ignore, NewRelic::Agent::Thread.bucket_thread(t, false)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_bucket_thread_as_request
|
22
|
+
t = ::Thread.new {}
|
23
|
+
frame = NewRelic::Agent::Instrumentation::MetricFrame.new
|
24
|
+
frame.request = "has a request"
|
25
|
+
t[:newrelic_metric_frame] = frame
|
26
|
+
|
27
|
+
assert_equal :request, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_bucket_thread_as_background
|
31
|
+
t = ::Thread.new {}
|
32
|
+
frame = NewRelic::Agent::Instrumentation::MetricFrame.new
|
33
|
+
t[:newrelic_metric_frame] = frame
|
34
|
+
|
35
|
+
assert_equal :background, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_bucket_thread_as_other_if_nil_frame
|
39
|
+
t = ::Thread.new {}
|
40
|
+
t[:newrelic_metric_frame] = nil
|
41
|
+
|
42
|
+
assert_equal :other, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_bucket_thread_as_other
|
46
|
+
t = ::Thread.new {}
|
47
|
+
assert_equal :other, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_runs_block
|
51
|
+
called = false
|
52
|
+
|
53
|
+
t = NewRelic::Agent::Thread.new("labelled") { called = true }
|
54
|
+
t.join
|
55
|
+
|
56
|
+
assert called
|
57
|
+
end
|
58
|
+
|
59
|
+
TRACE = [
|
60
|
+
"/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:100:in `catch'",
|
61
|
+
"/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.5.3.452.dev/lib/new_relic/agent/agent.rb:200:in `start_worker_thread'",
|
62
|
+
"/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/thin-1.5.0/lib/thin/backends/base.rb:300:in `block (3 levels) in run'",
|
63
|
+
]
|
64
|
+
|
65
|
+
def test_scrubs_backtrace_when_not_profiling_agent_code
|
66
|
+
result = NewRelic::Agent::Thread.scrub_backtrace(stub(:backtrace => TRACE), false)
|
67
|
+
assert_equal [TRACE[0], TRACE[2]], result
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_doesnt_scrub_backtrace_when_profiling_agent_code
|
71
|
+
result = NewRelic::Agent::Thread.scrub_backtrace(stub(:backtrace => TRACE), true)
|
72
|
+
assert_equal TRACE, result
|
73
|
+
end
|
74
|
+
|
75
|
+
DONT_CARE = true
|
76
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class ThreadedTest < Test::Unit::TestCase
|
2
|
+
def setup
|
3
|
+
@original_thread_class = NewRelic::Agent::Thread
|
4
|
+
swap_thread_class(FakeThread)
|
5
|
+
end
|
6
|
+
|
7
|
+
def teardown
|
8
|
+
swap_thread_class(@original_thread_class)
|
9
|
+
@original_thread_class = nil
|
10
|
+
|
11
|
+
FakeThread.list.clear
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_test
|
15
|
+
# no-op to keep quiet....
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def swap_thread_class(klass)
|
21
|
+
NewRelic::Agent.send(:remove_const, "Thread") if NewRelic::Agent.const_defined?("Thread")
|
22
|
+
NewRelic::Agent.const_set("Thread", klass)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class FakeThread
|
27
|
+
@@list = []
|
28
|
+
|
29
|
+
def initialize(locals={}, &block)
|
30
|
+
@locals = locals
|
31
|
+
yield if block_given?
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.current
|
35
|
+
{}
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.list
|
39
|
+
@@list
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.bucket_thread(thread, _)
|
43
|
+
thread[:bucket]
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.scrub_backtrace(thread, _)
|
47
|
+
thread[:scrubbed_backtrace] || thread.backtrace
|
48
|
+
end
|
49
|
+
|
50
|
+
def key?(key)
|
51
|
+
@locals.key?(key)
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](key)
|
55
|
+
@locals[key]
|
56
|
+
end
|
57
|
+
|
58
|
+
def backtrace
|
59
|
+
@locals[:backtrace] || []
|
60
|
+
end
|
61
|
+
|
62
|
+
def join
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -730,27 +730,30 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
|
|
730
730
|
# sample traces, for example. It's unfortunate, but we can't
|
731
731
|
# reliably turn off GC on all versions of ruby under test
|
732
732
|
def test_harvest_slowest
|
733
|
-
run_sample_trace
|
734
|
-
run_sample_trace
|
735
|
-
|
736
|
-
run_sample_trace
|
737
|
-
run_sample_trace
|
733
|
+
run_sample_trace(0,0.1)
|
734
|
+
run_sample_trace(0,0.1)
|
735
|
+
# two second duration
|
736
|
+
run_sample_trace(0,2)
|
737
|
+
run_sample_trace(0,0.1)
|
738
|
+
run_sample_trace(0,0.1)
|
738
739
|
|
739
740
|
with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
|
741
|
+
|
740
742
|
slowest = @sampler.harvest(nil)[0]
|
741
743
|
first_duration = slowest.duration
|
742
|
-
assert((first_duration >=
|
743
|
-
"expected sample duration
|
744
|
+
assert((first_duration.round >= 2),
|
745
|
+
"expected sample duration = 2, but was: #{slowest.duration.inspect}")
|
744
746
|
|
745
|
-
|
747
|
+
# 1 second duration
|
748
|
+
run_sample_trace(0,1)
|
746
749
|
not_as_slow = @sampler.harvest(slowest)[0]
|
747
750
|
assert((not_as_slow == slowest), "Should re-harvest the same transaction since it should be slower than the new transaction - expected #{slowest.inspect} but got #{not_as_slow.inspect}")
|
748
751
|
|
749
|
-
run_sample_trace
|
752
|
+
run_sample_trace(0,10)
|
750
753
|
|
751
754
|
new_slowest = @sampler.harvest(slowest)[0]
|
752
755
|
assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
|
753
|
-
|
756
|
+
assert_equal(new_slowest.duration.round, 10, "Slowest duration must be = 10, but was: #{new_slowest.duration.inspect}")
|
754
757
|
end
|
755
758
|
end
|
756
759
|
|
@@ -935,8 +938,8 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
|
|
935
938
|
|
936
939
|
private
|
937
940
|
|
938
|
-
def run_sample_trace
|
939
|
-
@sampler.notice_first_scope_push
|
941
|
+
def run_sample_trace(start = Time.now.to_f, stop = nil)
|
942
|
+
@sampler.notice_first_scope_push start
|
940
943
|
@sampler.notice_transaction '/path', nil, {}
|
941
944
|
@sampler.notice_push_scope "a"
|
942
945
|
@sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
|
@@ -948,6 +951,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
|
|
948
951
|
@sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
|
949
952
|
@sampler.notice_pop_scope "ac"
|
950
953
|
@sampler.notice_pop_scope "a"
|
951
|
-
@sampler.notice_scope_empty
|
954
|
+
@sampler.notice_scope_empty stop || Time.now.to_f
|
952
955
|
end
|
953
956
|
end
|
@@ -19,6 +19,23 @@ class NewRelic::Agent::WorkerLoopTest < Test::Unit::TestCase
|
|
19
19
|
assert @x
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_with_duration
|
23
|
+
worker_loop = NewRelic::Agent::WorkerLoop.new(:duration => 0.1)
|
24
|
+
count = 0
|
25
|
+
worker_loop.run(0.04) do
|
26
|
+
count += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal 2, count
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_loop_limit
|
33
|
+
worker_loop = NewRelic::Agent::WorkerLoop.new(:limit => 2)
|
34
|
+
iterations = 0
|
35
|
+
worker_loop.run(0) { iterations += 1 }
|
36
|
+
assert_equal 2, iterations
|
37
|
+
end
|
38
|
+
|
22
39
|
def test_density
|
23
40
|
# This shows how the tasks stay aligned with the period and don't drift.
|
24
41
|
count = 0
|
@@ -33,6 +50,7 @@ class NewRelic::Agent::WorkerLoopTest < Test::Unit::TestCase
|
|
33
50
|
elapsed = Time.now - start
|
34
51
|
assert_in_delta 0.09, elapsed, 0.03
|
35
52
|
end
|
53
|
+
|
36
54
|
def test_task_error__standard
|
37
55
|
@logger.expects(:debug)
|
38
56
|
@logger.expects(:error)
|
@@ -45,6 +63,7 @@ class NewRelic::Agent::WorkerLoopTest < Test::Unit::TestCase
|
|
45
63
|
end
|
46
64
|
assert done
|
47
65
|
end
|
66
|
+
|
48
67
|
class BadBoy < StandardError; end
|
49
68
|
|
50
69
|
def test_task_error__exception
|
@@ -55,6 +74,7 @@ class NewRelic::Agent::WorkerLoopTest < Test::Unit::TestCase
|
|
55
74
|
raise BadBoy, "oops"
|
56
75
|
end
|
57
76
|
end
|
77
|
+
|
58
78
|
def test_task_error__server
|
59
79
|
@logger.expects(:error).never
|
60
80
|
@logger.expects(:debug).once
|
@@ -8,17 +8,18 @@ require 'ostruct'
|
|
8
8
|
module NewRelic
|
9
9
|
class FakeCollector
|
10
10
|
attr_accessor :agent_data, :mock
|
11
|
-
|
11
|
+
|
12
12
|
def initialize
|
13
13
|
@id_counter = 0
|
14
14
|
@base_expectations = {
|
15
|
-
'get_redirect_host' => 'localhost',
|
16
|
-
'connect' => {
|
17
|
-
'
|
18
|
-
'
|
19
|
-
'
|
20
|
-
'
|
21
|
-
'
|
15
|
+
'get_redirect_host' => [200, {'return_value' => 'localhost'}],
|
16
|
+
'connect' => [200, {'return_value' => {"agent_run_id" => agent_run_id}}],
|
17
|
+
'get_agent_commands' => [200, {'return_value' => []}],
|
18
|
+
'metric_data' => [200, {'return_value' => {'Some/Metric/Spec' => 1}}],
|
19
|
+
'sql_trace_data' => [200, {'return_value' => nil}],
|
20
|
+
'transaction_sample_data' => [200, {'return_value' => nil}],
|
21
|
+
'error_data' => [200, {'return_value' => nil}],
|
22
|
+
'shutdown' => [200, {'return_value' => nil}]
|
22
23
|
}
|
23
24
|
reset
|
24
25
|
end
|
@@ -26,7 +27,7 @@ module NewRelic
|
|
26
27
|
def agent_run_id
|
27
28
|
@id_counter += 1
|
28
29
|
end
|
29
|
-
|
30
|
+
|
30
31
|
def call(env)
|
31
32
|
req = ::Rack::Request.new(env)
|
32
33
|
res = ::Rack::Response.new
|
@@ -34,25 +35,57 @@ module NewRelic
|
|
34
35
|
if uri.path =~ /agent_listener\/\d+\/.+\/(\w+)/
|
35
36
|
method = $1
|
36
37
|
if @mock.keys.include? method
|
37
|
-
res.
|
38
|
+
res.status = @mock[method][0]
|
39
|
+
if json_format?(uri)
|
40
|
+
res.write JSON.dump(@mock[method][1])
|
41
|
+
else
|
42
|
+
res.write Marshal.dump(@mock[method][1])
|
43
|
+
end
|
38
44
|
else
|
39
45
|
res.status = 500
|
40
46
|
res.write "Method not found"
|
41
47
|
end
|
42
48
|
run_id = uri.query =~ /run_id=(\d+)/ ? $1 : nil
|
43
49
|
req.body.rewind
|
50
|
+
body = if json_format?(uri) && RUBY_VERSION >= '1.9'
|
51
|
+
require 'json'
|
52
|
+
JSON.load(req.body.read)
|
53
|
+
else
|
54
|
+
Marshal.load(req.body.read)
|
55
|
+
end
|
44
56
|
@agent_data << OpenStruct.new(:action => method,
|
45
|
-
:body =>
|
57
|
+
:body => body,
|
46
58
|
:run_id => run_id)
|
47
59
|
end
|
48
60
|
res.finish
|
49
61
|
end
|
50
62
|
|
63
|
+
def calls_for(method)
|
64
|
+
@agent_data. \
|
65
|
+
select { |d| d.action == method }. \
|
66
|
+
map { |d| d.body }
|
67
|
+
end
|
68
|
+
|
69
|
+
def json_format?(uri)
|
70
|
+
uri.query && uri.query.include?('marshal_format=json')
|
71
|
+
end
|
72
|
+
|
51
73
|
def run(port=30303)
|
52
|
-
if
|
74
|
+
return if @thread && @thread.alive?
|
75
|
+
serve_on_port(port) do
|
53
76
|
@thread = Thread.new do
|
54
|
-
::Rack::Handler::WEBrick.run(self,
|
77
|
+
::Rack::Handler::WEBrick.run(self,
|
78
|
+
:Port => port,
|
79
|
+
:Logger => WEBrick::Log.new("/dev/null"),
|
80
|
+
:AccessLog => [nil, nil])
|
55
81
|
end
|
82
|
+
@thread.abort_on_exception = true
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def serve_on_port(port)
|
87
|
+
if is_port_available?('127.0.0.1', port)
|
88
|
+
yield
|
56
89
|
loop do
|
57
90
|
break if !is_port_available?('127.0.0.1', port)
|
58
91
|
sleep 0.01
|
@@ -61,8 +94,10 @@ module NewRelic
|
|
61
94
|
end
|
62
95
|
|
63
96
|
def stop
|
97
|
+
return unless @thread.alive?
|
64
98
|
::Rack::Handler::WEBrick.shutdown
|
65
99
|
@thread.join
|
100
|
+
reset
|
66
101
|
end
|
67
102
|
|
68
103
|
def reset
|
@@ -70,7 +105,7 @@ module NewRelic
|
|
70
105
|
@id_counter = 0
|
71
106
|
@agent_data = []
|
72
107
|
end
|
73
|
-
|
108
|
+
|
74
109
|
def is_port_available?(ip, port)
|
75
110
|
begin
|
76
111
|
Timeout::timeout(1) do
|
@@ -84,16 +119,33 @@ module NewRelic
|
|
84
119
|
end
|
85
120
|
rescue Timeout::Error
|
86
121
|
end
|
87
|
-
|
122
|
+
|
88
123
|
return true
|
89
124
|
end
|
90
125
|
end
|
126
|
+
|
127
|
+
# might we need this? I'll just leave it here for now
|
128
|
+
class FakeCollectorProcess < FakeCollector
|
129
|
+
def run(port=30303)
|
130
|
+
serve_on_port(port) do
|
131
|
+
@pid = Process.fork do
|
132
|
+
::Rack::Handler::WEBrick.run(self, :Port => port)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def stop
|
138
|
+
return unless @pid
|
139
|
+
Process.kill('QUIT', @pid)
|
140
|
+
Process.wait(@pid)
|
141
|
+
end
|
142
|
+
end
|
91
143
|
end
|
92
144
|
|
93
145
|
if $0 == __FILE__
|
94
146
|
require 'test/unit'
|
95
147
|
require 'net/http'
|
96
|
-
|
148
|
+
|
97
149
|
class FakeCollectorTest < Test::Unit::TestCase
|
98
150
|
def setup
|
99
151
|
@collector = NewRelic::FakeCollector.new
|
@@ -103,18 +155,18 @@ if $0 == __FILE__
|
|
103
155
|
def teardown
|
104
156
|
@collector.stop
|
105
157
|
end
|
106
|
-
|
158
|
+
|
107
159
|
def test_get_redirect
|
108
|
-
@collector.mock['get_redirect_host'] = 'test.example.com'
|
160
|
+
@collector.mock['get_redirect_host'] = [200, 'test.example.com']
|
109
161
|
response = invoke('get_redirect_host')
|
110
|
-
|
162
|
+
|
111
163
|
assert_equal 'test.example.com', response
|
112
164
|
assert_equal 'get_redirect_host', @collector.agent_data[0].action
|
113
165
|
end
|
114
|
-
|
166
|
+
|
115
167
|
def test_connect
|
116
168
|
response = invoke('connect')
|
117
|
-
|
169
|
+
|
118
170
|
assert_equal 1, response['agent_run_id']
|
119
171
|
assert_equal 'connect', @collector.agent_data[0].action
|
120
172
|
end
|
@@ -122,7 +174,7 @@ if $0 == __FILE__
|
|
122
174
|
def test_metric_data
|
123
175
|
response = invoke('metric_data?run_id=1',
|
124
176
|
{'Foo/Bar' => [1,2,3], 'Baz/Cux' => [4,5,6]})
|
125
|
-
|
177
|
+
|
126
178
|
assert_equal 1, response['Some/Metric/Spec']
|
127
179
|
post = @collector.agent_data[0]
|
128
180
|
assert_equal 'metric_data', post.action
|
@@ -151,16 +203,27 @@ if $0 == __FILE__
|
|
151
203
|
assert_equal ['node', ['node', 'node'], 'node'], post.body
|
152
204
|
assert_equal 3, post.run_id.to_i
|
153
205
|
end
|
154
|
-
|
206
|
+
|
207
|
+
def test_transaction_sample_data_json
|
208
|
+
response = invoke('transaction_sample_data?run_id=3&marshal_format=json',
|
209
|
+
'["node",["node","node"],"node"]')
|
210
|
+
|
211
|
+
assert_equal '', response
|
212
|
+
post = @collector.agent_data[0]
|
213
|
+
assert_equal 'transaction_sample_data', post.action
|
214
|
+
assert_equal '["node",["node","node"],"node"]', post.body
|
215
|
+
assert_equal 3, post.run_id.to_i
|
216
|
+
end
|
217
|
+
|
155
218
|
def test_error_data
|
156
219
|
response = invoke('error_data?run_id=4', ['error'])
|
157
|
-
|
220
|
+
|
158
221
|
assert_nil response
|
159
222
|
post = @collector.agent_data[0]
|
160
223
|
assert_equal 'error_data', post.action
|
161
224
|
assert_equal ['error'], post.body
|
162
225
|
end
|
163
|
-
|
226
|
+
|
164
227
|
def test_shutdown
|
165
228
|
response = invoke('shutdown?run_id=1')
|
166
229
|
|
@@ -183,7 +246,7 @@ if $0 == __FILE__
|
|
183
246
|
invoke('shutdown?run_id=2')
|
184
247
|
|
185
248
|
Process.wait(pid)
|
186
|
-
|
249
|
+
|
187
250
|
expected = ['get_redirect_host', 'connect', 'metric_data',
|
188
251
|
'transaction_sample_data', 'shutdown',
|
189
252
|
'get_redirect_host', 'connect', 'metric_data',
|
@@ -192,19 +255,28 @@ if $0 == __FILE__
|
|
192
255
|
end
|
193
256
|
|
194
257
|
def test_reset
|
195
|
-
@collector.mock['get_redirect_host'] = 'never!'
|
258
|
+
@collector.mock['get_redirect_host'] = [200, 'never!']
|
196
259
|
@collector.reset
|
197
|
-
assert_equal 'localhost', @collector.mock['get_redirect_host']
|
260
|
+
assert_equal [200, 'localhost'], @collector.mock['get_redirect_host']
|
198
261
|
end
|
199
|
-
|
262
|
+
|
200
263
|
def invoke(method, post={}, code=200)
|
201
264
|
uri = URI.parse("http://127.0.0.1:30303/agent_listener/8/12345/#{method}")
|
202
265
|
request = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
|
203
|
-
|
266
|
+
if uri.query && uri.query.include?('marsha_format=json')
|
267
|
+
request.body = JSON.dump(post)
|
268
|
+
else
|
269
|
+
request.body = Marshal.dump(post)
|
270
|
+
end
|
204
271
|
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
205
272
|
http.request(request)
|
206
273
|
end
|
207
|
-
|
274
|
+
if uri.query && uri.query.include?('marshal_format=json')
|
275
|
+
JSON.load(response.body)
|
276
|
+
else
|
277
|
+
Marshal.load(response.body)
|
278
|
+
end
|
208
279
|
end
|
209
280
|
end
|
210
281
|
end
|
282
|
+
|