factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -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 +67 -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/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -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 +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +214 -0
  90. data/recipes/newrelic.rb +6 -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 +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -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/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -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 +88 -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 +177 -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 +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -0
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'ostruct'
3
+ require 'active_record_fixtures'
4
+ class NewRelic::Agent::CollectionHelperTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+ NewRelic::Agent.manual_start
9
+ end
10
+ def teardown
11
+ super
12
+ end
13
+
14
+ include NewRelic::Agent::CollectionHelper
15
+ def test_string
16
+ val = (('A'..'Z').to_a.join * 100).to_s
17
+ assert_equal val[0...256] + "...", normalize_params(val)
18
+ end
19
+ def test_array
20
+ new_array = normalize_params [ 1000 ] * 50
21
+ assert_equal 20, new_array.size
22
+ assert_equal '1000', new_array[0]
23
+ end
24
+ def test_boolean
25
+ np = normalize_params(NewRelic::Control.instance.settings)
26
+ assert_equal false, np['monitor_mode']
27
+ end
28
+ def test_string__singleton
29
+ val = "This String"
30
+ def val.hello; end
31
+ assert_equal "This String", normalize_params(val)
32
+ assert val.respond_to?(:hello)
33
+ assert !normalize_params(val).respond_to?(:hello)
34
+ end
35
+ class MyString < String; end
36
+ def test_kind_of_string
37
+ s = MyString.new "This is a string"
38
+ assert_equal "This is a string", s.to_s
39
+ assert_equal MyString, s.class
40
+ assert_equal String, s.to_s.class
41
+ params = normalize_params(:val => [s])
42
+ assert_equal String, params[:val][0].class
43
+ assert_equal String, flatten(s).class
44
+ assert_equal String, truncate(s, 2).class
45
+ end
46
+ def test_number
47
+ np = normalize_params({ 'one' => 1.0, 'two' => '2'})
48
+ end
49
+ def test_nil
50
+ np = normalize_params({ nil => 1.0, 'two' => nil})
51
+ assert_equal "1.0", np['']
52
+ assert_equal nil, np['two']
53
+ end
54
+ def test_hash
55
+ val = ('A'..'Z').to_a.join * 100
56
+ assert_equal Hash["ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF..." => (("0"*256) + "...")], normalize_params({ val => '0' * 512 })
57
+ end
58
+ class MyHash < Hash
59
+
60
+ end
61
+ # Test to ensure that hash subclasses are properly converted
62
+ def test_hash_subclass
63
+ h = MyHash.new
64
+ h[:mine] = 'mine'
65
+ custom_params = { :one => {:hash => { :a => :b}, :myhash => h }}
66
+ nh = normalize_params(custom_params)
67
+ myhash = custom_params[:one][:myhash]
68
+ assert_equal MyHash, myhash.class
69
+ myhash = nh[:one][:myhash]
70
+ assert_equal Hash, myhash.class
71
+ end
72
+
73
+ class MyEnumerable
74
+ include Enumerable
75
+
76
+ def each
77
+ yield "1"
78
+ end
79
+ end
80
+
81
+ def test_enumerable
82
+ e = MyEnumerable.new
83
+ custom_params = { :one => {:hash => { :a => :b}, :myenum => e }}
84
+ nh = normalize_params(custom_params)
85
+ myenum = nh[:one][:myenum]
86
+ assert_match /MyEnumerable/, myenum
87
+ end
88
+
89
+ def test_stringio
90
+ # Verify StringIO works like this normally:
91
+ s = StringIO.new "start" + ("foo bar bat " * 1000)
92
+ val = nil
93
+ s.each { | entry | val = entry; break }
94
+ assert_match /^startfoo bar/, val
95
+
96
+ # make sure stringios aren't affected by calling normalize_params:
97
+ s = StringIO.new "start" + ("foo bar bat " * 1000)
98
+ v = normalize_params({ :foo => s.string })
99
+ s.each { | entry | val = entry; break }
100
+ assert_match /^startfoo bar/, val
101
+ end
102
+
103
+ def test_object
104
+ assert_equal ["foo", '#<OpenStruct>'], normalize_params(['foo', OpenStruct.new('z'=>'q')])
105
+ end
106
+
107
+ def test_strip_backtrace
108
+ begin
109
+ ActiveRecordFixtures.setup
110
+ # ActiveRecordFixtures::Order.add_delay
111
+ ActiveRecordFixtures::Order.find 0
112
+ flunk "should throw"
113
+ rescue => e
114
+ #puts e
115
+ #puts e.backtrace.join("\n")
116
+ #puts "\n\n"
117
+ clean_trace = strip_nr_from_backtrace(e.backtrace)
118
+ assert_equal 0, clean_trace.grep(/newrelic_rpm/).size, clean_trace.grep(/newrelic_rpm/)
119
+ assert_equal 0, clean_trace.grep(/trace/).size, clean_trace.grep(/trace/)
120
+ assert_equal 3, clean_trace.grep(/find/).size, "should see three frames with 'find' in them (#{e}): \n#{clean_trace.join("\n")}"
121
+ ensure
122
+ ActiveRecordFixtures.teardown
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,173 @@
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
20
+ NewRelic::Agent.instance.stubs(:should_send_errors).returns(true)
21
+ end
22
+ def teardown
23
+
24
+ end
25
+ def test_empty
26
+ @error_collector.harvest_errors([])
27
+ @error_collector.notice_error(nil, nil, 'path', {:x => 'y'})
28
+ errors = @error_collector.harvest_errors([])
29
+
30
+ assert_equal errors.length, 1
31
+
32
+ err = errors.first
33
+ assert_equal '<no message>', err.message
34
+ assert_equal 'y', err.params[:request_params][:x]
35
+ assert_equal '', err.params[:request_uri]
36
+ assert_equal '', err.params[:request_referer]
37
+ assert_equal 'path', err.path
38
+ assert_equal '<no exception>', err.exception_class
39
+
40
+ end
41
+ def test_simple
42
+ @error_collector.notice_error(Exception.new("message"), FakeRequest.new('/myurl/'), 'path', {:x => 'y'})
43
+
44
+ old_errors = []
45
+ errors = @error_collector.harvest_errors(old_errors)
46
+
47
+ assert_equal errors.length, 1
48
+
49
+ err = errors.first
50
+ assert_equal 'message', err.message
51
+ assert_equal 'y', err.params[:request_params][:x]
52
+ assert err.params[:request_uri] == '/myurl/'
53
+ assert err.params[:request_referer] == "test_referer"
54
+ assert err.path == 'path'
55
+ assert err.exception_class == 'Exception'
56
+
57
+ # the collector should now return an empty array since nothing
58
+ # has been added since its last harvest
59
+ errors = @error_collector.harvest_errors(nil)
60
+ assert errors.length == 0
61
+ end
62
+
63
+ def test_long_message
64
+ #yes, times 500. it's a 5000 byte string. Assuming strings are
65
+ #still 1 byte / char.
66
+ @error_collector.notice_error(Exception.new("1234567890" * 500), FakeRequest.new('/myurl/'), 'path', {:x => 'y'})
67
+
68
+ old_errors = []
69
+ errors = @error_collector.harvest_errors(old_errors)
70
+
71
+ assert_equal errors.length, 1
72
+
73
+ err = errors.first
74
+ assert_equal 4096, err.message.length
75
+ assert_equal ('1234567890' * 500)[0..4095], err.message
76
+ end
77
+
78
+ def test_collect_failover
79
+ @error_collector.notice_error(Exception.new("message"), nil, 'first', {:x => 'y'})
80
+
81
+ errors = @error_collector.harvest_errors([])
82
+
83
+ @error_collector.notice_error(Exception.new("message"), nil, 'second', {:x => 'y'})
84
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
85
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
86
+
87
+ errors = @error_collector.harvest_errors(errors)
88
+
89
+ assert_equal 1, errors.length
90
+ assert_equal 'first', errors.first.path
91
+
92
+ # add two more
93
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => 'y'})
94
+ @error_collector.notice_error(Exception.new("message"), nil, 'last', {:x => 'y'})
95
+
96
+ errors = @error_collector.harvest_errors(nil)
97
+ assert_equal 5, errors.length
98
+ assert_equal 'second', errors.first.path
99
+ assert_equal 'last', errors.last.path
100
+
101
+ end
102
+
103
+ def test_queue_overflow
104
+
105
+ max_q_length = 20 # for some reason I can't read the constant in ErrorCollector
106
+
107
+ silence_stream(::STDERR) do
108
+ (max_q_length + 5).times do |n|
109
+ @error_collector.notice_error(Exception.new("exception #{n}"), nil, "path", {:x => n})
110
+ end
111
+ end
112
+
113
+ errors = @error_collector.harvest_errors([])
114
+ assert errors.length == max_q_length
115
+ errors.each_index do |i|
116
+ err = errors.shift
117
+ assert_equal i.to_s, err.params[:request_params][:x], err.params.inspect
118
+ end
119
+ end
120
+
121
+ # Why would anyone undef these methods?
122
+ class TestClass
123
+ undef to_s
124
+ undef inspect
125
+ end
126
+
127
+
128
+ def test_supported_param_types
129
+
130
+ types = [[1, '1'],
131
+ [1.1, '1.1'],
132
+ ['hi', 'hi'],
133
+ [:hi, :hi],
134
+ [Exception.new("test"), "#<Exception>"],
135
+ [TestClass.new, "#<NewRelic::Agent::ErrorCollectorTest::TestClass>"]
136
+ ]
137
+
138
+
139
+ types.each do |test|
140
+ @error_collector.notice_error(Exception.new("message"), nil, 'path', {:x => test[0]})
141
+
142
+ assert_equal test[1], @error_collector.harvest_errors([])[0].params[:request_params][:x]
143
+ end
144
+ end
145
+
146
+
147
+ def test_exclude
148
+ @error_collector.ignore(["ActionController::RoutingError"])
149
+
150
+ @error_collector.notice_error(ActionController::RoutingError.new("message"), nil, 'path', {:x => 'y'})
151
+
152
+ errors = @error_collector.harvest_errors([])
153
+
154
+ assert_equal 0, errors.length
155
+ end
156
+
157
+ def test_exclude_block
158
+ @error_collector.ignore_error_filter do |e|
159
+ if e.is_a? ActionController::RoutingError
160
+ nil
161
+ else
162
+ e
163
+ end
164
+ end
165
+
166
+ @error_collector.notice_error(ActionController::RoutingError.new("message"), nil, 'path', {:x => 'y'})
167
+
168
+ errors = @error_collector.harvest_errors([])
169
+
170
+ assert_equal 0, errors.length
171
+ end
172
+
173
+ 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