techarch-newrelic_rpm 2.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +335 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +159 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/init.rb +38 -0
  10. data/install.rb +45 -0
  11. data/lib/new_relic/agent.rb +281 -0
  12. data/lib/new_relic/agent/agent.rb +636 -0
  13. data/lib/new_relic/agent/chained_call.rb +13 -0
  14. data/lib/new_relic/agent/collection_helper.rb +66 -0
  15. data/lib/new_relic/agent/error_collector.rb +121 -0
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  17. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +88 -0
  18. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  19. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +437 -0
  20. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  21. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +167 -0
  22. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  23. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  24. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  26. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  27. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  28. data/lib/new_relic/agent/instrumentation/rack.rb +108 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  31. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +41 -0
  32. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  34. data/lib/new_relic/agent/method_tracer.rb +349 -0
  35. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  36. data/lib/new_relic/agent/sampler.rb +12 -0
  37. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +138 -0
  39. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine.rb +22 -0
  42. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  43. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  44. data/lib/new_relic/agent/stats_engine/transactions.rb +152 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +310 -0
  46. data/lib/new_relic/agent/worker_loop.rb +118 -0
  47. data/lib/new_relic/commands/deployments.rb +145 -0
  48. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  49. data/lib/new_relic/control.rb +473 -0
  50. data/lib/new_relic/control/external.rb +13 -0
  51. data/lib/new_relic/control/merb.rb +22 -0
  52. data/lib/new_relic/control/rails.rb +145 -0
  53. data/lib/new_relic/control/ruby.rb +36 -0
  54. data/lib/new_relic/control/sinatra.rb +14 -0
  55. data/lib/new_relic/histogram.rb +89 -0
  56. data/lib/new_relic/local_environment.rb +328 -0
  57. data/lib/new_relic/merbtasks.rb +6 -0
  58. data/lib/new_relic/metric_data.rb +42 -0
  59. data/lib/new_relic/metric_parser.rb +124 -0
  60. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  61. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  62. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  63. data/lib/new_relic/metric_parser/controller.rb +54 -0
  64. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  65. data/lib/new_relic/metric_parser/errors.rb +6 -0
  66. data/lib/new_relic/metric_parser/external.rb +50 -0
  67. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +52 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/newrelic.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +25 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +361 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +666 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +313 -0
  84. data/lib/newrelic_rpm.rb +40 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +13 -0
  88. data/newrelic.yml +227 -0
  89. data/recipes/newrelic.rb +6 -0
  90. data/techarch-newrelic_rpm.gemspec +32 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +264 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +119 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +172 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +81 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +126 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +113 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +220 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +317 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +19 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +57 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +80 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +484 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +330 -0
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::DispatcherInstrumentationTest < Test::Unit::TestCase
4
+
5
+ class FunnyDispatcher
6
+ include NewRelic::Agent::Instrumentation::DispatcherInstrumentation
7
+ def newrelic_response_code; end
8
+ end
9
+ def setup
10
+ super
11
+ NewRelic::Agent.manual_start
12
+ NewRelic::Agent.instance.stats_engine.clear_stats
13
+ @instance_busy = NewRelic::Agent.agent.stats_engine.get_stats 'Instance/Busy'
14
+ @dispatch_stat = NewRelic::Agent.agent.stats_engine.get_stats 'HttpDispatcher'
15
+ @mongrel_queue_stat = NewRelic::Agent.agent.stats_engine.get_stats 'WebFrontend/Mongrel/Average Queue Time'
16
+ end
17
+
18
+ def test_normal_call
19
+ d = FunnyDispatcher.new
20
+ assert_equal 0, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
21
+ d.newrelic_dispatcher_start
22
+ sleep 1.0
23
+ assert_equal 1, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
24
+ d.newrelic_dispatcher_finish
25
+ assert_equal 0, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
26
+ assert_nil Thread.current[:newrelic_t0]
27
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
28
+
29
+ assert_equal 1, @instance_busy.call_count
30
+ assert_equal 1, @dispatch_stat.call_count
31
+ assert_equal 0, @mongrel_queue_stat.call_count
32
+ assert @dispatch_stat.total_call_time >= 1.0, "Total call time must be at least one second"
33
+ assert @instance_busy.total_call_time > 0.9 && @instance_busy.total_call_time <= 1.0, "instance busy = #{@instance_busy.inspect}"
34
+ end
35
+ def test_histogram
36
+ d = FunnyDispatcher.new
37
+ d.newrelic_dispatcher_start
38
+ d.newrelic_dispatcher_finish
39
+ bucket = NewRelic::Agent.instance.stats_engine.metrics.find { | m | m =~ /^Response Times/ }
40
+ assert_not_nil bucket
41
+ bucket_stats = NewRelic::Agent.instance.stats_engine.get_stats(bucket)
42
+ assert_equal 1, bucket_stats.call_count
43
+ end
44
+ def test_ignore_zero_counts
45
+ assert_equal 0, @instance_busy.call_count, "Problem with test--instance busy not starting off at zero."
46
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
47
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
48
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
49
+ assert_equal 0, @instance_busy.call_count
50
+ end
51
+ def test_recursive_call
52
+ d0 = FunnyDispatcher.new
53
+ d1 = FunnyDispatcher.new
54
+
55
+ assert_equal 0, @instance_busy.call_count, "Problem with test--instance busy not starting off at zero."
56
+
57
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
58
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
59
+ assert_equal 0, @instance_busy.call_count
60
+
61
+ assert_equal 0, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
62
+ d0.newrelic_dispatcher_start
63
+ assert_equal 1, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
64
+ d1.newrelic_dispatcher_start
65
+ assert_equal 1, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
66
+ sleep 1
67
+ d0.newrelic_dispatcher_finish
68
+ assert_equal 0, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
69
+ d1.newrelic_dispatcher_finish
70
+ assert_equal 0, NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.busy_count
71
+ assert_nil Thread.current[:newrelic_t0]
72
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.harvest_busy
73
+ assert_equal 1, @instance_busy.call_count
74
+ assert @instance_busy.total_call_time.between?(0.8, 1.1), "Should be about 100%: #{@instance_busy.total_call_time}"
75
+ end
76
+ end
@@ -0,0 +1,172 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'test/unit'
3
+
4
+ class FakeRequest
5
+ attr_reader :path
6
+
7
+ def initialize(path)
8
+ @path = path
9
+ end
10
+
11
+ def referer
12
+ "test_referer"
13
+ end
14
+ end
15
+
16
+ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @error_collector = NewRelic::Agent::ErrorCollector.new(nil)
20
+ end
21
+ def teardown
22
+
23
+ end
24
+ def test_empty
25
+ @error_collector.harvest_errors([])
26
+ @error_collector.notice_error(nil, nil, 'path', {:x => 'y'})
27
+ errors = @error_collector.harvest_errors([])
28
+
29
+ assert_equal errors.length, 1
30
+
31
+ err = errors.first
32
+ assert_equal '<no message>', err.message
33
+ assert_equal 'y', err.params[:request_params][:x]
34
+ assert_equal '', err.params[:request_uri]
35
+ assert_equal '', err.params[:request_referer]
36
+ assert_equal 'path', err.path
37
+ assert_equal '<no exception>', err.exception_class
38
+
39
+ end
40
+ def test_simple
41
+ @error_collector.notice_error(Exception.new("message"), FakeRequest.new('/myurl/'), 'path', {:x => 'y'})
42
+
43
+ old_errors = []
44
+ errors = @error_collector.harvest_errors(old_errors)
45
+
46
+ assert_equal errors.length, 1
47
+
48
+ err = errors.first
49
+ assert_equal 'message', err.message
50
+ assert_equal 'y', err.params[:request_params][:x]
51
+ assert err.params[:request_uri] == '/myurl/'
52
+ assert err.params[:request_referer] == "test_referer"
53
+ assert err.path == 'path'
54
+ assert err.exception_class == 'Exception'
55
+
56
+ # the collector should now return an empty array since nothing
57
+ # has been added since its last harvest
58
+ errors = @error_collector.harvest_errors(nil)
59
+ assert errors.length == 0
60
+ end
61
+
62
+ def test_long_message
63
+ #yes, times 500. it's a 5000 byte string. Assuming strings are
64
+ #still 1 byte / char.
65
+ @error_collector.notice_error(Exception.new("1234567890" * 500), FakeRequest.new('/myurl/'), 'path', {:x => 'y'})
66
+
67
+ old_errors = []
68
+ errors = @error_collector.harvest_errors(old_errors)
69
+
70
+ assert_equal errors.length, 1
71
+
72
+ err = errors.first
73
+ assert_equal 4096, err.message.length
74
+ assert_equal ('1234567890' * 500)[0..4095], err.message
75
+ end
76
+
77
+ def test_collect_failover
78
+ @error_collector.notice_error(Exception.new("message"), nil, 'first', {:x => 'y'})
79
+
80
+ errors = @error_collector.harvest_errors([])
81
+
82
+ @error_collector.notice_error(Exception.new("message"), nil, 'second', {:x => 'y'})
83
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
84
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
85
+
86
+ errors = @error_collector.harvest_errors(errors)
87
+
88
+ assert_equal 1, errors.length
89
+ assert_equal 'first', errors.first.path
90
+
91
+ # add two more
92
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
93
+ @error_collector.notice_error(Exception.new("message"), nil, 'last', {:x => 'y'})
94
+
95
+ errors = @error_collector.harvest_errors(nil)
96
+ assert_equal 5, errors.length
97
+ assert_equal 'second', errors.first.path
98
+ assert_equal 'last', errors.last.path
99
+
100
+ end
101
+
102
+ def test_queue_overflow
103
+
104
+ max_q_length = 20 # for some reason I can't read the constant in ErrorCollector
105
+
106
+ silence_stream(::STDERR) do
107
+ (max_q_length + 5).times do |n|
108
+ @error_collector.notice_error(Exception.new("exception #{n}"), nil, "path", {:x => n})
109
+ end
110
+ end
111
+
112
+ errors = @error_collector.harvest_errors([])
113
+ assert errors.length == max_q_length
114
+ errors.each_index do |i|
115
+ err = errors.shift
116
+ assert_equal i.to_s, err.params[:request_params][:x], err.params.inspect
117
+ end
118
+ end
119
+
120
+ # Why would anyone undef these methods?
121
+ class TestClass
122
+ undef to_s
123
+ undef inspect
124
+ end
125
+
126
+
127
+ def test_supported_param_types
128
+
129
+ types = [[1, '1'],
130
+ [1.1, '1.1'],
131
+ ['hi', 'hi'],
132
+ [:hi, :hi],
133
+ [Exception.new("test"), "#<Exception>"],
134
+ [TestClass.new, "#<NewRelic::Agent::ErrorCollectorTest::TestClass>"]
135
+ ]
136
+
137
+
138
+ types.each do |test|
139
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => test[0]})
140
+
141
+ assert_equal test[1], @error_collector.harvest_errors([])[0].params[:request_params][:x]
142
+ end
143
+ end
144
+
145
+
146
+ def test_exclude
147
+ @error_collector.ignore(["ActionController::RoutingError"])
148
+
149
+ @error_collector.notice_error(ActionController::RoutingError.new("message"), nil, 'path', {:x => 'y'})
150
+
151
+ errors = @error_collector.harvest_errors([])
152
+
153
+ assert_equal 0, errors.length
154
+ end
155
+
156
+ def test_exclude_block
157
+ @error_collector.ignore_error_filter do |e|
158
+ if e.is_a? ActionController::RoutingError
159
+ nil
160
+ else
161
+ e
162
+ end
163
+ end
164
+
165
+ @error_collector.notice_error(ActionController::RoutingError.new("message"), nil, 'path', {:x => 'y'})
166
+
167
+ errors = @error_collector.harvest_errors([])
168
+
169
+ assert_equal 0, errors.length
170
+ end
171
+
172
+ end
@@ -0,0 +1,340 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'new_relic/agent/mock_scope_listener'
3
+
4
+ class Module
5
+ def method_traced?(method_name, metric_name)
6
+ traced_method_prefix = _traced_method_name(method_name, metric_name)
7
+
8
+ method_defined? traced_method_prefix
9
+ end
10
+ end
11
+
12
+ class Insider
13
+ def initialize(stats_engine)
14
+ @stats_engine = stats_engine
15
+ end
16
+ def catcher(level=0)
17
+ thrower(level) if level>0
18
+ end
19
+ def thrower(level)
20
+ if level == 0
21
+ # don't use a real sampler because we can't instantiate one
22
+ # sampler = NewRelic::Agent::TransactionSampler.new(NewRelic::Agent.instance)
23
+ sampler = "<none>"
24
+ begin
25
+ @stats_engine.transaction_sampler = sampler
26
+ fail "This should not have worked."
27
+ rescue; end
28
+ else
29
+ thrower(level-1)
30
+ end
31
+ end
32
+ end
33
+
34
+ module NewRelic
35
+ module Agent
36
+ extend self
37
+ def module_method_to_be_traced (x, testcase)
38
+ testcase.assert x == "x"
39
+ testcase.assert testcase.stats_engine.peek_scope.name == "x"
40
+ end
41
+ end
42
+ end
43
+
44
+ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
45
+ attr_reader :stats_engine
46
+
47
+ def setup
48
+ super
49
+ NewRelic::Agent.manual_start
50
+ @stats_engine = NewRelic::Agent.instance.stats_engine
51
+ @stats_engine.clear_stats
52
+ @scope_listener = NewRelic::Agent::MockScopeListener.new
53
+ @old_sampler = NewRelic::Agent.instance.transaction_sampler
54
+ @stats_engine.transaction_sampler = @scope_listener
55
+ end
56
+
57
+ def teardown
58
+ @stats_engine.transaction_sampler = @old_sampler
59
+ @stats_engine.clear_stats
60
+ self.class.remove_method_tracer :method_to_be_traced, @metric_name if @metric_name
61
+ @metric_name = nil
62
+ super
63
+ end
64
+
65
+ def test_basic
66
+ metric = "hello"
67
+ t1 = Time.now
68
+ self.class.trace_execution_scoped metric do
69
+ sleep 1
70
+ assert metric == @stats_engine.peek_scope.name
71
+ end
72
+ elapsed = Time.now - t1
73
+
74
+ stats = @stats_engine.get_stats(metric)
75
+ check_time stats.total_call_time, elapsed
76
+ assert stats.call_count == 1
77
+ end
78
+
79
+ def test_basic__original_api
80
+ metric = "hello"
81
+ t1 = Time.now
82
+ self.class.trace_method_execution metric, true, true, true do
83
+ sleep 1
84
+ assert metric == @stats_engine.peek_scope.name
85
+ end
86
+ elapsed = Time.now - t1
87
+
88
+ stats = @stats_engine.get_stats(metric)
89
+ check_time stats.total_call_time, elapsed
90
+ assert stats.call_count == 1
91
+ end
92
+
93
+ METRIC = "metric"
94
+ def test_add_method_tracer
95
+ @metric_name = METRIC
96
+ self.class.add_method_tracer :method_to_be_traced, METRIC
97
+
98
+ t1 = Time.now
99
+ method_to_be_traced 1,2,3,true,METRIC
100
+ elapsed = Time.now - t1
101
+
102
+ stats = @stats_engine.get_stats(METRIC)
103
+ check_time stats.total_call_time, elapsed
104
+ assert stats.call_count == 1
105
+ end
106
+
107
+ def test_add_method_tracer__default
108
+ self.class.add_method_tracer :simple_method
109
+
110
+ simple_method
111
+
112
+ stats = @stats_engine.get_stats("Custom/#{self.class.name}/simple_method")
113
+ assert stats.call_count == 1
114
+
115
+ end
116
+ def test_add_method_tracer__reentry
117
+ self.class.add_method_tracer :simple_method
118
+ self.class.add_method_tracer :simple_method
119
+ self.class.add_method_tracer :simple_method
120
+
121
+ simple_method
122
+
123
+ stats = @stats_engine.get_stats("Custom/#{self.class.name}/simple_method")
124
+ assert stats.call_count == 1
125
+
126
+ end
127
+
128
+ def test_method_traced?
129
+ assert !self.class.method_traced?(:method_to_be_traced, METRIC)
130
+ self.class.add_method_tracer :method_to_be_traced, METRIC
131
+ assert self.class.method_traced?(:method_to_be_traced, METRIC)
132
+ end
133
+
134
+ def test_tt_only
135
+
136
+ assert_nil @scope_listener.scope["c2"]
137
+ self.class.add_method_tracer :method_c1, "c1", true
138
+
139
+ self.class.add_method_tracer :method_c2, "c2", :metric => false
140
+ self.class.add_method_tracer :method_c3, "c3", false
141
+
142
+ method_c1
143
+
144
+ assert_not_nil @stats_engine.lookup_stat("c1")
145
+ assert_nil @stats_engine.lookup_stat("c2")
146
+ assert_not_nil @stats_engine.lookup_stat("c3")
147
+
148
+ assert_not_nil @scope_listener.scope["c2"]
149
+ end
150
+
151
+ def test_nested_scope_tracer
152
+ Insider.add_method_tracer :catcher, "catcher", :push_scope => true
153
+ Insider.add_method_tracer :thrower, "thrower", :push_scope => true
154
+ sampler = NewRelic::Agent.instance.transaction_sampler
155
+ mock = Insider.new(@stats_engine)
156
+ mock.catcher(0)
157
+ mock.catcher(5)
158
+ stats = @stats_engine.get_stats("catcher")
159
+ assert_equal 2, stats.call_count
160
+ stats = @stats_engine.get_stats("thrower")
161
+ assert_equal 6, stats.call_count
162
+ sample = sampler.harvest
163
+ assert_not_nil sample
164
+ end
165
+
166
+ def test_add_same_tracer_twice
167
+ @metric_name = METRIC
168
+ self.class.add_method_tracer :method_to_be_traced, METRIC
169
+ self.class.add_method_tracer :method_to_be_traced, METRIC
170
+
171
+ t1 = Time.now
172
+ method_to_be_traced 1,2,3,true,METRIC
173
+ elapsed = Time.now - t1
174
+
175
+ stats = @stats_engine.get_stats(METRIC)
176
+ check_time stats.total_call_time, elapsed
177
+ assert stats.call_count == 1
178
+ end
179
+
180
+ def test_add_tracer_with_dynamic_metric
181
+ metric_code = '#{args[0]}.#{args[1]}'
182
+ @metric_name = metric_code
183
+ expected_metric = "1.2"
184
+ self.class.add_method_tracer :method_to_be_traced, metric_code
185
+
186
+ t1 = Time.now
187
+ method_to_be_traced 1,2,3,true,expected_metric
188
+ elapsed = Time.now - t1
189
+
190
+ stats = @stats_engine.get_stats(expected_metric)
191
+ check_time stats.total_call_time, elapsed
192
+ assert stats.call_count == 1
193
+ end
194
+
195
+ def test_trace_method_with_block
196
+ self.class.add_method_tracer :method_with_block, METRIC
197
+
198
+ t1 = Time.now
199
+ method_with_block(1,2,3,true,METRIC) do |scope|
200
+ assert scope == METRIC
201
+ end
202
+ elapsed = Time.now - t1
203
+
204
+ stats = @stats_engine.get_stats(METRIC)
205
+ check_time stats.total_call_time, elapsed
206
+ assert stats.call_count == 1
207
+ end
208
+
209
+ def test_trace_module_method
210
+ NewRelic::Agent.add_method_tracer :module_method_to_be_traced, '#{args[0]}'
211
+ NewRelic::Agent.module_method_to_be_traced "x", self
212
+ NewRelic::Agent.remove_method_tracer :module_method_to_be_traced, '#{args[0]}'
213
+ end
214
+
215
+ def test_remove
216
+ self.class.add_method_tracer :method_to_be_traced, METRIC
217
+ self.class.remove_method_tracer :method_to_be_traced, METRIC
218
+
219
+ t1 = Time.now
220
+ method_to_be_traced 1,2,3,false,METRIC
221
+ elapsed = Time.now - t1
222
+
223
+ stats = @stats_engine.get_stats(METRIC)
224
+ assert stats.call_count == 0
225
+ end
226
+
227
+ def self.static_method(x, testcase, is_traced)
228
+ testcase.assert x == "x"
229
+ testcase.assert((testcase.stats_engine.peek_scope.name == "x") == is_traced)
230
+ end
231
+
232
+ def trace_trace_static_method
233
+ self.add_method_tracer :static_method, '#{args[0]}'
234
+ self.class.static_method "x", self, true
235
+ self.remove_method_tracer :static_method, '#{args[0]}'
236
+ self.class.static_method "x", self, false
237
+ end
238
+
239
+ def test_multiple_metrics__scoped
240
+ metrics = %w[first second third]
241
+ self.class.trace_execution_scoped metrics do
242
+ sleep 0.05
243
+ end
244
+ elapsed = @stats_engine.get_stats('first').average_call_time
245
+ metrics.map{|name| @stats_engine.get_stats name}.each do | m |
246
+ assert_equal 1, m.call_count
247
+ assert_equal elapsed, m.total_call_time
248
+ end
249
+ end
250
+ def test_multiple_metrics__unscoped
251
+ metrics = %w[first second third]
252
+ self.class.trace_execution_unscoped metrics do
253
+ sleep 0.05
254
+ end
255
+ elapsed = @stats_engine.get_stats('first').average_call_time
256
+ metrics.map{|name| @stats_engine.get_stats name}.each do | m |
257
+ assert_equal 1, m.call_count
258
+ assert_equal elapsed, m.total_call_time
259
+ end
260
+ end
261
+ def test_exception
262
+ begin
263
+ metric = "hey"
264
+ self.class.trace_execution_scoped metric do
265
+ assert @stats_engine.peek_scope.name == metric
266
+ throw Exception.new
267
+ end
268
+
269
+ assert false # should never get here
270
+ rescue Exception
271
+ # make sure the scope gets popped
272
+ assert @stats_engine.peek_scope == nil
273
+ end
274
+
275
+ stats = @stats_engine.get_stats metric
276
+ assert stats.call_count == 1
277
+ end
278
+
279
+ def test_add_multiple_tracers
280
+ self.class.add_method_tracer :method_to_be_traced, 'X', :push_scope => false
281
+ method_to_be_traced 1,2,3,true,nil
282
+ self.class.add_method_tracer :method_to_be_traced, 'Y'
283
+ method_to_be_traced 1,2,3,true,'Y'
284
+ self.class.remove_method_tracer :method_to_be_traced, 'Y'
285
+ method_to_be_traced 1,2,3,true,nil
286
+ self.class.remove_method_tracer :method_to_be_traced, 'X'
287
+ method_to_be_traced 1,2,3,false,'X'
288
+ end
289
+
290
+ def trace_no_push_scope
291
+ self.class.add_method_tracer :method_to_be_traced, 'X', :push_scope => false
292
+ method_to_be_traced 1,2,3,true,nil
293
+ self.class.remove_method_tracer :method_to_be_traced, 'X'
294
+ method_to_be_traced 1,2,3,false,'X'
295
+ end
296
+
297
+ def check_time (t1, t2)
298
+ assert((t2-t1).abs < 0.01, "Times not close: #{t1} ~ #{t2}")
299
+ end
300
+
301
+ # =======================================================
302
+ # test methods to be traced
303
+ def method_to_be_traced(x, y, z, is_traced, expected_metric)
304
+ sleep 0.1
305
+ assert x == 1
306
+ assert y == 2
307
+ assert z == 3
308
+ scope_name = @stats_engine.peek_scope ? @stats_engine.peek_scope.name : nil
309
+ if is_traced
310
+ assert_equal expected_metric, scope_name
311
+ else
312
+ assert_not_equal expected_metric, scope_name
313
+ end
314
+ end
315
+
316
+ def method_with_block(x, y, z, is_traced, expected_metric, &block)
317
+ sleep 0.1
318
+ assert x == 1
319
+ assert y == 2
320
+ assert z == 3
321
+ block.call(@stats_engine.peek_scope.name)
322
+
323
+ scope_name = @stats_engine.peek_scope ? @stats_engine.peek_scope.name : nil
324
+ assert((expected_metric == scope_name) == is_traced)
325
+ end
326
+
327
+ def method_c1
328
+ method_c2
329
+ end
330
+
331
+ def method_c2
332
+ method_c3
333
+ end
334
+
335
+ def method_c3
336
+ end
337
+
338
+ def simple_method
339
+ end
340
+ end