ghazel-newrelic_rpm 3.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/CHANGELOG +525 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +179 -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 +118 -0
  8. data/cert/oldsite.pem +28 -0
  9. data/cert/site.pem +27 -0
  10. data/install.rb +9 -0
  11. data/lib/conditional_vendored_dependency_detection.rb +3 -0
  12. data/lib/conditional_vendored_metric_parser.rb +5 -0
  13. data/lib/new_relic/agent.rb +423 -0
  14. data/lib/new_relic/agent/agent.rb +1040 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +61 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +83 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/error_collector.rb +203 -0
  20. data/lib/new_relic/agent/instrumentation.rb +9 -0
  21. data/lib/new_relic/agent/instrumentation/active_merchant.rb +21 -0
  22. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +62 -0
  23. data/lib/new_relic/agent/instrumentation/authlogic.rb +13 -0
  24. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +413 -0
  25. data/lib/new_relic/agent/instrumentation/data_mapper.rb +232 -0
  26. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +44 -0
  27. data/lib/new_relic/agent/instrumentation/memcache.rb +56 -0
  28. data/lib/new_relic/agent/instrumentation/merb/controller.rb +36 -0
  29. data/lib/new_relic/agent/instrumentation/merb/errors.rb +23 -0
  30. data/lib/new_relic/agent/instrumentation/metric_frame.rb +326 -0
  31. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +84 -0
  32. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  33. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +34 -0
  34. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  35. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  36. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +83 -0
  37. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +36 -0
  38. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +108 -0
  39. data/lib/new_relic/agent/instrumentation/rails/errors.rb +36 -0
  40. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +60 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +112 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +31 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +54 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +23 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +16 -0
  46. data/lib/new_relic/agent/method_tracer.rb +422 -0
  47. data/lib/new_relic/agent/sampler.rb +50 -0
  48. data/lib/new_relic/agent/samplers/cpu_sampler.rb +58 -0
  49. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +40 -0
  50. data/lib/new_relic/agent/samplers/memory_sampler.rb +144 -0
  51. data/lib/new_relic/agent/samplers/object_sampler.rb +26 -0
  52. data/lib/new_relic/agent/shim_agent.rb +29 -0
  53. data/lib/new_relic/agent/stats_engine.rb +24 -0
  54. data/lib/new_relic/agent/stats_engine/metric_stats.rb +182 -0
  55. data/lib/new_relic/agent/stats_engine/samplers.rb +89 -0
  56. data/lib/new_relic/agent/stats_engine/transactions.rb +184 -0
  57. data/lib/new_relic/agent/transaction_sample_builder.rb +101 -0
  58. data/lib/new_relic/agent/transaction_sampler.rb +378 -0
  59. data/lib/new_relic/agent/worker_loop.rb +81 -0
  60. data/lib/new_relic/collection_helper.rb +69 -0
  61. data/lib/new_relic/command.rb +85 -0
  62. data/lib/new_relic/commands/deployments.rb +105 -0
  63. data/lib/new_relic/commands/install.rb +91 -0
  64. data/lib/new_relic/control.rb +48 -0
  65. data/lib/new_relic/control/class_methods.rb +47 -0
  66. data/lib/new_relic/control/configuration.rb +157 -0
  67. data/lib/new_relic/control/frameworks/external.rb +16 -0
  68. data/lib/new_relic/control/frameworks/merb.rb +30 -0
  69. data/lib/new_relic/control/frameworks/rails.rb +151 -0
  70. data/lib/new_relic/control/frameworks/rails3.rb +70 -0
  71. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  72. data/lib/new_relic/control/frameworks/sinatra.rb +23 -0
  73. data/lib/new_relic/control/instance_methods.rb +154 -0
  74. data/lib/new_relic/control/instrumentation.rb +81 -0
  75. data/lib/new_relic/control/logging_methods.rb +88 -0
  76. data/lib/new_relic/control/profiling.rb +25 -0
  77. data/lib/new_relic/control/server_methods.rb +104 -0
  78. data/lib/new_relic/data_serialization.rb +84 -0
  79. data/lib/new_relic/delayed_job_injection.rb +38 -0
  80. data/lib/new_relic/histogram.rb +91 -0
  81. data/lib/new_relic/local_environment.rb +391 -0
  82. data/lib/new_relic/merbtasks.rb +6 -0
  83. data/lib/new_relic/metric_data.rb +46 -0
  84. data/lib/new_relic/metric_spec.rb +73 -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/browser_monitoring.rb +68 -0
  88. data/lib/new_relic/rack/developer_mode.rb +267 -0
  89. data/lib/new_relic/rack/metric_app.rb +65 -0
  90. data/lib/new_relic/rack/mongrel_rpm.ru +28 -0
  91. data/lib/new_relic/rack/newrelic.yml +27 -0
  92. data/lib/new_relic/rack_app.rb +6 -0
  93. data/lib/new_relic/recipes.rb +73 -0
  94. data/lib/new_relic/stats.rb +378 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +76 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +47 -0
  98. data/lib/new_relic/transaction_sample.rb +272 -0
  99. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  100. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  101. data/lib/new_relic/transaction_sample/segment.rb +251 -0
  102. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  103. data/lib/new_relic/url_rule.rb +14 -0
  104. data/lib/new_relic/version.rb +55 -0
  105. data/lib/newrelic_rpm.rb +49 -0
  106. data/lib/tasks/all.rb +4 -0
  107. data/lib/tasks/install.rake +7 -0
  108. data/lib/tasks/tests.rake +19 -0
  109. data/newrelic.yml +265 -0
  110. data/newrelic_rpm.gemspec +330 -0
  111. data/recipes/newrelic.rb +6 -0
  112. data/test/active_record_fixtures.rb +77 -0
  113. data/test/config/newrelic.yml +48 -0
  114. data/test/config/test_control.rb +48 -0
  115. data/test/new_relic/agent/agent/connect_test.rb +316 -0
  116. data/test/new_relic/agent/agent/start_test.rb +340 -0
  117. data/test/new_relic/agent/agent/start_worker_thread_test.rb +155 -0
  118. data/test/new_relic/agent/agent_test.rb +173 -0
  119. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  120. data/test/new_relic/agent/agent_test_controller_test.rb +366 -0
  121. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  122. data/test/new_relic/agent/beacon_configuration_test.rb +104 -0
  123. data/test/new_relic/agent/browser_monitoring_test.rb +274 -0
  124. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  125. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  126. data/test/new_relic/agent/error_collector_test.rb +168 -0
  127. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +522 -0
  128. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +36 -0
  129. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  130. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +179 -0
  131. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  132. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  133. data/test/new_relic/agent/instrumentation/queue_time_test.rb +387 -0
  134. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  135. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  136. data/test/new_relic/agent/memcache_instrumentation_test.rb +107 -0
  137. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  138. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  139. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  140. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  141. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  142. data/test/new_relic/agent/sampler_test.rb +19 -0
  143. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  144. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  145. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +81 -0
  146. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  147. data/test/new_relic/agent/stats_engine_test.rb +185 -0
  148. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  149. data/test/new_relic/agent/transaction_sampler_test.rb +945 -0
  150. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  151. data/test/new_relic/collection_helper_test.rb +127 -0
  152. data/test/new_relic/command/deployments_test.rb +68 -0
  153. data/test/new_relic/control/class_methods_test.rb +62 -0
  154. data/test/new_relic/control/logging_methods_test.rb +157 -0
  155. data/test/new_relic/control_test.rb +195 -0
  156. data/test/new_relic/data_serialization_test.rb +70 -0
  157. data/test/new_relic/local_environment_test.rb +72 -0
  158. data/test/new_relic/metric_data_test.rb +125 -0
  159. data/test/new_relic/metric_spec_test.rb +95 -0
  160. data/test/new_relic/rack/all_test.rb +11 -0
  161. data/test/new_relic/rack/browser_monitoring_test.rb +84 -0
  162. data/test/new_relic/rack/developer_mode_test.rb +43 -0
  163. data/test/new_relic/stats_test.rb +426 -0
  164. data/test/new_relic/transaction_analysis/segment_summary_test.rb +77 -0
  165. data/test/new_relic/transaction_analysis_test.rb +121 -0
  166. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  167. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  168. data/test/new_relic/transaction_sample/segment_test.rb +454 -0
  169. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  170. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  171. data/test/new_relic/transaction_sample_test.rb +170 -0
  172. data/test/new_relic/version_number_test.rb +89 -0
  173. data/test/test_contexts.rb +29 -0
  174. data/test/test_helper.rb +144 -0
  175. data/ui/helpers/developer_mode_helper.rb +351 -0
  176. data/ui/helpers/google_pie_chart.rb +49 -0
  177. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  178. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  179. data/ui/views/newrelic/_sample.rhtml +20 -0
  180. data/ui/views/newrelic/_segment.rhtml +28 -0
  181. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  182. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  183. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  184. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  185. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  186. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  187. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  188. data/ui/views/newrelic/_table.rhtml +12 -0
  189. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  190. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  191. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  192. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  193. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  194. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  195. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  196. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  197. data/ui/views/newrelic/file/images/textmate.png +0 -0
  198. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  199. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  200. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  201. data/ui/views/newrelic/index.rhtml +71 -0
  202. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  203. data/ui/views/newrelic/show_sample.rhtml +80 -0
  204. data/ui/views/newrelic/show_source.rhtml +3 -0
  205. data/ui/views/newrelic/threads.rhtml +53 -0
  206. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +19 -0
  207. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  208. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +57 -0
  209. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  210. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  211. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
  212. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  213. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  214. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  215. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  216. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  217. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  218. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  219. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  220. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +122 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +66 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  255. metadata +399 -0
@@ -0,0 +1,54 @@
1
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
2
+
3
+ DependencyDetection.defer do
4
+ depends_on do
5
+ defined?(::Sinatra) && defined?(::Sinatra::Base)
6
+ end
7
+
8
+ executes do
9
+ ::Sinatra::Base.class_eval do
10
+ include NewRelic::Agent::Instrumentation::Sinatra
11
+ alias route_eval_without_newrelic route_eval
12
+ alias route_eval route_eval_with_newrelic
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ module NewRelic
19
+ module Agent
20
+ module Instrumentation
21
+ # NewRelic instrumentation for Sinatra applications. Sinatra actions will
22
+ # appear in the UI similar to controller actions, and have breakdown charts
23
+ # and transaction traces.
24
+ #
25
+ # The actions in the UI will correspond to the pattern expression used
26
+ # to match them. HTTP operations are not distinguished. Multiple matches
27
+ # will all be tracked as separate actions.
28
+ module Sinatra
29
+
30
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
31
+
32
+ def route_eval_with_newrelic(&block_arg)
33
+ path = unescape(@request.path_info)
34
+ name = path
35
+ # Go through each route and look for a match
36
+ if routes = self.class.routes[@request.request_method]
37
+ routes.detect do |pattern, keys, conditions, block|
38
+ if block_arg.equal? block
39
+ name = pattern.source
40
+ end
41
+ end
42
+ end
43
+ # strip off leading ^ and / chars and trailing $ and /
44
+ name.gsub!(%r{^[/^]*(.*?)[/\$\?]*$}, '\1')
45
+ name = 'root' if name.empty?
46
+ name = @request.request_method + ' ' + name if @request && @request.respond_to?(:request_method)
47
+ perform_action_with_newrelic_trace(:category => :sinatra, :name => name, :params => @request.params) do
48
+ route_eval_without_newrelic(&block_arg)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ DependencyDetection.defer do
2
+ depends_on do
3
+ defined?(::Sunspot)
4
+ end
5
+
6
+ executes do
7
+ ::Sunspot.module_eval do
8
+ class << self
9
+ %w(index index!).each do |method|
10
+ add_method_tracer method, 'SolrClient/Sunspot/index'
11
+ end
12
+ add_method_tracer :commit, 'SolrClient/Sunspot/commit'
13
+
14
+ %w[search more_like_this].each do |method|
15
+ add_method_tracer method, 'SolrClient/Sunspot/query'
16
+ end
17
+ %w[remove remove! remove_by_id remove_by_id! remove_all remove_all!].each do |method|
18
+ add_method_tracer method, 'SolrClient/Sunspot/delete'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ DependencyDetection.defer do
2
+ depends_on do
3
+ defined?(::Unicorn) && defined?(::Unicorn::HttpServer)
4
+ end
5
+
6
+ executes do
7
+ Unicorn::HttpServer.class_eval do
8
+ NewRelic::Agent.logger.debug "Installing Unicorn worker hook."
9
+ old_worker_loop = instance_method(:worker_loop)
10
+ define_method(:worker_loop) do | worker |
11
+ NewRelic::Agent.after_fork(:force_reconnect => true)
12
+ old_worker_loop.bind(self).call(worker)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,422 @@
1
+ require 'new_relic/control'
2
+ module NewRelic
3
+ module Agent
4
+ # This module contains class methods added to support installing custom
5
+ # metric tracers and executing for individual metrics.
6
+ #
7
+ # == Examples
8
+ #
9
+ # When the agent initializes, it extends Module with these methods.
10
+ # However if you want to use the API in code that might get loaded
11
+ # before the agent is initialized you will need to require
12
+ # this file:
13
+ #
14
+ # require 'new_relic/agent/method_tracer'
15
+ # class A
16
+ # include NewRelic::Agent::MethodTracer
17
+ # def process
18
+ # ...
19
+ # end
20
+ # add_method_tracer :process
21
+ # end
22
+ #
23
+ # To instrument a class method:
24
+ #
25
+ # require 'new_relic/agent/method_tracer'
26
+ # class An
27
+ # def self.process
28
+ # ...
29
+ # end
30
+ # class << self
31
+ # include NewRelic::Agent::MethodTracer
32
+ # add_method_tracer :process
33
+ # end
34
+ # end
35
+
36
+ module MethodTracer
37
+
38
+ def self.included clazz #:nodoc:
39
+ clazz.extend ClassMethods
40
+ clazz.send :include, InstanceMethods
41
+ end
42
+
43
+ def self.extended clazz #:nodoc:
44
+ clazz.extend ClassMethods
45
+ clazz.extend InstanceMethods
46
+ end
47
+
48
+ module InstanceMethods
49
+ # Deprecated: original method preserved for API backward compatibility.
50
+ # Use either #trace_execution_scoped or #trace_execution_unscoped
51
+ def trace_method_execution(metric_names, push_scope, produce_metric, deduct_call_time_from_parent, &block) #:nodoc:
52
+ if push_scope
53
+ trace_execution_scoped(metric_names, :metric => produce_metric,
54
+ :deduct_call_time_from_parent => deduct_call_time_from_parent, &block)
55
+ else
56
+ trace_execution_unscoped(metric_names, &block)
57
+ end
58
+ end
59
+
60
+ # Trace a given block with stats assigned to the given metric_name. It does not
61
+ # provide scoped measurements, meaning whatever is being traced will not 'blame the
62
+ # Controller'--that is to say appear in the breakdown chart.
63
+ # This is code is inlined in #add_method_tracer.
64
+ # * <tt>metric_names</tt> is a single name or an array of names of metrics
65
+ # * <tt>:force => true</tt> will force the metric to be captured even when
66
+ # tracing is disabled with NewRelic::Agent#disable_all_tracing
67
+ #
68
+ def trace_execution_unscoped(metric_names, options={})
69
+ return yield unless NewRelic::Agent.is_execution_traced?
70
+ t0 = Time.now
71
+ stats = Array(metric_names).map do | metric_name |
72
+ NewRelic::Agent.instance.stats_engine.get_stats_no_scope metric_name
73
+ end
74
+ begin
75
+ NewRelic::Agent.instance.push_trace_execution_flag(true) if options[:force]
76
+ yield
77
+ ensure
78
+ NewRelic::Agent.instance.pop_trace_execution_flag if options[:force]
79
+ duration = (Time.now - t0).to_f # for some reason this is 3 usec faster than Time - Time
80
+ stats.each { |stat| stat.trace_call(duration) }
81
+ end
82
+ end
83
+
84
+ # Deprecated. Use #trace_execution_scoped, a version with an options hash.
85
+ def trace_method_execution_with_scope(metric_names, produce_metric, deduct_call_time_from_parent, scoped_metric_only=false, &block) #:nodoc:
86
+ trace_execution_scoped(metric_names,
87
+ :metric => produce_metric,
88
+ :deduct_call_time_from_parent => deduct_call_time_from_parent,
89
+ :scoped_metric_only => scoped_metric_only, &block)
90
+ end
91
+
92
+ alias trace_method_execution_no_scope trace_execution_unscoped #:nodoc:
93
+
94
+ module TraceExecutionScoped
95
+ def agent_instance
96
+ NewRelic::Agent.instance
97
+ end
98
+
99
+ def traced?
100
+ NewRelic::Agent.is_execution_traced?
101
+ end
102
+
103
+ def trace_disabled?(options)
104
+ !(traced? || options[:force])
105
+ end
106
+
107
+ def stat_engine
108
+ agent_instance.stats_engine
109
+ end
110
+
111
+ def get_stats_scoped(first_name, scoped_metric_only)
112
+ stat_engine.get_stats(first_name, true, scoped_metric_only)
113
+ end
114
+ def get_stats_unscoped(name)
115
+ stat_engine.get_stats_no_scope(name)
116
+ end
117
+
118
+ def main_stat(metric, options)
119
+ get_stats_scoped(metric, options[:scoped_metric_only])
120
+ end
121
+
122
+ def get_metric_stats(metrics, options)
123
+ metrics = Array(metrics)
124
+ first_name = metrics.shift
125
+ stats = metrics.map do | name |
126
+ get_stats_unscoped(name)
127
+ end
128
+ stats.unshift(main_stat(first_name, options)) if options[:metric]
129
+ [first_name, stats]
130
+ end
131
+
132
+ def set_if_nil(hash, key)
133
+ hash[key] = true if hash[key].nil?
134
+ end
135
+
136
+ def push_flag!(forced)
137
+ agent_instance.push_trace_execution_flag(true) if forced
138
+ end
139
+
140
+ def pop_flag!(forced)
141
+ agent_instance.pop_trace_execution_flag if forced
142
+ end
143
+
144
+ def log_errors(code_area, metric)
145
+ yield
146
+ rescue => e
147
+ NewRelic::Control.instance.log.error("Caught exception in #{code_area}. Metric name = #{metric}, exception = #{e}")
148
+ NewRelic::Control.instance.log.error(e.backtrace.join("\n"))
149
+ end
150
+
151
+ def trace_execution_scoped_header(metric, options, t0=Time.now.to_f)
152
+ scope = log_errors("trace_execution_scoped header", metric) do
153
+ push_flag!(options[:force])
154
+ scope = stat_engine.push_scope(metric, t0, options[:deduct_call_time_from_parent])
155
+ end
156
+ # needed in case we have an error, above, to always return
157
+ # the start time.
158
+ [t0, scope]
159
+ end
160
+
161
+ def trace_execution_scoped_footer(t0, first_name, metric_stats, expected_scope, forced, t1=Time.now.to_f)
162
+ log_errors("trace_method_execution footer", first_name) do
163
+ duration = t1 - t0
164
+
165
+ pop_flag!(forced)
166
+ if expected_scope
167
+ scope = stat_engine.pop_scope(expected_scope, duration, t1)
168
+ exclusive = duration - scope.children_time
169
+ metric_stats.each { |stats| stats.trace_call(duration, exclusive) }
170
+ end
171
+ end
172
+ end
173
+
174
+ # Trace a given block with stats and keep track of the caller.
175
+ # See NewRelic::Agent::MethodTracer::ClassMethods#add_method_tracer for a description of the arguments.
176
+ # +metric_names+ is either a single name or an array of metric names.
177
+ # If more than one metric is passed, the +produce_metric+ option only applies to the first. The
178
+ # others are always recorded. Only the first metric is pushed onto the scope stack.
179
+ #
180
+ # Generally you pass an array of metric names if you want to record the metric under additional
181
+ # categories, but generally this *should never ever be done*. Most of the time you can aggregate
182
+ # on the server.
183
+
184
+ def trace_execution_scoped(metric_names, options={})
185
+ return yield if trace_disabled?(options)
186
+ set_if_nil(options, :metric)
187
+ set_if_nil(options, :deduct_call_time_from_parent)
188
+ first_name, metric_stats = get_metric_stats(metric_names, options)
189
+ start_time, expected_scope = trace_execution_scoped_header(first_name, options)
190
+ begin
191
+ yield
192
+ ensure
193
+ trace_execution_scoped_footer(start_time, first_name, metric_stats, expected_scope, options[:force])
194
+ end
195
+ end
196
+
197
+ end
198
+ include TraceExecutionScoped
199
+
200
+ end
201
+
202
+ module ClassMethods
203
+ # Add a method tracer to the specified method.
204
+ #
205
+ # === Common Options
206
+ #
207
+ # * <tt>:push_scope => false</tt> specifies this method tracer should not
208
+ # keep track of the caller; it will not show up in controller breakdown
209
+ # pie charts.
210
+ # * <tt>:metric => false</tt> specifies that no metric will be recorded.
211
+ # Instead the call will show up in transaction traces as well as traces
212
+ # shown in Developer Mode.
213
+ #
214
+ # === Uncommon Options
215
+ #
216
+ # * <tt>:scoped_metric_only => true</tt> indicates that the unscoped metric
217
+ # should not be recorded. Normally two metrics are potentially created
218
+ # on every invocation: the aggregate method where statistics for all calls
219
+ # of that metric are stored, and the "scoped metric" which records the
220
+ # statistics for invocations in a particular scope--generally a controller
221
+ # action. This option indicates that only the second type should be recorded.
222
+ # The effect is similar to <tt>:metric => false</tt> but in addition you
223
+ # will also see the invocation in breakdown pie charts.
224
+ # * <tt>:deduct_call_time_from_parent => false</tt> indicates that the method invocation
225
+ # time should never be deducted from the time reported as 'exclusive' in the
226
+ # caller. You would want to use this if you are tracing a recursive method
227
+ # or a method that might be called inside another traced method.
228
+ # * <tt>:code_header</tt> and <tt>:code_footer</tt> specify ruby code that
229
+ # is inserted into the tracer before and after the call.
230
+ # * <tt>:force = true</tt> will ensure the metric is captured even if called inside
231
+ # an untraced execution call. (See NewRelic::Agent#disable_all_tracing)
232
+ #
233
+ # === Overriding the metric name
234
+ #
235
+ # +metric_name_code+ is a string that is eval'd to get the
236
+ # name of the metric associated with the call, so if you want to
237
+ # use interpolaion evaluated at call time, then single quote
238
+ # the value like this:
239
+ #
240
+ # add_method_tracer :foo, 'Custom/#{self.class.name}/foo'
241
+ #
242
+ # This would name the metric according to the class of the runtime
243
+ # intance, as opposed to the class where +foo+ is defined.
244
+ #
245
+ # If not provided, the metric name will be <tt>Custom/ClassName/method_name</tt>.
246
+ #
247
+ # === Examples
248
+ #
249
+ # Instrument +foo+ only for custom views--will not show up in transaction traces or caller breakdown graphs:
250
+ #
251
+ # add_method_tracer :foo, :push_scope => false
252
+ #
253
+ # Instrument +foo+ just for transaction traces only:
254
+ #
255
+ # add_method_tracer :foo, :metric => false
256
+ #
257
+ # Instrument +foo+ so it shows up in transaction traces and caller breakdown graphs
258
+ # for actions:
259
+ #
260
+ # add_method_tracer :foo
261
+ #
262
+ # which is equivalent to:
263
+ #
264
+ # add_method_tracer :foo, 'Custom/#{self.class.name}/foo', :push_scope => true, :metric => true
265
+ #
266
+ # Instrument the class method +foo+ with the metric name 'Custom/People/fetch':
267
+ #
268
+ # class << self
269
+ # add_method_tracer :foo, 'Custom/People/fetch'
270
+ # end
271
+ #
272
+
273
+ module AddMethodTracer
274
+ ALLOWED_KEYS = [:force, :metric, :push_scope, :deduct_call_time_from_parent, :code_header, :code_footer, :scoped_metric_only].freeze
275
+
276
+ def unrecognized_keys(expected, given)
277
+ given.keys - expected
278
+ end
279
+
280
+ def any_unrecognized_keys?(expected, given)
281
+ unrecognized_keys(expected, given).any?
282
+ end
283
+
284
+ def check_for_illegal_keys!(options)
285
+ if any_unrecognized_keys?(ALLOWED_KEYS, options)
286
+ raise "Unrecognized options in add_method_tracer_call: #{unrecognized_keys(ALLOWED_KEYS, options).join(', ')}"
287
+ end
288
+ end
289
+
290
+ def set_deduct_call_time_based_on_metric(options)
291
+ {:deduct_call_time_from_parent => !!options[:metric]}.merge(options)
292
+ end
293
+
294
+ def check_for_push_scope_and_metric(options)
295
+ unless options[:push_scope] || options[:metric]
296
+ raise "Can't add a tracer where push_scope is false and metric is false"
297
+ end
298
+ end
299
+
300
+ DEFAULT_SETTINGS = {:push_scope => true, :metric => true, :force => false, :code_header => "", :code_footer => "", :scoped_metric_only => false}.freeze
301
+
302
+ def validate_options(options)
303
+ raise TypeError.new("provided options must be a Hash") unless options.is_a?(Hash)
304
+ check_for_illegal_keys!(options)
305
+ options = set_deduct_call_time_based_on_metric(DEFAULT_SETTINGS.merge(options))
306
+ check_for_push_scope_and_metric(options)
307
+ options
308
+ end
309
+
310
+ # Default to the class where the method is defined.
311
+ def default_metric_name_code(method_name)
312
+ "Custom/#{self.name}/#{method_name.to_s}"
313
+ end
314
+
315
+ def newrelic_method_exists?(method_name)
316
+ exists = method_defined?(method_name) || private_method_defined?(method_name)
317
+ NewRelic::Control.instance.log.warn("Did not trace #{self.name}##{method_name} because that method does not exist") unless exists
318
+ exists
319
+ end
320
+
321
+ def traced_method_exists?(method_name, metric_name_code)
322
+ exists = method_defined?(_traced_method_name(method_name, metric_name_code))
323
+ NewRelic::Control.instance.log.warn("Attempt to trace a method twice with the same metric: Method = #{method_name}, Metric Name = #{metric_name_code}") if exists
324
+ exists
325
+ end
326
+
327
+ def assemble_code_header(method_name, metric_name_code, options)
328
+ unless options[:force]
329
+ "return #{_untraced_method_name(method_name, metric_name_code)}(*args, &block) unless NewRelic::Agent.is_execution_traced?\n"
330
+ end.to_s + options[:code_header].to_s
331
+ end
332
+
333
+ def method_without_push_scope(method_name, metric_name_code, options)
334
+ "def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
335
+ #{assemble_code_header(method_name, metric_name_code, options)}
336
+ t0 = Time.now
337
+ stats = NewRelic::Agent.instance.stats_engine.get_stats_no_scope \"#{metric_name_code}\"
338
+ begin
339
+ #{"NewRelic::Agent.instance.push_trace_execution_flag(true)\n" if options[:force]}
340
+ #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)\n
341
+ ensure
342
+ #{"NewRelic::Agent.instance.pop_trace_execution_flag\n" if options[:force] }
343
+ duration = (Time.now - t0).to_f
344
+ stats.trace_call(duration)
345
+ #{options[:code_footer]}
346
+ end
347
+ end"
348
+ end
349
+
350
+ def method_with_push_scope(method_name, metric_name_code, options)
351
+ klass = (self === Module) ? "self" : "self.class"
352
+
353
+ "def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
354
+ #{options[:code_header]}
355
+ result = #{klass}.trace_execution_scoped(\"#{metric_name_code}\",
356
+ :metric => #{options[:metric]},
357
+ :forced => #{options[:force]},
358
+ :deduct_call_time_from_parent => #{options[:deduct_call_time_from_parent]},
359
+ :scoped_metric_only => #{options[:scoped_metric_only]}) do
360
+ #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)
361
+ end
362
+ #{options[:code_footer]}
363
+ result
364
+ end"
365
+ end
366
+
367
+ def code_to_eval(method_name, metric_name_code, options)
368
+ options = validate_options(options)
369
+ if options[:push_scope]
370
+ method_with_push_scope(method_name, metric_name_code, options)
371
+ else
372
+ method_without_push_scope(method_name, metric_name_code, options)
373
+ end
374
+ end
375
+ end
376
+ include AddMethodTracer
377
+
378
+ def add_method_tracer(method_name, metric_name_code=nil, options = {})
379
+ return unless newrelic_method_exists?(method_name)
380
+ metric_name_code ||= default_metric_name_code(method_name)
381
+ return if traced_method_exists?(method_name, metric_name_code)
382
+
383
+ traced_method = code_to_eval(method_name, metric_name_code, options)
384
+
385
+ class_eval traced_method, __FILE__, __LINE__
386
+ alias_method _untraced_method_name(method_name, metric_name_code), method_name
387
+ alias_method method_name, _traced_method_name(method_name, metric_name_code)
388
+ NewRelic::Control.instance.log.debug("Traced method: class = #{self.name},"+
389
+ "method = #{method_name}, "+
390
+ "metric = '#{metric_name_code}'")
391
+ end
392
+
393
+ # For tests only because tracers must be removed in reverse-order
394
+ # from when they were added, or else other tracers that were added to the same method
395
+ # may get removed as well.
396
+ def remove_method_tracer(method_name, metric_name_code) # :nodoc:
397
+ return unless NewRelic::Control.instance.agent_enabled?
398
+ if method_defined? "#{_traced_method_name(method_name, metric_name_code)}"
399
+ alias_method method_name, "#{_untraced_method_name(method_name, metric_name_code)}"
400
+ undef_method "#{_traced_method_name(method_name, metric_name_code)}"
401
+ NewRelic::Control.instance.log.debug("removed method tracer #{method_name} #{metric_name_code}\n")
402
+ else
403
+ raise "No tracer for '#{metric_name_code}' on method '#{method_name}'"
404
+ end
405
+ end
406
+ private
407
+
408
+ def _untraced_method_name(method_name, metric_name)
409
+ "#{_sanitize_name(method_name)}_without_trace_#{_sanitize_name(metric_name)}"
410
+ end
411
+
412
+ def _traced_method_name(method_name, metric_name)
413
+ "#{_sanitize_name(method_name)}_with_trace_#{_sanitize_name(metric_name)}"
414
+ end
415
+
416
+ def _sanitize_name(name)
417
+ name.to_s.tr_s('^a-zA-Z0-9', '_')
418
+ end
419
+ end
420
+ end
421
+ end
422
+ end