wd_newrelic_rpm 3.3.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. data/CHANGELOG +591 -0
  2. data/LICENSE +64 -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 +467 -0
  14. data/lib/new_relic/agent/agent.rb +1325 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +121 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +142 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +99 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/database.rb +223 -0
  20. data/lib/new_relic/agent/error_collector.rb +251 -0
  21. data/lib/new_relic/agent/instrumentation.rb +9 -0
  22. data/lib/new_relic/agent/instrumentation/active_merchant.rb +29 -0
  23. data/lib/new_relic/agent/instrumentation/active_record.rb +137 -0
  24. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +68 -0
  25. data/lib/new_relic/agent/instrumentation/authlogic.rb +19 -0
  26. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +443 -0
  27. data/lib/new_relic/agent/instrumentation/data_mapper.rb +238 -0
  28. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +52 -0
  29. data/lib/new_relic/agent/instrumentation/memcache.rb +80 -0
  30. data/lib/new_relic/agent/instrumentation/merb/controller.rb +41 -0
  31. data/lib/new_relic/agent/instrumentation/merb/errors.rb +29 -0
  32. data/lib/new_relic/agent/instrumentation/metric_frame.rb +353 -0
  33. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +80 -0
  34. data/lib/new_relic/agent/instrumentation/net.rb +29 -0
  35. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +37 -0
  36. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  37. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  38. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +114 -0
  39. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +42 -0
  40. data/lib/new_relic/agent/instrumentation/rails/errors.rb +42 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +180 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +37 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +78 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +29 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +21 -0
  46. data/lib/new_relic/agent/method_tracer.rb +528 -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 +143 -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/sql_sampler.rb +286 -0
  54. data/lib/new_relic/agent/stats_engine.rb +26 -0
  55. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  56. data/lib/new_relic/agent/stats_engine/metric_stats.rb +187 -0
  57. data/lib/new_relic/agent/stats_engine/samplers.rb +95 -0
  58. data/lib/new_relic/agent/stats_engine/transactions.rb +125 -0
  59. data/lib/new_relic/agent/transaction_info.rb +74 -0
  60. data/lib/new_relic/agent/transaction_sample_builder.rb +116 -0
  61. data/lib/new_relic/agent/transaction_sampler.rb +468 -0
  62. data/lib/new_relic/agent/worker_loop.rb +89 -0
  63. data/lib/new_relic/collection_helper.rb +77 -0
  64. data/lib/new_relic/command.rb +85 -0
  65. data/lib/new_relic/commands/deployments.rb +105 -0
  66. data/lib/new_relic/commands/install.rb +80 -0
  67. data/lib/new_relic/control.rb +46 -0
  68. data/lib/new_relic/control/class_methods.rb +53 -0
  69. data/lib/new_relic/control/configuration.rb +206 -0
  70. data/lib/new_relic/control/frameworks.rb +10 -0
  71. data/lib/new_relic/control/frameworks/external.rb +16 -0
  72. data/lib/new_relic/control/frameworks/merb.rb +31 -0
  73. data/lib/new_relic/control/frameworks/rails.rb +164 -0
  74. data/lib/new_relic/control/frameworks/rails3.rb +75 -0
  75. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  76. data/lib/new_relic/control/frameworks/sinatra.rb +20 -0
  77. data/lib/new_relic/control/instance_methods.rb +179 -0
  78. data/lib/new_relic/control/instrumentation.rb +100 -0
  79. data/lib/new_relic/control/logging_methods.rb +143 -0
  80. data/lib/new_relic/control/profiling.rb +25 -0
  81. data/lib/new_relic/control/server_methods.rb +114 -0
  82. data/lib/new_relic/data_serialization.rb +151 -0
  83. data/lib/new_relic/delayed_job_injection.rb +51 -0
  84. data/lib/new_relic/language_support.rb +73 -0
  85. data/lib/new_relic/local_environment.rb +428 -0
  86. data/lib/new_relic/merbtasks.rb +6 -0
  87. data/lib/new_relic/metric_data.rb +51 -0
  88. data/lib/new_relic/metric_spec.rb +76 -0
  89. data/lib/new_relic/metrics.rb +9 -0
  90. data/lib/new_relic/noticed_error.rb +29 -0
  91. data/lib/new_relic/rack/browser_monitoring.rb +76 -0
  92. data/lib/new_relic/rack/developer_mode.rb +268 -0
  93. data/lib/new_relic/recipes.rb +77 -0
  94. data/lib/new_relic/stats.rb +335 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +77 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +49 -0
  98. data/lib/new_relic/transaction_sample.rb +261 -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 +203 -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 +312 -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/fixtures/proc_cpuinfo.txt +575 -0
  116. data/test/new_relic/agent/agent/connect_test.rb +403 -0
  117. data/test/new_relic/agent/agent/start_test.rb +255 -0
  118. data/test/new_relic/agent/agent/start_worker_thread_test.rb +153 -0
  119. data/test/new_relic/agent/agent_test.rb +140 -0
  120. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  121. data/test/new_relic/agent/agent_test_controller_test.rb +382 -0
  122. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  123. data/test/new_relic/agent/beacon_configuration_test.rb +111 -0
  124. data/test/new_relic/agent/browser_monitoring_test.rb +323 -0
  125. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  126. data/test/new_relic/agent/database_test.rb +149 -0
  127. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  128. data/test/new_relic/agent/error_collector_test.rb +192 -0
  129. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +576 -0
  130. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +34 -0
  131. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  132. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +171 -0
  133. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  134. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  135. data/test/new_relic/agent/instrumentation/queue_time_test.rb +382 -0
  136. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  137. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  138. data/test/new_relic/agent/memcache_instrumentation_test.rb +143 -0
  139. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  140. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  141. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  142. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  143. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  144. data/test/new_relic/agent/sampler_test.rb +19 -0
  145. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  146. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  147. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  148. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +82 -0
  149. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  150. data/test/new_relic/agent/stats_engine_test.rb +220 -0
  151. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  152. data/test/new_relic/agent/transaction_sample_builder_test.rb +219 -0
  153. data/test/new_relic/agent/transaction_sampler_test.rb +967 -0
  154. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  155. data/test/new_relic/agent_test.rb +187 -0
  156. data/test/new_relic/collection_helper_test.rb +149 -0
  157. data/test/new_relic/command/deployments_test.rb +68 -0
  158. data/test/new_relic/control/class_methods_test.rb +62 -0
  159. data/test/new_relic/control/configuration_test.rb +84 -0
  160. data/test/new_relic/control/logging_methods_test.rb +185 -0
  161. data/test/new_relic/control_test.rb +256 -0
  162. data/test/new_relic/data_serialization_test.rb +208 -0
  163. data/test/new_relic/delayed_job_injection_test.rb +16 -0
  164. data/test/new_relic/local_environment_test.rb +85 -0
  165. data/test/new_relic/metric_data_test.rb +125 -0
  166. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  167. data/test/new_relic/metric_spec_test.rb +95 -0
  168. data/test/new_relic/rack/all_test.rb +11 -0
  169. data/test/new_relic/rack/browser_monitoring_test.rb +142 -0
  170. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  171. data/test/new_relic/rack/developer_mode_test.rb +74 -0
  172. data/test/new_relic/stats_test.rb +411 -0
  173. data/test/new_relic/transaction_analysis/segment_summary_test.rb +91 -0
  174. data/test/new_relic/transaction_analysis_test.rb +121 -0
  175. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  176. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  177. data/test/new_relic/transaction_sample/segment_test.rb +389 -0
  178. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  179. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  180. data/test/new_relic/transaction_sample_test.rb +177 -0
  181. data/test/new_relic/version_number_test.rb +89 -0
  182. data/test/script/build_test_gem.sh +51 -0
  183. data/test/script/ci.sh +94 -0
  184. data/test/script/ci_bench.sh +52 -0
  185. data/test/test_contexts.rb +29 -0
  186. data/test/test_helper.rb +155 -0
  187. data/ui/helpers/developer_mode_helper.rb +357 -0
  188. data/ui/helpers/google_pie_chart.rb +48 -0
  189. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  190. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  191. data/ui/views/newrelic/_sample.rhtml +20 -0
  192. data/ui/views/newrelic/_segment.rhtml +28 -0
  193. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  194. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  195. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  196. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  197. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  198. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  199. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  200. data/ui/views/newrelic/_table.rhtml +12 -0
  201. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  202. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  203. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  204. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  205. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  206. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  207. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  208. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  209. data/ui/views/newrelic/file/images/textmate.png +0 -0
  210. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  211. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  212. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  213. data/ui/views/newrelic/index.rhtml +71 -0
  214. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  215. data/ui/views/newrelic/show_sample.rhtml +80 -0
  216. data/ui/views/newrelic/show_source.rhtml +3 -0
  217. data/ui/views/newrelic/threads.rhtml +53 -0
  218. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +5 -0
  219. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +67 -0
  220. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +135 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  255. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  256. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  257. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  258. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  259. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  260. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +70 -0
  261. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  262. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  263. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  264. metadata +376 -0
@@ -0,0 +1,238 @@
1
+ ## NewRelic instrumentation for DataMapper
2
+ #
3
+ # Instrumenting DM has different key challenges versus AR:
4
+ #
5
+ # 1. The hooking of SQL logging in DM is decoupled from any knowledge of the
6
+ # Model#method that invoked it. But on the positive side, the duration is
7
+ # already calculated for you (and it happens inside the C-based DO code, so
8
+ # it's faster than a Ruby equivalent).
9
+ #
10
+ # 2. There are a lot more entry points that need to be hooked in order to
11
+ # understand call flow: DM::Model (model class) vs. DM::Resource (model
12
+ # instance) vs. DM::Collection (collection of model instances). And
13
+ # others.
14
+ #
15
+ # 3. Strategic Eager Loading (SEL) combined with separately-grouped
16
+ # lazy-loaded attributes presents a unique problem for tying resulting
17
+ # SEL-invoked SQL calls to their proper scope.
18
+ #
19
+ # NOTE: On using "Database" versus "ActiveRecord" as base metric name
20
+ #
21
+ # Using "Database" as the metric name base seems to properly identify methods
22
+ # as being DB-related in call graphs, but certain New Relic views that show
23
+ # aggregations of DB CPM, etc still seem to rely solely on "ActiveRecord"
24
+ # being the base name, thus AFAICT "Database" calls to this are lost. (Though
25
+ # I haven't yet tested "Database/SQL/{find/save/destroy/all}" yet, as it seems
26
+ # like an intuitively good name to use.)
27
+ #
28
+ # So far I think these are the rules:
29
+ #
30
+ # - ActiveRecord/{find/save/destroy} populates "Database Throughput" and
31
+ # "Database Response Time" in the Database tab. [non-scoped]
32
+ #
33
+ # - ActiveRecord/all populates the main Overview tab of DB time. (still
34
+ # unsure about this one). [non-scoped]
35
+ #
36
+ # These metrics are represented as :push_scope => false or included as the
37
+ # non-first metric in trace_execution_scoped() (docs say only first counts
38
+ # towards scope) so they don't show up ine normal call graph/trace.
39
+
40
+ DependencyDetection.defer do
41
+ @name = :data_mapper
42
+
43
+ depends_on do
44
+ defined?(::DataMapper)
45
+ end
46
+
47
+ depends_on do
48
+ defined?(DataMapper::Model)
49
+ end
50
+
51
+ depends_on do
52
+ defined?(DataMapper::Resource)
53
+ end
54
+
55
+ depends_on do
56
+ defined?(DataMapper::Collection)
57
+ end
58
+
59
+ executes do
60
+ NewRelic::Agent.logger.debug 'Installing DataMapper instrumentation'
61
+ end
62
+
63
+ executes do
64
+ DataMapper::Model.class_eval do
65
+ add_method_tracer :get, 'ActiveRecord/#{self.name}/get'
66
+ add_method_tracer :first, 'ActiveRecord/#{self.name}/first'
67
+ add_method_tracer :last, 'ActiveRecord/#{self.name}/last'
68
+ add_method_tracer :all, 'ActiveRecord/#{self.name}/all'
69
+
70
+ add_method_tracer :create, 'ActiveRecord/#{self.name}/create'
71
+ add_method_tracer :create!, 'ActiveRecord/#{self.name}/create'
72
+ add_method_tracer :update, 'ActiveRecord/#{self.name}/update'
73
+ add_method_tracer :update!, 'ActiveRecord/#{self.name}/update'
74
+ add_method_tracer :destroy, 'ActiveRecord/#{self.name}/destroy'
75
+ add_method_tracer :destroy!, 'ActiveRecord/#{self.name}/destroy'
76
+
77
+ # For dm-aggregates and partial dm-ar-finders support:
78
+ for method in [ :aggregate, :find, :find_by_sql ] do
79
+ next unless method_defined? method
80
+ add_method_tracer(method, 'ActiveRecord/#{self.name}/' + method.to_s)
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+ executes do
87
+ DataMapper::Resource.class_eval do
88
+ add_method_tracer :update, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/update'
89
+ add_method_tracer :update!, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/update'
90
+ add_method_tracer :save, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/save'
91
+ add_method_tracer :save!, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/save'
92
+ add_method_tracer :destroy, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/destroy'
93
+ add_method_tracer :destroy!, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/destroy'
94
+
95
+ end
96
+ end
97
+
98
+ executes do
99
+ DataMapper::Collection.class_eval do
100
+ # DM's Collection instance methods
101
+ add_method_tracer :get, 'ActiveRecord/#{self.name}/get'
102
+ add_method_tracer :first, 'ActiveRecord/#{self.name}/first'
103
+ add_method_tracer :last, 'ActiveRecord/#{self.name}/last'
104
+ add_method_tracer :all, 'ActiveRecord/#{self.name}/all'
105
+
106
+ add_method_tracer :lazy_load, 'ActiveRecord/#{self.name}/lazy_load'
107
+
108
+ add_method_tracer :create, 'ActiveRecord/#{self.name}/create'
109
+ add_method_tracer :create!, 'ActiveRecord/#{self.name}/create'
110
+ add_method_tracer :update, 'ActiveRecord/#{self.name}/update'
111
+ add_method_tracer :update!, 'ActiveRecord/#{self.name}/update'
112
+ add_method_tracer :destroy, 'ActiveRecord/#{self.name}/destroy'
113
+ add_method_tracer :destroy!, 'ActiveRecord/#{self.name}/destroy'
114
+
115
+ # For dm-aggregates support:
116
+ for method in [ :aggregate ] do
117
+ next unless method_defined? method
118
+ add_method_tracer(method, 'ActiveRecord/#{self.name}/' + method.to_s)
119
+ end
120
+
121
+ end
122
+ end
123
+ end
124
+
125
+ DependencyDetection.defer do
126
+
127
+ depends_on do
128
+ defined?(DataMapper) && defined?(DataMapper::Adapters) && defined?(DataMapper::Adapters::DataObjectsAdapter)
129
+ end
130
+
131
+ executes do
132
+
133
+ # Catch the two entry points into DM::Repository::Adapter that bypass CRUD
134
+ # (for when SQL is run directly).
135
+ DataMapper::Adapters::DataObjectsAdapter.class_eval do
136
+
137
+ add_method_tracer :select, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/select'
138
+ add_method_tracer :execute, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/execute'
139
+
140
+ end
141
+ end
142
+ end
143
+
144
+ DependencyDetection.defer do
145
+
146
+ depends_on do
147
+ defined?(DataMapper) && defined?(DataMapper::Validations) && defined?(DataMapper::Validations::ClassMethods)
148
+ end
149
+
150
+ # DM::Validations overrides Model#create, but currently in a way that makes it
151
+ # impossible to instrument from one place. I've got a patch pending inclusion
152
+ # to make it instrumentable by putting the create method inside ClassMethods.
153
+ # This will pick it up if/when that patch is accepted.
154
+ executes do
155
+ DataMapper::Validations::ClassMethods.class_eval do
156
+ next unless method_defined? :create
157
+ add_method_tracer :create, 'ActiveRecord/#{self.name}/create'
158
+ end
159
+ end
160
+ end
161
+
162
+ DependencyDetection.defer do
163
+
164
+ depends_on do
165
+ defined?(DataMapper) && defined?(DataMapper::Transaction)
166
+ end
167
+
168
+ # NOTE: DM::Transaction basically calls commit() twice, so as-is it will show
169
+ # up in traces twice -- second time subordinate to the first's scope. Works
170
+ # well enough.
171
+ executes do
172
+ DataMapper::Transaction.module_eval do
173
+ add_method_tracer :commit, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/commit'
174
+ end
175
+ end
176
+ end
177
+
178
+
179
+ module NewRelic
180
+ module Agent
181
+ module Instrumentation
182
+ module DataMapperInstrumentation
183
+
184
+ def self.included(klass)
185
+ klass.class_eval do
186
+ alias_method :log_without_newrelic_instrumentation, :log
187
+ alias_method :log, :log_with_newrelic_instrumentation
188
+ end
189
+ end
190
+
191
+ # Unlike in AR, log is called in DM after the query actually ran,
192
+ # complete with metrics. Since DO has already calculated the
193
+ # duration, there's nothing more to measure, so just record and log.
194
+ #
195
+ # We rely on the assumption that all possible entry points have been
196
+ # hooked with tracers, ensuring that notice_sql attaches this SQL to
197
+ # the proper call scope.
198
+ def log_with_newrelic_instrumentation(msg)
199
+ return unless NewRelic::Agent.is_execution_traced?
200
+ return unless operation = case msg.query
201
+ when /^\s*select/i then 'find'
202
+ when /^\s*(update|insert)/i then 'save'
203
+ when /^\s*delete/i then 'destroy'
204
+ else nil
205
+ end
206
+
207
+ # FYI: self.to_s will yield connection URI string.
208
+ duration = msg.duration / 1000000.0
209
+
210
+ # Attach SQL to current segment/scope.
211
+ NewRelic::Agent.instance.transaction_sampler.notice_sql(msg.query, nil, duration)
212
+
213
+ # Record query duration associated with each of the desired metrics.
214
+ metrics = [ "ActiveRecord/#{operation}", 'ActiveRecord/all' ]
215
+ metrics.each do |metric|
216
+ NewRelic::Agent.instance.stats_engine.get_stats_no_scope(metric).trace_call(duration)
217
+ end
218
+ ensure
219
+ log_without_newrelic_instrumentation(msg)
220
+ end
221
+
222
+ end # DataMapperInstrumentation
223
+ end # Instrumentation
224
+ end # Agent
225
+ end # NewRelic
226
+
227
+ DependencyDetection.defer do
228
+
229
+ depends_on do
230
+ defined?(DataObjects) && defined?(DataObjects::Connection)
231
+ end
232
+
233
+ executes do
234
+ DataObjects::Connection.class_eval do
235
+ include ::NewRelic::Agent::Instrumentation::DataMapperInstrumentation
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,52 @@
1
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
2
+
3
+ DependencyDetection.defer do
4
+ @name = :delayed_job
5
+
6
+ depends_on do
7
+ !NewRelic::Control.instance['disable_dj']
8
+ end
9
+
10
+ depends_on do
11
+ # double check because of old JRuby bug
12
+ defined?(::Delayed) && defined?(::Delayed::Job) &&
13
+ Delayed::Job.method_defined?(:invoke_job)
14
+ end
15
+
16
+ executes do
17
+ NewRelic::Agent.logger.debug 'Installing DelayedJob instrumentation'
18
+ end
19
+
20
+ executes do
21
+ Delayed::Job.class_eval do
22
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
23
+ if self.instance_methods.include?('name') || self.instance_methods.include?(:name)
24
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob', :path => '#{self.name}'
25
+ else
26
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob'
27
+ end
28
+ end
29
+ end
30
+
31
+ executes do
32
+ Delayed::Job.instance_eval do
33
+ if self.respond_to?('after_fork')
34
+ if method_defined?(:after_fork)
35
+ def after_fork_with_newrelic
36
+ NewRelic::Agent.after_fork(:force_reconnect => true)
37
+ after_fork_without_newrelic
38
+ end
39
+
40
+ alias_method :after_fork_without_newrelic, :after_fork
41
+ alias_method :after_fork, :after_fork_with_newrelic
42
+ else
43
+ def after_fork
44
+ NewRelic::Agent.after_fork(:force_reconnect => true)
45
+ super
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,80 @@
1
+ # NOTE there are multiple implementations of the MemCache client in Ruby,
2
+ # each with slightly different API's and semantics.
3
+ # See:
4
+ # http://www.deveiate.org/code/Ruby-MemCache/ (Gem: Ruby-MemCache)
5
+ # http://seattlerb.rubyforge.org/memcache-client/ (Gem: memcache-client)
6
+ # http://github.com/mperham/dalli (Gem: dalli)
7
+
8
+ module NewRelic
9
+ module Agent
10
+ module Instrumentation
11
+ module Memcache
12
+ module_function
13
+ def instrument_methods(the_class, method_names)
14
+ method_names.each do |method_name|
15
+ next unless the_class.method_defined? method_name.to_sym
16
+ the_class.class_eval <<-EOD
17
+ def #{method_name}_with_newrelic_trace(*args, &block)
18
+ metrics = ["Memcache/#{method_name}",
19
+ (NewRelic::Agent::Instrumentation::MetricFrame.recording_web_transaction? ? 'Memcache/allWeb' : 'Memcache/allOther')]
20
+ self.class.trace_execution_scoped(metrics) do
21
+ t0 = Time.now
22
+ begin
23
+ #{method_name}_without_newrelic_trace(*args, &block)
24
+ ensure
25
+ #{memcache_key_snippet(method_name)}
26
+ end
27
+ end
28
+ end
29
+ alias #{method_name}_without_newrelic_trace #{method_name}
30
+ alias #{method_name} #{method_name}_with_newrelic_trace
31
+ EOD
32
+ end
33
+ end
34
+ def memcache_key_snippet(method_name)
35
+ return "" unless NewRelic::Control.instance['capture_memcache_keys']
36
+ "NewRelic::Agent.instance.transaction_sampler.notice_nosql(args.first.inspect, (Time.now - t0).to_f) rescue nil"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ DependencyDetection.defer do
44
+ @name = :memcache
45
+
46
+ depends_on do
47
+ !NewRelic::Control.instance['disable_memcache_instrumentation']
48
+ end
49
+
50
+ depends_on do
51
+ defined?(::MemCache) || defined?(::Memcached) ||
52
+ defined?(::Dalli::Client) || defined?(::Spymemcached)
53
+ end
54
+
55
+ executes do
56
+ commands = %w[get get_multi set add incr decr delete replace append prepend]
57
+ if defined? ::MemCache
58
+ NewRelic::Agent::Instrumentation::Memcache.instrument_methods(::MemCache,
59
+ commands)
60
+ NewRelic::Agent.logger.debug 'Installing MemCache instrumentation'
61
+ end
62
+ if defined? ::Memcached
63
+ commands << 'cas'
64
+ NewRelic::Agent::Instrumentation::Memcache.instrument_methods(::Memcached,
65
+ commands)
66
+ NewRelic::Agent.logger.debug 'Installing Memcached instrumentation'
67
+ end
68
+ if defined? ::Dalli::Client
69
+ NewRelic::Agent::Instrumentation::Memcache.instrument_methods(::Dalli::Client,
70
+ commands)
71
+ NewRelic::Agent.logger.debug 'Installing Dalli Memcache instrumentation'
72
+ end
73
+ if defined? ::Spymemcached
74
+ commands << 'multiget'
75
+ NewRelic::Agent::Instrumentation::Memcache.instrument_methods(::Spymemcached,
76
+ commands)
77
+ NewRelic::Agent.logger.debug 'Installing Spymemcached instrumentation'
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,41 @@
1
+ require 'set'
2
+
3
+ DependencyDetection.defer do
4
+ @name = :merb_controller
5
+
6
+ depends_on do
7
+ defined?(Merb) && defined?(Merb::Controller)
8
+ end
9
+
10
+ executes do
11
+ NewRelic::Agent.logger.debug 'Installing Merb Controller instrumentation'
12
+ end
13
+
14
+ executes do
15
+ require 'merb-core/controller/merb_controller'
16
+
17
+ Merb::Controller.class_eval do
18
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
19
+
20
+ class_inheritable_accessor :do_not_trace
21
+ class_inheritable_accessor :ignore_apdex
22
+
23
+ def self.newrelic_write_attr(attr_name, value) # :nodoc:
24
+ self.send "#{attr_name}=", attr_name, value
25
+ end
26
+
27
+ def self.newrelic_read_attr(attr_name) # :nodoc:
28
+ self.send attr_name
29
+ end
30
+
31
+ protected
32
+ # determine the path that is used in the metric name for
33
+ # the called controller action
34
+ def newrelic_metric_path
35
+ "#{controller_name}/#{action_name}"
36
+ end
37
+ alias_method :perform_action_without_newrelic_trace, :_dispatch
38
+ alias_method :_dispatch, :perform_action_with_newrelic_trace
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ DependencyDetection.defer do
2
+ @name = :merb_error
3
+
4
+ depends_on do
5
+ defined?(Merb) && defined?(Merb::Dispatcher) && defined?(Merb::Dispatcher::DefaultException)
6
+ end
7
+
8
+ depends_on do
9
+ Merb::Dispatcher::DefaultException.respond_to?(:before)
10
+ end
11
+
12
+ executes do
13
+ NewRelic::Agent.logger.debug 'Installing Merb Errors instrumentation'
14
+ end
15
+
16
+ executes do
17
+
18
+ # Hook in the notification to merb
19
+ error_notifier = Proc.new {
20
+ if request.exceptions #check that there's actually an exception
21
+ # Note, this assumes we have already captured the other information such as uri and params in the MetricFrame.
22
+ NewRelic::Agent::Instrumentation::MetricFrame.notice_error(request.exceptions.first)
23
+ end
24
+ }
25
+ Merb::Dispatcher::DefaultException.before error_notifier
26
+ Exceptions.before error_notifier
27
+
28
+ end
29
+ end
@@ -0,0 +1,353 @@
1
+ require 'new_relic/agent/instrumentation/metric_frame/pop'
2
+
3
+ # A struct holding the information required to measure a controller
4
+ # action. This is put on the thread local. Handles the issue of
5
+ # re-entrancy, or nested action calls.
6
+ #
7
+ # This class is not part of the public API. Avoid making calls on it directly.
8
+ #
9
+ module NewRelic
10
+ module Agent
11
+ module Instrumentation
12
+ class MetricFrame
13
+ # helper module refactored out of the `pop` method
14
+ include Pop
15
+
16
+ attr_accessor :start # A Time instance for the start time, never nil
17
+ attr_accessor :apdex_start # A Time instance used for calculating the apdex score, which
18
+ # might end up being @start, or it might be further upstream if
19
+ # we can find a request header for the queue entry time
20
+ attr_accessor :exception,
21
+ :filtered_params, :force_flag,
22
+ :jruby_cpu_start, :process_cpu_start, :database_metric_name
23
+
24
+ # Give the current metric frame a request context. Use this to
25
+ # get the URI and referer. The request is interpreted loosely
26
+ # as a Rack::Request or an ActionController::AbstractRequest.
27
+ attr_accessor :request
28
+
29
+
30
+ @@check_server_connection = false
31
+ def self.check_server_connection=(value)
32
+ @@check_server_connection = value
33
+ end
34
+ # Return the currently active metric frame, or nil. Call with +true+
35
+ # to create a new metric frame if one is not already on the thread.
36
+ def self.current(create_if_empty=nil)
37
+ f = Thread.current[:newrelic_metric_frame]
38
+ return f if f || !create_if_empty
39
+
40
+ # Reconnect to the server if necessary. This is only done
41
+ # for old versions of passenger that don't implement an explicit after_fork
42
+ # event.
43
+ agent.after_fork(:keep_retrying => false) if @@check_server_connection
44
+
45
+ Thread.current[:newrelic_metric_frame] = new
46
+ end
47
+
48
+ # This is the name of the model currently assigned to database
49
+ # measurements, overriding the default.
50
+ def self.database_metric_name
51
+ current && current.database_metric_name
52
+ end
53
+
54
+ def self.referer
55
+ current && current.referer
56
+ end
57
+
58
+ def self.agent
59
+ NewRelic::Agent.instance
60
+ end
61
+
62
+ @@java_classes_loaded = false
63
+
64
+ if defined? JRuby
65
+ begin
66
+ require 'java'
67
+ include_class 'java.lang.management.ManagementFactory'
68
+ include_class 'com.sun.management.OperatingSystemMXBean'
69
+ @@java_classes_loaded = true
70
+ rescue => e
71
+ end
72
+ end
73
+
74
+ attr_reader :depth
75
+
76
+ def initialize
77
+ @start = Time.now
78
+ @path_stack = [] # stack of [controller, path] elements
79
+ @jruby_cpu_start = jruby_cpu_time
80
+ @process_cpu_start = process_cpu
81
+ Thread.current[:last_metric_frame] = self
82
+ end
83
+
84
+ def agent
85
+ NewRelic::Agent.instance
86
+ end
87
+
88
+ def transaction_sampler
89
+ agent.transaction_sampler
90
+ end
91
+
92
+ def sql_sampler
93
+ agent.sql_sampler
94
+ end
95
+
96
+ private :agent
97
+ private :transaction_sampler
98
+ private :sql_sampler
99
+
100
+ # Indicate that we are entering a measured controller action or task.
101
+ # Make sure you unwind every push with a pop call.
102
+ def push(m)
103
+ transaction_sampler.notice_first_scope_push(start)
104
+ sql_sampler.notice_first_scope_push(start)
105
+ @path_stack.push NewRelic::MetricParser::MetricParser.for_metric_named(m)
106
+ end
107
+
108
+ # Indicate that you don't want to keep the currently saved transaction
109
+ # information
110
+ def self.abort_transaction!
111
+ current.abort_transaction! if current
112
+ end
113
+
114
+ # For the current web transaction, return the path of the URI minus the host part and query string, or nil.
115
+ def uri
116
+ @uri ||= self.class.uri_from_request(@request) unless @request.nil?
117
+ end
118
+
119
+ # For the current web transaction, return the full referer, minus the host string, or nil.
120
+ def referer
121
+ @referer ||= self.class.referer_from_request(@request)
122
+ end
123
+
124
+ # Call this to ensure that the current transaction is not saved
125
+ def abort_transaction!
126
+ transaction_sampler.ignore_transaction
127
+ end
128
+ # This needs to be called after entering the call to trace the
129
+ # controller action, otherwise the controller action blames
130
+ # itself. It gets reset in the normal #pop call.
131
+ def start_transaction
132
+ agent.stats_engine.start_transaction metric_name
133
+ # Only push the transaction context info once, on entry:
134
+ if @path_stack.size == 1
135
+ transaction_sampler.notice_transaction(metric_name, uri, filtered_params)
136
+ sql_sampler.notice_transaction(metric_name, uri, filtered_params)
137
+ end
138
+ end
139
+
140
+ def current_metric
141
+ @path_stack.last
142
+ end
143
+
144
+ # Return the path, the part of the metric after the category
145
+ def path
146
+ @path_stack.last.last
147
+ end
148
+
149
+ # Unwind one stack level. It knows if it's back at the outermost caller and
150
+ # does the appropriate wrapup of the context.
151
+ def pop
152
+ metric = @path_stack.pop
153
+ log_underflow if metric.nil?
154
+ if @path_stack.empty?
155
+ handle_empty_path_stack(metric)
156
+ else
157
+ set_new_scope!(current_stack_metric)
158
+ end
159
+ end
160
+
161
+ # If we have an active metric frame, notice the error and increment the error metric.
162
+ # Options:
163
+ # * <tt>:request</tt> => Request object to get the uri and referer
164
+ # * <tt>:uri</tt> => The request path, minus any request params or query string.
165
+ # * <tt>:referer</tt> => The URI of the referer
166
+ # * <tt>:metric</tt> => The metric name associated with the transaction
167
+ # * <tt>:request_params</tt> => Request parameters, already filtered if necessary
168
+ # * <tt>:custom_params</tt> => Custom parameters
169
+ # Anything left over is treated as custom params
170
+
171
+ def self.notice_error(e, options={})
172
+ request = options.delete(:request)
173
+ if request
174
+ options[:referer] = referer_from_request(request)
175
+ options[:uri] = uri_from_request(request)
176
+ end
177
+ if current
178
+ current.notice_error(e, options)
179
+ else
180
+ agent.error_collector.notice_error(e, options)
181
+ end
182
+ end
183
+
184
+ # Do not call this. Invoke the class method instead.
185
+ def notice_error(e, options={}) # :nodoc:
186
+ params = custom_parameters
187
+ options[:referer] = referer if referer
188
+ options[:request_params] = filtered_params if filtered_params
189
+ options[:uri] = uri if uri
190
+ options[:metric] = metric_name
191
+ options.merge!(custom_parameters)
192
+ if exception != e
193
+ result = agent.error_collector.notice_error(e, options)
194
+ self.exception = result if result
195
+ end
196
+ end
197
+
198
+ # Add context parameters to the metric frame. This information will be passed in to errors
199
+ # and transaction traces. Keys and Values should be strings, numbers or date/times.
200
+ def self.add_custom_parameters(p)
201
+ current.add_custom_parameters(p) if current
202
+ end
203
+
204
+ def self.custom_parameters
205
+ (current && current.custom_parameters) ? current.custom_parameters : {}
206
+ end
207
+
208
+ def self.set_user_attributes(attributes)
209
+ current.set_user_attributes(attributes) if current
210
+ end
211
+
212
+ def self.user_attributes
213
+ (current) ? current.user_attributes : {}
214
+ end
215
+
216
+ def record_apdex()
217
+ return unless recording_web_transaction? && NewRelic::Agent.is_execution_traced?
218
+ t = Time.now
219
+ self.class.record_apdex(current_metric, t - start, t - apdex_start, !exception.nil?)
220
+ end
221
+
222
+ def metric_name
223
+ return nil if @path_stack.empty?
224
+ current_metric.name
225
+ end
226
+
227
+ # Return the array of metrics to record for the current metric frame.
228
+ def recorded_metrics
229
+ metrics = [ metric_name ]
230
+ metrics += current_metric.summary_metrics if @path_stack.size == 1
231
+ metrics
232
+ end
233
+
234
+ # Yield to a block that is run with a database metric name context. This means
235
+ # the Database instrumentation will use this for the metric name if it does not
236
+ # otherwise know about a model. This is re-entrant.
237
+ #
238
+ # * <tt>model</tt> is the DB model class
239
+ # * <tt>method</tt> is the name of the finder method or other method to identify the operation with.
240
+ #
241
+ def with_database_metric_name(model, method)
242
+ previous = @database_metric_name
243
+ model_name = case model
244
+ when Class
245
+ model.name
246
+ when String
247
+ model
248
+ else
249
+ model.to_s
250
+ end
251
+ @database_metric_name = "ActiveRecord/#{model_name}/#{method}"
252
+ yield
253
+ ensure
254
+ @database_metric_name=previous
255
+ end
256
+
257
+ def custom_parameters
258
+ @custom_parameters ||= {}
259
+ end
260
+
261
+ def user_attributes
262
+ @user_atrributes ||= {}
263
+ end
264
+
265
+ def queue_time
266
+ apdex_start - start
267
+ end
268
+
269
+ def add_custom_parameters(p)
270
+ custom_parameters.merge!(p)
271
+ end
272
+
273
+ def set_user_attributes(attributes)
274
+ user_attributes.merge!(attributes)
275
+ end
276
+
277
+ def self.recording_web_transaction?
278
+ c = Thread.current[:newrelic_metric_frame]
279
+ if c
280
+ c.recording_web_transaction?
281
+ end
282
+ end
283
+
284
+ def recording_web_transaction?
285
+ current_metric && current_metric.is_web_transaction?
286
+ end
287
+
288
+ def is_web_transaction?(metric)
289
+ 0 == metric.index("Controller")
290
+ end
291
+
292
+ # Make a safe attempt to get the referer from a request object, generally successful when
293
+ # it's a Rack request.
294
+ def self.referer_from_request(request)
295
+ if request && request.respond_to?(:referer)
296
+ request.referer.to_s.split('?').first
297
+ end
298
+ end
299
+
300
+ # Make a safe attempt to get the URI, without the host and query string.
301
+ def self.uri_from_request(request)
302
+ approximate_uri = case
303
+ when request.respond_to?(:fullpath) then request.fullpath
304
+ when request.respond_to?(:path) then request.path
305
+ when request.respond_to?(:request_uri) then request.request_uri
306
+ when request.respond_to?(:uri) then request.uri
307
+ when request.respond_to?(:url) then request.url
308
+ end
309
+ return approximate_uri[%r{^(https?://.*?)?(/[^?]*)}, 2] || '/' if approximate_uri # '
310
+ end
311
+
312
+ def self.record_apdex(current_metric, action_duration, total_duration, is_error)
313
+ summary_stat = agent.stats_engine.get_custom_stats("Apdex", NewRelic::ApdexStats)
314
+ controller_stat = agent.stats_engine.get_custom_stats(current_metric.apdex_metric_path, NewRelic::ApdexStats)
315
+ update_apdex(summary_stat, total_duration, is_error)
316
+ update_apdex(controller_stat, action_duration, is_error)
317
+ end
318
+
319
+ # Record an apdex value for the given stat. when `failed`
320
+ # the apdex should be recorded as a failure regardless of duration.
321
+ def self.update_apdex(stat, duration, failed)
322
+ duration = duration.to_f
323
+ apdex_t = NewRelic::Control.instance.apdex_t
324
+ case
325
+ when failed
326
+ stat.record_apdex_f
327
+ when duration <= apdex_t
328
+ stat.record_apdex_s
329
+ when duration <= 4 * apdex_t
330
+ stat.record_apdex_t
331
+ else
332
+ stat.record_apdex_f
333
+ end
334
+ end
335
+
336
+ private
337
+
338
+ def process_cpu
339
+ return nil if defined? JRuby
340
+ p = Process.times
341
+ p.stime + p.utime
342
+ end
343
+
344
+ def jruby_cpu_time # :nodoc:
345
+ return nil unless @@java_classes_loaded
346
+ threadMBean = ManagementFactory.getThreadMXBean()
347
+ java_utime = threadMBean.getCurrentThreadUserTime() # ns
348
+ -1 == java_utime ? 0.0 : java_utime/1e9
349
+ end
350
+ end
351
+ end
352
+ end
353
+ end