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,26 @@
1
+ require 'new_relic/agent/stats_engine/metric_stats'
2
+ require 'new_relic/agent/stats_engine/samplers'
3
+ require 'new_relic/agent/stats_engine/transactions'
4
+ require 'new_relic/agent/stats_engine/gc_profiler'
5
+
6
+ module NewRelic
7
+ module Agent
8
+ # This class handles all the statistics gathering for the agent
9
+ class StatsEngine
10
+ include MetricStats
11
+ include Samplers
12
+ include Transactions
13
+
14
+ def initialize
15
+ # Makes the unit tests happy
16
+ Thread::current[:newrelic_scope_stack] = nil
17
+ start_sampler_thread
18
+ end
19
+
20
+ def log
21
+ NewRelic::Control.instance.log
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,123 @@
1
+ # -*- coding: utf-8 -*-
2
+ module NewRelic
3
+ module Agent
4
+ class StatsEngine
5
+ module GCProfiler
6
+ def self.init
7
+ @profiler = RailsBench.new if RailsBench.enabled?
8
+ @profiler = Ruby19.new if Ruby19.enabled?
9
+ @profiler = Rubinius.new if Rubinius.enabled?
10
+ end
11
+
12
+ def self.capture
13
+ @profiler.capture if @profiler
14
+ end
15
+
16
+ class Profiler
17
+ def initialize
18
+ if self.class.enabled?
19
+ @last_timestamp = call_time
20
+ @last_count = call_count
21
+ end
22
+ end
23
+
24
+ def capture
25
+ return unless self.class.enabled?
26
+ return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
27
+
28
+ num_calls = call_count - @last_count
29
+ elapsed = (call_time - @last_timestamp).to_f
30
+ @last_timestamp = call_time
31
+ @last_count = call_count
32
+ reset
33
+
34
+ record_gc_metric(num_calls, elapsed)
35
+ end
36
+
37
+ def reset; end
38
+
39
+ protected
40
+
41
+ def record_gc_metric(num_calls, elapsed)
42
+ if num_calls > 0
43
+ # microseconds to seconds
44
+ elapsed = elapsed / 1_000_000.0
45
+ # Allocate the GC time to a scope as if the GC just ended
46
+ # right now.
47
+ time = Time.now.to_f
48
+ gc_scope = NewRelic::Agent.instance.stats_engine.push_scope("GC/cumulative", time - elapsed)
49
+ # GC stats are collected into a blamed metric which allows
50
+ # us to show the stats controller by controller
51
+ gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
52
+ gc_stats.record_multiple_data_points(elapsed, num_calls)
53
+ NewRelic::Agent.instance.stats_engine.pop_scope(gc_scope, elapsed, time)
54
+ end
55
+ end
56
+
57
+ def scope_stack
58
+ Thread::current[:newrelic_scope_stack] ||= []
59
+ end
60
+ end
61
+
62
+ class RailsBench < Profiler
63
+ def self.enabled?
64
+ ::GC.respond_to?(:time) && ::GC.respond_to?(:collections)
65
+ end
66
+
67
+ # microseconds spent in GC
68
+ def call_time
69
+ ::GC.time # this should already be microseconds
70
+ end
71
+
72
+ def call_count
73
+ ::GC.collections
74
+ end
75
+ end
76
+
77
+ class Ruby19 < Profiler
78
+ def self.enabled?
79
+ defined?(::GC::Profiler) && ::GC::Profiler.enabled?
80
+ end
81
+
82
+ # microseconds spent in GC
83
+ # 1.9 total_time returns seconds. Don't trust the docs. It's seconds.
84
+ def call_time
85
+ ::GC::Profiler.total_time * 1_000_000.0 # convert seconds to microseconds
86
+ end
87
+
88
+ def call_count
89
+ ::GC.count
90
+ end
91
+
92
+ def reset
93
+ ::GC::Profiler.clear
94
+ @last_timestamp = 0
95
+ end
96
+ end
97
+
98
+ class Rubinius < Profiler
99
+ def self.enabled?
100
+ if NewRelic::LanguageSupport.using_engine?('rbx')
101
+ require 'rubinius/agent'
102
+ true
103
+ else
104
+ false
105
+ end
106
+ end
107
+
108
+ def call_time
109
+ agent = ::Rubinius::Agent.loopback
110
+ (agent.get('system.gc.young.total_wallclock')[1] +
111
+ agent.get('system.gc.full.total_wallclock')[1]) * 1000
112
+ end
113
+
114
+ def call_count
115
+ agent = ::Rubinius::Agent.loopback
116
+ agent.get('system.gc.young.count')[1] +
117
+ agent.get('system.gc.full.count')[1]
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,187 @@
1
+ require 'new_relic/language_support'
2
+
3
+ module NewRelic
4
+ module Agent
5
+ class StatsEngine
6
+ # Handles methods related to actual Metric collection
7
+ module MetricStats
8
+ # A simple mutex-synchronized hash to make sure our statistics
9
+ # are internally consistent even in truly-threaded rubies like JRuby
10
+ class SynchronizedHash < ::Hash
11
+ include NewRelic::LanguageSupport::SynchronizedHash
12
+
13
+ def initialize
14
+ @lock = Mutex.new
15
+ end
16
+
17
+ def []=(*args)
18
+ @lock.synchronize { super }
19
+ end
20
+
21
+ def clear(*args)
22
+ @lock.synchronize { super }
23
+ end
24
+
25
+ def delete(*args)
26
+ @lock.synchronize { super }
27
+ end
28
+
29
+ def delete_if(*args)
30
+ @lock.synchronize { super }
31
+ end
32
+ end
33
+
34
+ # Returns all of the metric names of all the stats in the engine
35
+ def metrics
36
+ stats_hash.keys.map(&:to_s)
37
+ end
38
+
39
+ # a simple accessor for looking up a stat with no scope -
40
+ # returns a new stats object if no stats object for that
41
+ # metric exists yet
42
+ def get_stats_no_scope(metric_name)
43
+ stats_hash[NewRelic::MetricSpec.new(metric_name, '')] ||= NewRelic::MethodTraceStats.new
44
+ end
45
+
46
+ # This version allows a caller to pass a stat class to use
47
+ def get_custom_stats(metric_name, stat_class)
48
+ stats_hash[NewRelic::MetricSpec.new(metric_name)] ||= stat_class.new
49
+ end
50
+
51
+ # If use_scope is true, two chained metrics are created, one with scope and one without
52
+ # If scoped_metric_only is true, only a scoped metric is created (used by rendering metrics which by definition are per controller only)
53
+ def get_stats(metric_name, use_scope = true, scoped_metric_only = false, scope = nil)
54
+ scope ||= scope_name if use_scope
55
+ if scoped_metric_only
56
+ spec = NewRelic::MetricSpec.new metric_name, scope
57
+ stats = stats_hash[spec] ||= NewRelic::MethodTraceStats.new
58
+ else
59
+ stats = stats_hash[NewRelic::MetricSpec.new(metric_name)] ||= NewRelic::MethodTraceStats.new
60
+ if scope && scope != metric_name
61
+ spec = NewRelic::MetricSpec.new metric_name, scope
62
+ stats = stats_hash[spec] ||= NewRelic::ScopedMethodTraceStats.new(stats)
63
+ end
64
+ end
65
+ stats
66
+ end
67
+
68
+ # Returns a stat if one exists, otherwise returns nil. If you
69
+ # want auto-initialization, use one of get_stats or get_stats_no_scope
70
+ def lookup_stats(metric_name, scope_name = '')
71
+ stats_hash[NewRelic::MetricSpec.new(metric_name, scope_name)]
72
+ end
73
+
74
+ # This module was extracted from the harvest method and should
75
+ # be refactored
76
+ module Harvest
77
+
78
+ # merge data from previous harvests into this stats engine -
79
+ # takes into account the case where there are new stats for
80
+ # that metric, and the case where there is no current data
81
+ # for that metric
82
+ def merge_data(metric_data_hash)
83
+ metric_data_hash.each do |metric_spec, metric_data|
84
+ new_data = lookup_stats(metric_spec.name, metric_spec.scope)
85
+ if new_data
86
+ new_data.merge!(metric_data.stats)
87
+ else
88
+ stats_hash[metric_spec] = metric_data.stats
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+ def get_stats_hash_from(engine_or_hash)
95
+ if engine_or_hash.is_a?(StatsEngine)
96
+ engine_or_hash.stats_hash
97
+ else
98
+ engine_or_hash
99
+ end
100
+ end
101
+
102
+ def coerce_to_metric_spec(metric_spec)
103
+ if metric_spec.is_a?(NewRelic::MetricSpec)
104
+ metric_spec
105
+ else
106
+ NewRelic::MetricSpec.new(metric_spec)
107
+ end
108
+ end
109
+
110
+ def clone_and_reset_stats(metric_spec, stats)
111
+ if stats.nil?
112
+ raise "Nil stats for #{metric_spec.name} (#{metric_spec.scope})"
113
+ end
114
+
115
+ stats_copy = stats.clone
116
+ stats.reset
117
+ stats_copy
118
+ end
119
+
120
+ # if the previous timeslice data has not been reported (due to an error of some sort)
121
+ # then we need to merge this timeslice with the previously accumulated - but not sent
122
+ # data
123
+ def merge_old_data!(metric_spec, stats, old_data)
124
+ metric_data = old_data[metric_spec]
125
+ stats.merge!(metric_data.stats) unless metric_data.nil?
126
+ end
127
+
128
+ def add_data_to_send_unless_empty(data, stats, metric_spec, id)
129
+ # don't bother collecting and reporting stats that have
130
+ # zero-values for this timeslice. significant
131
+ # performance boost and storage savings.
132
+ return if stats.is_reset?
133
+ data[metric_spec] = NewRelic::MetricData.new((id ? nil : metric_spec), stats, id)
134
+ end
135
+
136
+ def merge_stats(other_engine_or_hash, metric_ids)
137
+ old_data = get_stats_hash_from(other_engine_or_hash)
138
+
139
+ timeslice_data = {}
140
+ stats_hash.each do | metric_spec, stats |
141
+
142
+ metric_spec = coerce_to_metric_spec(metric_spec)
143
+ stats_copy = clone_and_reset_stats(metric_spec, stats)
144
+ merge_old_data!(metric_spec, stats_copy, old_data)
145
+ add_data_to_send_unless_empty(timeslice_data, stats_copy, metric_spec, metric_ids[metric_spec])
146
+ end
147
+ timeslice_data
148
+ end
149
+
150
+ end
151
+ include Harvest
152
+
153
+ # Harvest the timeslice data. First recombine current statss
154
+ # with any previously
155
+ # unsent metrics, clear out stats cache, and return the current
156
+ # stats.
157
+ # ---
158
+ # Note: this is not synchronized. There is still some risk in this and
159
+ # we will revisit later to see if we can make this more robust without
160
+ # sacrificing efficiency.
161
+ # +++
162
+ def harvest_timeslice_data(previous_timeslice_data, metric_ids)
163
+
164
+ poll harvest_samplers
165
+ merge_stats(previous_timeslice_data, metric_ids)
166
+ end
167
+
168
+ # Remove all stats. For test code only.
169
+ def clear_stats
170
+ @stats_hash = SynchronizedHash.new
171
+ NewRelic::Agent::BusyCalculator.reset
172
+ end
173
+
174
+ # Reset each of the stats, such as when a new passenger instance starts up.
175
+ def reset_stats
176
+ stats_hash.values.each { |s| s.reset }
177
+ end
178
+
179
+ # returns a memoized SynchronizedHash that holds the actual
180
+ # instances of Stats keyed off their MetricName
181
+ def stats_hash
182
+ @stats_hash ||= SynchronizedHash.new
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,95 @@
1
+ module NewRelic
2
+ module Agent
3
+ class StatsEngine
4
+ module Shim # :nodoc:
5
+ def add_sampler(*args); end
6
+ def add_harvest_sampler(*args); end
7
+ def start_sampler_thread(*args); end
8
+ end
9
+
10
+ # Contains statistics engine extensions to support the concept of samplers
11
+ module Samplers
12
+
13
+ # By default a sampler polls on harvest time, once a minute. However you can
14
+ # override #use_harvest_sampler? to return false and it will sample
15
+ # every POLL_PERIOD seconds on a background thread.
16
+ POLL_PERIOD = 20
17
+
18
+ # starts the sampler thread which runs periodically, rather than
19
+ # at harvest time. This is deprecated, and should not actually
20
+ # be used - mo threads mo problems
21
+ #
22
+ # returns unless there are actually periodic samplers to run
23
+ def start_sampler_thread
24
+
25
+ return if @sampler_thread && @sampler_thread.alive?
26
+
27
+ # start up a thread that will periodically poll for metric samples
28
+ return if periodic_samplers.empty?
29
+
30
+ @sampler_thread = Thread.new do
31
+ while true do
32
+ begin
33
+ sleep POLL_PERIOD
34
+ poll periodic_samplers
35
+ end
36
+ end
37
+ end
38
+ @sampler_thread['newrelic_label'] = 'Sampler Tasks'
39
+ end
40
+
41
+ private
42
+
43
+ def add_sampler_to(sampler_array, sampler)
44
+ raise "Sampler #{sampler.inspect} is already registered. Don't call add_sampler directly anymore." if sampler_array.include?(sampler)
45
+ sampler_array << sampler
46
+ sampler.stats_engine = self
47
+ end
48
+
49
+ def log_added_sampler(type, sampler)
50
+ log.debug "Adding #{type} sampler: #{sampler.inspect}"
51
+ end
52
+
53
+ public
54
+
55
+ # Add an instance of Sampler to be invoked about every 10 seconds on a background
56
+ # thread.
57
+ def add_sampler(sampler)
58
+ add_sampler_to(periodic_samplers, sampler)
59
+ log_added_sampler('periodic', sampler)
60
+ end
61
+
62
+ # Add a sampler to be invoked just before each harvest.
63
+ def add_harvest_sampler(sampler)
64
+ add_sampler_to(harvest_samplers, sampler)
65
+ log_added_sampler('harvest-time', sampler)
66
+ end
67
+
68
+ private
69
+
70
+ # Call poll on each of the samplers. Remove
71
+ # the sampler if it raises.
72
+ def poll(samplers)
73
+ samplers.delete_if do |sampled_item|
74
+ begin
75
+ sampled_item.poll
76
+ false # it's okay. don't delete it.
77
+ rescue => e
78
+ log.error "Removing #{sampled_item} from list"
79
+ log.error e
80
+ log.debug e.backtrace.to_s
81
+ true # remove the sampler
82
+ end
83
+ end
84
+ end
85
+
86
+ def harvest_samplers
87
+ @harvest_samplers ||= []
88
+ end
89
+ def periodic_samplers
90
+ @periodic_samplers ||= []
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,125 @@
1
+ # -*- coding: utf-8 -*-
2
+ module NewRelic
3
+ module Agent
4
+ class StatsEngine
5
+ # A simple stack element that tracks the current name and length
6
+ # of the executing stack
7
+ class ScopeStackElement
8
+ attr_reader :name, :deduct_call_time_from_parent
9
+ attr_accessor :children_time
10
+ def initialize(name, deduct_call_time)
11
+ @name = name
12
+ @deduct_call_time_from_parent = deduct_call_time
13
+ @children_time = 0
14
+ end
15
+ end
16
+
17
+ # Handles pushing and popping elements onto an internal stack that
18
+ # tracks where time should be allocated in Transaction Traces
19
+ module Transactions
20
+
21
+ # Defines methods that stub out the stats engine methods
22
+ # when the agent is disabled
23
+ module Shim # :nodoc:
24
+ def start_transaction(*args); end
25
+ def end_transaction; end
26
+ def push_scope(*args); end
27
+ def transaction_sampler=(*args); end
28
+ def scope_name=(*args); end
29
+ def scope_name; end
30
+ def pop_scope(*args); end
31
+ end
32
+
33
+ # add a new transaction sampler, unless we're currently in a
34
+ # transaction (then we fail)
35
+ def transaction_sampler= sampler
36
+ fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
37
+ @transaction_sampler = sampler
38
+ end
39
+
40
+ # removes a transaction sampler
41
+ def remove_transaction_sampler(l)
42
+ @transaction_sampler = nil
43
+ end
44
+
45
+ # Pushes a scope onto the transaction stack - this generates a
46
+ # TransactionSample::Segment at the end of transaction execution
47
+ def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
48
+ stack = scope_stack
49
+ stack.empty? ? GCProfiler.init : GCProfiler.capture
50
+ @transaction_sampler.notice_push_scope metric, time if @transaction_sampler
51
+ scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
52
+ stack.push scope
53
+ scope
54
+ end
55
+
56
+ # Pops a scope off the transaction stack - this updates the
57
+ # transaction sampler that we've finished execution of a traced method
58
+ def pop_scope(expected_scope, duration, time=Time.now.to_f)
59
+ GCProfiler.capture
60
+ stack = scope_stack
61
+ scope = stack.pop
62
+ fail "unbalanced pop from blame stack, got #{scope ? scope.name : 'nil'}, expected #{expected_scope ? expected_scope.name : 'nil'}" if scope != expected_scope
63
+
64
+ if !stack.empty?
65
+ if scope.deduct_call_time_from_parent
66
+ stack.last.children_time += duration
67
+ else
68
+ stack.last.children_time += scope.children_time
69
+ end
70
+ end
71
+ @transaction_sampler.notice_pop_scope(scope.name, time) if @transaction_sampler
72
+ scope
73
+ end
74
+
75
+ # Returns the latest ScopeStackElement
76
+ def peek_scope
77
+ scope_stack.last
78
+ end
79
+
80
+ # set the name of the transaction for the current thread, which will be used
81
+ # to define the scope of all traced methods called on this thread until the
82
+ # scope stack is empty.
83
+ #
84
+ # currently the transaction name is the name of the controller action that
85
+ # is invoked via the dispatcher, but conceivably we could use other transaction
86
+ # names in the future if the traced application does more than service http request
87
+ # via controller actions
88
+ def scope_name=(transaction)
89
+ Thread::current[:newrelic_scope_name] = transaction
90
+ end
91
+
92
+ # Returns the current scope name from the thread local
93
+ def scope_name
94
+ Thread::current[:newrelic_scope_name]
95
+ end
96
+
97
+ # Start a new transaction, unless one is already in progress
98
+ def start_transaction(name = nil)
99
+ Thread::current[:newrelic_scope_stack] ||= []
100
+ self.scope_name = name if name
101
+ end
102
+
103
+ # Try to clean up gracefully, otherwise we leave things hanging around on thread locals.
104
+ # If it looks like a transaction is still in progress, then maybe this is an inner transaction
105
+ # and is ignored.
106
+ #
107
+ def end_transaction
108
+ stack = scope_stack
109
+
110
+ if stack && stack.empty?
111
+ Thread::current[:newrelic_scope_stack] = nil
112
+ Thread::current[:newrelic_scope_name] = nil
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ # Returns the current scope stack, memoized to a thread local variable
119
+ def scope_stack
120
+ Thread::current[:newrelic_scope_stack] ||= []
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end