honkster-newrelic_rpm 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/CHANGELOG +462 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +172 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +9 -0
  9. data/lib/new_relic/agent.rb +382 -0
  10. data/lib/new_relic/agent/agent.rb +741 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/error_collector.rb +131 -0
  14. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  16. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +409 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +58 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +319 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +98 -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/rails3/action_controller.rb +45 -0
  32. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  34. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  36. data/lib/new_relic/agent/method_tracer.rb +350 -0
  37. data/lib/new_relic/agent/sampler.rb +50 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  39. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  40. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  41. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  42. data/lib/new_relic/agent/shim_agent.rb +25 -0
  43. data/lib/new_relic/agent/stats_engine.rb +24 -0
  44. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  45. data/lib/new_relic/agent/stats_engine/samplers.rb +83 -0
  46. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  47. data/lib/new_relic/agent/transaction_sampler.rb +330 -0
  48. data/lib/new_relic/agent/worker_loop.rb +81 -0
  49. data/lib/new_relic/collection_helper.rb +71 -0
  50. data/lib/new_relic/command.rb +85 -0
  51. data/lib/new_relic/commands/deployments.rb +105 -0
  52. data/lib/new_relic/commands/install.rb +81 -0
  53. data/lib/new_relic/control.rb +203 -0
  54. data/lib/new_relic/control/configuration.rb +149 -0
  55. data/lib/new_relic/control/frameworks/external.rb +13 -0
  56. data/lib/new_relic/control/frameworks/merb.rb +24 -0
  57. data/lib/new_relic/control/frameworks/rails.rb +126 -0
  58. data/lib/new_relic/control/frameworks/rails3.rb +60 -0
  59. data/lib/new_relic/control/frameworks/ruby.rb +36 -0
  60. data/lib/new_relic/control/frameworks/sinatra.rb +18 -0
  61. data/lib/new_relic/control/instrumentation.rb +95 -0
  62. data/lib/new_relic/control/logging_methods.rb +74 -0
  63. data/lib/new_relic/control/profiling.rb +24 -0
  64. data/lib/new_relic/control/server_methods.rb +88 -0
  65. data/lib/new_relic/delayed_job_injection.rb +27 -0
  66. data/lib/new_relic/histogram.rb +91 -0
  67. data/lib/new_relic/local_environment.rb +333 -0
  68. data/lib/new_relic/merbtasks.rb +6 -0
  69. data/lib/new_relic/metric_data.rb +42 -0
  70. data/lib/new_relic/metric_parser.rb +136 -0
  71. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  72. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  73. data/lib/new_relic/metric_parser/active_record.rb +28 -0
  74. data/lib/new_relic/metric_parser/apdex.rb +88 -0
  75. data/lib/new_relic/metric_parser/controller.rb +62 -0
  76. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  77. data/lib/new_relic/metric_parser/errors.rb +6 -0
  78. data/lib/new_relic/metric_parser/external.rb +50 -0
  79. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  80. data/lib/new_relic/metric_parser/other_transaction.rb +36 -0
  81. data/lib/new_relic/metric_parser/view.rb +61 -0
  82. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  83. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  84. data/lib/new_relic/metric_spec.rb +67 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/developer_mode.rb +257 -0
  88. data/lib/new_relic/rack/metric_app.rb +64 -0
  89. data/lib/new_relic/rack/mongrel_rpm.ru +26 -0
  90. data/lib/new_relic/rack/newrelic.yml +27 -0
  91. data/lib/new_relic/rack_app.rb +6 -0
  92. data/lib/new_relic/recipes.rb +82 -0
  93. data/lib/new_relic/stats.rb +368 -0
  94. data/lib/new_relic/timer_lib.rb +27 -0
  95. data/lib/new_relic/transaction_analysis.rb +119 -0
  96. data/lib/new_relic/transaction_sample.rb +586 -0
  97. data/lib/new_relic/url_rule.rb +14 -0
  98. data/lib/new_relic/version.rb +55 -0
  99. data/lib/new_relic_api.rb +276 -0
  100. data/lib/newrelic_rpm.rb +49 -0
  101. data/lib/tasks/all.rb +4 -0
  102. data/lib/tasks/install.rake +7 -0
  103. data/lib/tasks/tests.rake +15 -0
  104. data/newrelic.yml +246 -0
  105. data/newrelic_rpm.gemspec +254 -0
  106. data/recipes/newrelic.rb +6 -0
  107. data/test/active_record_fixtures.rb +55 -0
  108. data/test/config/newrelic.yml +48 -0
  109. data/test/config/test_control.rb +36 -0
  110. data/test/new_relic/agent/active_record_instrumentation_test.rb +286 -0
  111. data/test/new_relic/agent/agent_controller_test.rb +294 -0
  112. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  113. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  114. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  115. data/test/new_relic/agent/error_collector_test.rb +163 -0
  116. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  117. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  118. data/test/new_relic/agent/metric_data_test.rb +53 -0
  119. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  120. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  121. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  122. data/test/new_relic/agent/rpm_agent_test.rb +142 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  124. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  125. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  126. data/test/new_relic/agent/task_instrumentation_test.rb +188 -0
  127. data/test/new_relic/agent/testable_agent.rb +13 -0
  128. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  129. data/test/new_relic/agent/transaction_sample_test.rb +192 -0
  130. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  131. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  132. data/test/new_relic/control_test.rb +127 -0
  133. data/test/new_relic/deployments_api_test.rb +69 -0
  134. data/test/new_relic/environment_test.rb +75 -0
  135. data/test/new_relic/metric_parser_test.rb +226 -0
  136. data/test/new_relic/metric_spec_test.rb +177 -0
  137. data/test/new_relic/rack/episodes_test.rb +318 -0
  138. data/test/new_relic/shim_agent_test.rb +9 -0
  139. data/test/new_relic/stats_test.rb +312 -0
  140. data/test/new_relic/version_number_test.rb +89 -0
  141. data/test/test_contexts.rb +28 -0
  142. data/test/test_helper.rb +72 -0
  143. data/ui/helpers/developer_mode_helper.rb +359 -0
  144. data/ui/helpers/google_pie_chart.rb +49 -0
  145. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  146. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  147. data/ui/views/newrelic/_sample.rhtml +20 -0
  148. data/ui/views/newrelic/_segment.rhtml +29 -0
  149. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  150. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  151. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  152. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  153. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  154. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  155. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  156. data/ui/views/newrelic/_table.rhtml +12 -0
  157. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  158. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  159. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  160. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  161. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  162. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  163. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  164. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  165. data/ui/views/newrelic/file/images/textmate.png +0 -0
  166. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  167. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  168. data/ui/views/newrelic/file/stylesheets/style.css +484 -0
  169. data/ui/views/newrelic/index.rhtml +59 -0
  170. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  171. data/ui/views/newrelic/show_sample.rhtml +79 -0
  172. data/ui/views/newrelic/show_source.rhtml +3 -0
  173. data/ui/views/newrelic/threads.rhtml +52 -0
  174. metadata +307 -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' if defined?(::ActiveRecord)
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::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 ] * 2000
21
+ assert_equal 1024, 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.inspect
119
+ assert_equal 0, clean_trace.grep(/trace/).size, clean_trace.inspect
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 if defined?(::ActiveRecord)
125
+ end
@@ -0,0 +1,163 @@
1
+ # Run faster standalone
2
+ ENV['SKIP_RAILS'] = 'true'
3
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
4
+
5
+ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ super
9
+ @error_collector = NewRelic::Agent::ErrorCollector.new
10
+ @error_collector.stubs(:enabled).returns(true)
11
+ end
12
+ def test_empty
13
+ @error_collector.harvest_errors([])
14
+ @error_collector.notice_error(nil, :metric=> 'path', :request_params => {:x => 'y'})
15
+ errors = @error_collector.harvest_errors([])
16
+
17
+ assert_equal 0, errors.length
18
+
19
+ @error_collector.notice_error('Some error message', :metric=> 'path', :request_params => {:x => 'y'})
20
+ errors = @error_collector.harvest_errors([])
21
+
22
+ err = errors.first
23
+ assert_equal 'Some error message', err.message
24
+ assert_equal 'y', err.params[:request_params][:x]
25
+ assert_equal '', err.params[:request_uri]
26
+ assert_equal '', err.params[:request_referer]
27
+ assert_equal 'path', err.path
28
+ assert_equal 'Error', err.exception_class
29
+
30
+ end
31
+ def test_simple
32
+ @error_collector.notice_error(Exception.new("message"), :uri => '/myurl/', :metric => 'path', :referer => 'test_referer', :request_params => {:x => 'y'})
33
+
34
+ old_errors = []
35
+ errors = @error_collector.harvest_errors(old_errors)
36
+
37
+ assert_equal errors.length, 1
38
+
39
+ err = errors.first
40
+ assert_equal 'message', err.message
41
+ assert_equal 'y', err.params[:request_params][:x]
42
+ assert err.params[:request_uri] == '/myurl/'
43
+ assert err.params[:request_referer] == "test_referer"
44
+ assert err.path == 'path'
45
+ assert err.exception_class == 'Exception'
46
+
47
+ # the collector should now return an empty array since nothing
48
+ # has been added since its last harvest
49
+ errors = @error_collector.harvest_errors(nil)
50
+ assert errors.length == 0
51
+ end
52
+
53
+ def test_long_message
54
+ #yes, times 500. it's a 5000 byte string. Assuming strings are
55
+ #still 1 byte / char.
56
+ @error_collector.notice_error(Exception.new("1234567890" * 500), :uri => '/myurl/', :metric => 'path', :request_params => {:x => 'y'})
57
+
58
+ old_errors = []
59
+ errors = @error_collector.harvest_errors(old_errors)
60
+
61
+ assert_equal errors.length, 1
62
+
63
+ err = errors.first
64
+ assert_equal 4096, err.message.length
65
+ assert_equal ('1234567890' * 500)[0..4095], err.message
66
+ end
67
+
68
+ def test_collect_failover
69
+ @error_collector.notice_error(Exception.new("message"), :metric => 'first', :request_params => {:x => 'y'})
70
+
71
+ errors = @error_collector.harvest_errors([])
72
+
73
+ @error_collector.notice_error(Exception.new("message"), :metric => 'second', :request_params => {:x => 'y'})
74
+ @error_collector.notice_error(Exception.new("message"), :metric => 'path', :request_params => {:x => 'y'})
75
+ @error_collector.notice_error(Exception.new("message"), :metric => 'path', :request_params => {:x => 'y'})
76
+
77
+ errors = @error_collector.harvest_errors(errors)
78
+
79
+ assert_equal 1, errors.length
80
+ assert_equal 'first', errors.first.path
81
+
82
+ # add two more
83
+ @error_collector.notice_error(Exception.new("message"), :metric => 'path', :request_params => {:x => 'y'})
84
+ @error_collector.notice_error(Exception.new("message"), :metric => 'last', :request_params => {:x => 'y'})
85
+
86
+ errors = @error_collector.harvest_errors(nil)
87
+ assert_equal 5, errors.length
88
+ assert_equal 'second', errors.first.path
89
+ assert_equal 'last', errors.last.path
90
+
91
+ end
92
+
93
+ def test_queue_overflow
94
+
95
+ max_q_length = 20 # for some reason I can't read the constant in ErrorCollector
96
+
97
+ silence_stream(::STDOUT) do
98
+ (max_q_length + 5).times do |n|
99
+ @error_collector.notice_error(Exception.new("exception #{n}"), :metric => "path", :request_params => {:x => n})
100
+ end
101
+ end
102
+
103
+ errors = @error_collector.harvest_errors([])
104
+ assert errors.length == max_q_length
105
+ errors.each_index do |i|
106
+ err = errors.shift
107
+ assert_equal i.to_s, err.params[:request_params][:x], err.params.inspect
108
+ end
109
+ end
110
+
111
+ # Why would anyone undef these methods?
112
+ class TestClass
113
+ undef to_s
114
+ undef inspect
115
+ end
116
+
117
+
118
+ def test_supported_param_types
119
+
120
+ types = [[1, '1'],
121
+ [1.1, '1.1'],
122
+ ['hi', 'hi'],
123
+ [:hi, :hi],
124
+ [Exception.new("test"), "#<Exception>"],
125
+ [TestClass.new, "#<NewRelic::Agent::ErrorCollectorTest::TestClass>"]
126
+ ]
127
+
128
+
129
+ types.each do |test|
130
+ @error_collector.notice_error(Exception.new("message"), :metric => 'path', :request_params => {:x => test[0]})
131
+
132
+ assert_equal test[1], @error_collector.harvest_errors([])[0].params[:request_params][:x]
133
+ end
134
+ end
135
+
136
+
137
+ def test_exclude
138
+ @error_collector.ignore(["IOError"])
139
+
140
+ @error_collector.notice_error(IOError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
141
+
142
+ errors = @error_collector.harvest_errors([])
143
+
144
+ assert_equal 0, errors.length
145
+ end
146
+
147
+ def test_exclude_block
148
+ @error_collector.ignore_error_filter do |e|
149
+ if e.is_a? IOError
150
+ nil
151
+ else
152
+ e
153
+ end
154
+ end
155
+
156
+ @error_collector.notice_error(IOError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
157
+
158
+ errors = @error_collector.harvest_errors([])
159
+
160
+ assert_equal 0, errors.length
161
+ end
162
+
163
+ end
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::MemcacheInstrumentationTest < Test::Unit::TestCase
4
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
5
+
6
+ # This implementation: http://seattlerb.rubyforge.org/memcache-client/
7
+ def using_memcache_client?
8
+ ::MemCache.method_defined? :cache_get
9
+ end
10
+
11
+ def setup
12
+ NewRelic::Agent.manual_start
13
+ @engine = NewRelic::Agent.instance.stats_engine
14
+
15
+ if using_memcache_client?
16
+ @cache = ::MemCache.new('localhost')
17
+ else
18
+ server = ::MemCache::Server.new('localhost')
19
+ @cache = ::MemCache.new(server)
20
+ end
21
+ @key = 'schluessel'
22
+ end
23
+
24
+ def _call_test_method_in_web_transaction(method, *args)
25
+ @engine.clear_stats
26
+ begin
27
+ perform_action_with_newrelic_trace(:name=>'action', :category => :controller) do
28
+ @cache.send(method.to_sym, *[@key, *args])
29
+ end
30
+ rescue ::MemCache::MemCacheError
31
+ # There's probably no memcached around
32
+ end
33
+ end
34
+
35
+ def _call_test_method_in_background_task(method, *args)
36
+ @engine.clear_stats
37
+ begin
38
+ perform_action_with_newrelic_trace(:name => 'bg_task', :category => :task) do
39
+ @cache.send(method.to_sym, *[@key, *args])
40
+ end
41
+ rescue ::MemCache::MemCacheError
42
+ # There's probably no memcached around
43
+ end
44
+ end
45
+
46
+ def test_reads__web
47
+ %w[get get_multi].each do |method|
48
+ if @cache.class.method_defined?(method)
49
+ _call_test_method_in_web_transaction(method)
50
+ compare_metrics ["MemCache/#{method}", "MemCache/allWeb", "MemCache/#{method}:Controller/NewRelic::Agent::MemcacheInstrumentationTest/action"],
51
+ @engine.metrics.select{|m| m =~ /^memcache.*/i}
52
+ end
53
+ end
54
+ end
55
+
56
+ def test_writes__web
57
+ %w[incr decr delete].each do |method|
58
+ if @cache.class.method_defined?(method)
59
+ _call_test_method_in_web_transaction(method)
60
+ expected_metrics = ["MemCache/#{method}", "MemCache/allWeb", "MemCache/#{method}:Controller/NewRelic::Agent::MemcacheInstrumentationTest/action"]
61
+ compare_metrics expected_metrics, @engine.metrics.select{|m| m =~ /^memcache.*/i}
62
+ end
63
+ end
64
+
65
+ %w[set add].each do |method|
66
+ if @cache.class.method_defined?(method)
67
+ expected_metrics = ["MemCache/#{method}", "MemCache/allWeb", "MemCache/#{method}:Controller/NewRelic::Agent::MemcacheInstrumentationTest/action"]
68
+ _call_test_method_in_web_transaction(method, 'value')
69
+ compare_metrics expected_metrics, @engine.metrics.select{|m| m =~ /^memcache.*/i}
70
+ end
71
+ end
72
+ end
73
+
74
+ def test_reads__background
75
+ %w[get get_multi].each do |method|
76
+ if @cache.class.method_defined?(method)
77
+ _call_test_method_in_background_task(method)
78
+ compare_metrics ["MemCache/#{method}", "MemCache/allOther", "MemCache/#{method}:OtherTransaction/Background/NewRelic::Agent::MemcacheInstrumentationTest/bg_task"],
79
+ @engine.metrics.select{|m| m =~ /^memcache.*/i}
80
+ end
81
+ end
82
+ end
83
+
84
+ def test_writes__background
85
+
86
+ %w[incr decr delete].each do |method|
87
+ expected_metrics = ["MemCache/#{method}", "MemCache/allOther", "MemCache/#{method}:OtherTransaction/Background/NewRelic::Agent::MemcacheInstrumentationTest/bg_task"]
88
+ if @cache.class.method_defined?(method)
89
+ _call_test_method_in_background_task(method)
90
+ compare_metrics expected_metrics, @engine.metrics.select{|m| m =~ /^memcache.*/i}
91
+ end
92
+ end
93
+
94
+ %w[set add].each do |method|
95
+ expected_metrics = ["MemCache/#{method}", "MemCache/allOther", "MemCache/#{method}:OtherTransaction/Background/NewRelic::Agent::MemcacheInstrumentationTest/bg_task"]
96
+ if @cache.class.method_defined?(method)
97
+ _call_test_method_in_background_task(method, 'value')
98
+ compare_metrics expected_metrics, @engine.metrics.select{|m| m =~ /^memcache.*/i}
99
+ end
100
+ end
101
+ end
102
+
103
+ end if defined? MemCache
@@ -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_in_delta t2, t1, 0.02
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