wd_newrelic_rpm 3.3.4.1 → 3.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (290) hide show
  1. data/.gitignore +20 -0
  2. data/.project +23 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +317 -39
  5. data/GUIDELINES_FOR_CONTRIBUTING.md +76 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE +1 -1
  8. data/{README.rdoc → README.md} +71 -55
  9. data/Rakefile +62 -0
  10. data/bin/mongrel_rpm +1 -1
  11. data/config.dot +290 -0
  12. data/config/database.yml +5 -0
  13. data/init.rb +31 -0
  14. data/lib/new_relic/agent.rb +31 -60
  15. data/lib/new_relic/agent/agent.rb +277 -502
  16. data/lib/new_relic/agent/agent_logger.rb +165 -0
  17. data/lib/new_relic/agent/audit_logger.rb +72 -0
  18. data/lib/new_relic/agent/beacon_configuration.rb +36 -50
  19. data/lib/new_relic/agent/browser_monitoring.rb +114 -61
  20. data/lib/new_relic/agent/busy_calculator.rb +14 -6
  21. data/lib/new_relic/agent/configuration.rb +74 -0
  22. data/lib/new_relic/agent/configuration/defaults.rb +126 -0
  23. data/lib/new_relic/agent/configuration/environment_source.rb +49 -0
  24. data/lib/new_relic/agent/configuration/manager.rb +136 -0
  25. data/lib/new_relic/agent/configuration/mask_defaults.rb +10 -0
  26. data/lib/new_relic/agent/configuration/server_source.rb +27 -0
  27. data/lib/new_relic/agent/configuration/yaml_source.rb +63 -0
  28. data/lib/new_relic/agent/cross_process_monitoring.rb +43 -0
  29. data/lib/new_relic/agent/database.rb +39 -26
  30. data/lib/new_relic/agent/error_collector.rb +48 -49
  31. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  32. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -7
  33. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +1 -1
  34. data/lib/new_relic/agent/instrumentation/authlogic.rb +1 -1
  35. data/lib/new_relic/agent/instrumentation/browser_monitoring_timings.rb +41 -0
  36. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +35 -12
  37. data/lib/new_relic/agent/instrumentation/data_mapper.rb +4 -12
  38. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +2 -2
  39. data/lib/new_relic/agent/instrumentation/memcache.rb +6 -6
  40. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  42. data/lib/new_relic/agent/instrumentation/metric_frame.rb +4 -14
  43. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -1
  44. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  45. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +6 -20
  46. data/lib/new_relic/agent/instrumentation/queue_time.rb +2 -2
  47. data/lib/new_relic/agent/instrumentation/rack.rb +1 -1
  48. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +7 -7
  49. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  50. data/lib/new_relic/agent/instrumentation/rails/errors.rb +1 -1
  51. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -5
  52. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +6 -2
  53. data/lib/new_relic/agent/instrumentation/resque.rb +81 -0
  54. data/lib/new_relic/agent/instrumentation/sinatra.rb +21 -10
  55. data/lib/new_relic/agent/instrumentation/sunspot.rb +1 -1
  56. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +5 -4
  57. data/lib/new_relic/agent/method_tracer.rb +11 -9
  58. data/lib/new_relic/agent/new_relic_service.rb +379 -0
  59. data/lib/new_relic/agent/pipe_channel_manager.rb +175 -0
  60. data/lib/new_relic/agent/pipe_service.rb +58 -0
  61. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +86 -0
  62. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -8
  63. data/lib/new_relic/agent/sql_sampler.rb +31 -74
  64. data/lib/new_relic/agent/stats_engine.rb +0 -5
  65. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +40 -24
  66. data/lib/new_relic/agent/stats_engine/metric_stats.rb +89 -14
  67. data/lib/new_relic/agent/stats_engine/samplers.rb +9 -7
  68. data/lib/new_relic/agent/stats_engine/transactions.rb +20 -12
  69. data/lib/new_relic/agent/thread.rb +32 -0
  70. data/lib/new_relic/agent/thread_profiler.rb +306 -0
  71. data/lib/new_relic/agent/transaction_info.rb +24 -4
  72. data/lib/new_relic/agent/transaction_sample_builder.rb +12 -13
  73. data/lib/new_relic/agent/transaction_sampler.rb +49 -65
  74. data/lib/new_relic/agent/worker_loop.rb +31 -25
  75. data/lib/new_relic/collection_helper.rb +1 -1
  76. data/lib/new_relic/commands/deployments.rb +19 -10
  77. data/lib/new_relic/control.rb +1 -3
  78. data/lib/new_relic/control/class_methods.rb +10 -5
  79. data/lib/new_relic/control/frameworks/merb.rb +0 -6
  80. data/lib/new_relic/control/frameworks/rails.rb +30 -45
  81. data/lib/new_relic/control/frameworks/rails3.rb +23 -18
  82. data/lib/new_relic/control/frameworks/rails4.rb +23 -0
  83. data/lib/new_relic/control/frameworks/ruby.rb +2 -23
  84. data/lib/new_relic/control/instance_methods.rb +35 -73
  85. data/lib/new_relic/control/instrumentation.rb +7 -12
  86. data/lib/new_relic/control/server_methods.rb +17 -19
  87. data/lib/new_relic/delayed_job_injection.rb +2 -2
  88. data/lib/new_relic/helper.rb +34 -0
  89. data/lib/new_relic/language_support.rb +56 -37
  90. data/lib/new_relic/local_environment.rb +32 -67
  91. data/lib/new_relic/metric_data.rb +10 -2
  92. data/lib/new_relic/metric_spec.rb +7 -3
  93. data/lib/new_relic/noticed_error.rb +32 -9
  94. data/lib/new_relic/rack.rb +4 -0
  95. data/lib/new_relic/rack/browser_monitoring.rb +34 -25
  96. data/lib/new_relic/rack/developer_mode.rb +3 -0
  97. data/lib/new_relic/rack/error_collector.rb +56 -0
  98. data/lib/new_relic/stats.rb +9 -7
  99. data/lib/new_relic/transaction_sample.rb +19 -18
  100. data/lib/new_relic/transaction_sample/segment.rb +13 -15
  101. data/lib/new_relic/version.rb +19 -3
  102. data/lib/newrelic_rpm.rb +1 -1
  103. data/lib/tasks/tests.rake +6 -8
  104. data/newrelic.yml +15 -32
  105. data/newrelic_rpm.gemspec +224 -43
  106. data/newrelic_rpm.gemspec.erb +54 -0
  107. data/test/config/newrelic.yml +4 -3
  108. data/test/config/test_control.rb +18 -18
  109. data/test/fixtures/gemspec_no_build.rb +442 -0
  110. data/test/fixtures/gemspec_with_build.rb +442 -0
  111. data/test/fixtures/gemspec_with_build_and_stage.rb +442 -0
  112. data/test/intentional_fail.rb +10 -0
  113. data/test/multiverse/.gitignore +10 -0
  114. data/test/multiverse/README.md +85 -0
  115. data/test/multiverse/lib/multiverse/color.rb +13 -0
  116. data/test/multiverse/lib/multiverse/envfile.rb +66 -0
  117. data/test/multiverse/lib/multiverse/environment.rb +16 -0
  118. data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
  119. data/test/multiverse/lib/multiverse/runner.rb +44 -0
  120. data/test/multiverse/lib/multiverse/suite.rb +162 -0
  121. data/test/multiverse/script/run_one +3 -0
  122. data/test/multiverse/script/runner +9 -0
  123. data/test/multiverse/suites/active_record/Envfile +13 -0
  124. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
  125. data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
  126. data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
  127. data/test/multiverse/suites/agent_only/Envfile +4 -0
  128. data/test/multiverse/suites/agent_only/audit_log_test.rb +99 -0
  129. data/test/multiverse/suites/agent_only/config/newrelic.yml +22 -0
  130. data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
  131. data/test/multiverse/suites/agent_only/marshaling_test.rb +109 -0
  132. data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
  133. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
  134. data/test/multiverse/suites/agent_only/service_timeout_test.rb +29 -0
  135. data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
  136. data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
  137. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +80 -0
  138. data/test/multiverse/suites/config_file_loading/Envfile +7 -0
  139. data/test/multiverse/suites/config_file_loading/config_file_loading_test.rb +106 -0
  140. data/test/multiverse/suites/datamapper/Envfile +8 -0
  141. data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
  142. data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
  143. data/test/multiverse/suites/logging/Envfile +4 -0
  144. data/test/multiverse/suites/logging/config/newrelic.yml +22 -0
  145. data/test/multiverse/suites/logging/logging_test.rb +143 -0
  146. data/test/multiverse/suites/monitor_mode_false/Envfile +2 -0
  147. data/test/multiverse/suites/monitor_mode_false/config/newrelic.yml +25 -0
  148. data/test/multiverse/suites/monitor_mode_false/no_dns_resolv.rb +29 -0
  149. data/test/multiverse/suites/no_load/Envfile +2 -0
  150. data/test/multiverse/suites/no_load/config/newrelic.yml +22 -0
  151. data/test/multiverse/suites/no_load/start_up_test.rb +14 -0
  152. data/test/multiverse/suites/rails_3_error_tracing/Envfile +15 -0
  153. data/test/multiverse/suites/rails_3_error_tracing/config/newrelic.yml +165 -0
  154. data/test/multiverse/suites/rails_3_error_tracing/error_tracing_test.rb +236 -0
  155. data/test/multiverse/suites/rails_3_gc/Envfile +8 -0
  156. data/test/multiverse/suites/rails_3_gc/config/newrelic.yml +167 -0
  157. data/test/multiverse/suites/rails_3_gc/instrumentation_test.rb +92 -0
  158. data/test/multiverse/suites/rails_3_queue_time/Envfile +15 -0
  159. data/test/multiverse/suites/rails_3_queue_time/config/newrelic.yml +165 -0
  160. data/test/multiverse/suites/rails_3_queue_time/queue_time_test.rb +75 -0
  161. data/test/multiverse/suites/rails_3_views/.gitignore +3 -0
  162. data/test/multiverse/suites/rails_3_views/Envfile +16 -0
  163. data/test/multiverse/suites/rails_3_views/app/views/foos/_foo.html.haml +1 -0
  164. data/test/multiverse/suites/rails_3_views/app/views/test/_a_partial.html.erb +1 -0
  165. data/test/multiverse/suites/rails_3_views/app/views/test/_mid_partial.html.erb +1 -0
  166. data/test/multiverse/suites/rails_3_views/app/views/test/_top_partial.html.erb +3 -0
  167. data/test/multiverse/suites/rails_3_views/app/views/test/deep_partial.html.erb +3 -0
  168. data/test/multiverse/suites/rails_3_views/app/views/test/haml_view.html.haml +6 -0
  169. data/test/multiverse/suites/rails_3_views/app/views/test/index.html.erb +4 -0
  170. data/test/multiverse/suites/rails_3_views/config/newrelic.yml +164 -0
  171. data/test/multiverse/suites/rails_3_views/view_instrumentation_test.rb +245 -0
  172. data/test/multiverse/suites/resque/Envfile +21 -0
  173. data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
  174. data/test/multiverse/suites/resque/dump.rdb +0 -0
  175. data/test/multiverse/suites/resque/instrumentation_test.rb +73 -0
  176. data/test/multiverse/suites/rum_auto_instrumentation/Envfile +4 -0
  177. data/test/multiverse/suites/rum_auto_instrumentation/config/newrelic.yml +24 -0
  178. data/test/multiverse/suites/rum_auto_instrumentation/responses/worst_case_small.html +5000 -0
  179. data/test/multiverse/suites/rum_auto_instrumentation/sanity_test.rb +102 -0
  180. data/test/multiverse/suites/sinatra/Envfile +13 -0
  181. data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
  182. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +77 -0
  183. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
  184. data/test/multiverse/suites/sinatra/sinatra_test.rb +55 -0
  185. data/test/multiverse/test/multiverse_test.rb +55 -0
  186. data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
  187. data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
  188. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
  189. data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
  190. data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
  191. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
  192. data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
  193. data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
  194. data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
  195. data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
  196. data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
  197. data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
  198. data/test/new_relic/agent/agent/connect_test.rb +151 -227
  199. data/test/new_relic/agent/agent/start_test.rb +68 -118
  200. data/test/new_relic/agent/agent/start_worker_thread_test.rb +12 -74
  201. data/test/new_relic/agent/agent_logger_test.rb +153 -0
  202. data/test/new_relic/agent/agent_test.rb +116 -30
  203. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  204. data/test/new_relic/agent/agent_test_controller_test.rb +42 -10
  205. data/test/new_relic/agent/audit_logger_test.rb +105 -0
  206. data/test/new_relic/agent/beacon_configuration_test.rb +63 -67
  207. data/test/new_relic/agent/browser_monitoring_test.rb +151 -79
  208. data/test/new_relic/agent/busy_calculator_test.rb +7 -0
  209. data/test/new_relic/agent/configuration/environment_source_test.rb +79 -0
  210. data/test/new_relic/agent/configuration/manager_test.rb +204 -0
  211. data/test/new_relic/agent/configuration/server_source_test.rb +45 -0
  212. data/test/new_relic/agent/configuration/yaml_source_test.rb +75 -0
  213. data/test/new_relic/agent/cross_process_monitoring_test.rb +77 -0
  214. data/test/new_relic/agent/database_test.rb +12 -11
  215. data/test/new_relic/agent/error_collector/notice_error_test.rb +64 -53
  216. data/test/new_relic/agent/error_collector_test.rb +33 -19
  217. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +60 -30
  218. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +39 -0
  219. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +1 -1
  220. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +6 -0
  221. data/test/new_relic/agent/instrumentation/queue_time_test.rb +14 -0
  222. data/test/new_relic/agent/instrumentation/sinatra_test.rb +25 -0
  223. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  224. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +4 -10
  225. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +3 -15
  226. data/test/new_relic/agent/method_tracer_test.rb +7 -6
  227. data/test/new_relic/agent/mock_scope_listener.rb +3 -0
  228. data/test/new_relic/agent/new_relic_service_test.rb +376 -0
  229. data/test/new_relic/agent/pipe_channel_manager_test.rb +131 -0
  230. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  231. data/test/new_relic/agent/rpm_agent_test.rb +27 -50
  232. data/test/new_relic/agent/sql_sampler_test.rb +81 -56
  233. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +3 -20
  234. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +12 -1
  235. data/test/new_relic/agent/stats_engine_test.rb +17 -8
  236. data/test/new_relic/agent/thread_profiler_test.rb +537 -0
  237. data/test/new_relic/agent/thread_test.rb +89 -0
  238. data/test/new_relic/agent/threaded_test.rb +65 -0
  239. data/test/new_relic/agent/transaction_info_test.rb +45 -4
  240. data/test/new_relic/agent/transaction_sample_builder_test.rb +8 -7
  241. data/test/new_relic/agent/transaction_sampler_test.rb +193 -204
  242. data/test/new_relic/agent/worker_loop_test.rb +26 -11
  243. data/test/new_relic/agent_test.rb +113 -33
  244. data/test/new_relic/collection_helper_test.rb +7 -8
  245. data/test/new_relic/command/deployments_test.rb +18 -2
  246. data/test/new_relic/control/class_methods_test.rb +0 -18
  247. data/test/new_relic/control/frameworks/rails_test.rb +26 -0
  248. data/test/new_relic/control_test.rb +96 -137
  249. data/test/new_relic/delayed_job_injection_test.rb +6 -1
  250. data/test/new_relic/dispatcher_test.rb +54 -0
  251. data/test/new_relic/fake_collector.rb +283 -0
  252. data/test/new_relic/fake_service.rb +53 -0
  253. data/test/new_relic/fakes_sending_data.rb +30 -0
  254. data/test/new_relic/framework_test.rb +53 -0
  255. data/test/new_relic/load_test.rb +13 -0
  256. data/test/new_relic/local_environment_test.rb +11 -11
  257. data/test/new_relic/metric_data_test.rb +45 -16
  258. data/test/new_relic/noticed_error_test.rb +24 -0
  259. data/test/new_relic/rack/browser_monitoring_test.rb +20 -10
  260. data/test/new_relic/rack/developer_mode_test.rb +13 -7
  261. data/test/new_relic/rack/error_collector_test.rb +74 -0
  262. data/test/new_relic/stats_test.rb +10 -0
  263. data/test/new_relic/transaction_sample/segment_test.rb +23 -4
  264. data/test/new_relic/transaction_sample_test.rb +47 -2
  265. data/test/new_relic/version_number_test.rb +32 -0
  266. data/test/script/build_test_gem.sh +9 -3
  267. data/test/script/ci.sh +108 -35
  268. data/test/script/ci_agent-tests_runner.sh +82 -0
  269. data/test/script/ci_multiverse_runner.sh +63 -0
  270. data/test/test_contexts.rb +1 -0
  271. data/test/test_helper.rb +68 -18
  272. data/ui/helpers/developer_mode_helper.rb +21 -11
  273. data/ui/views/layouts/newrelic_default.rhtml +1 -0
  274. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  275. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  276. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  277. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  278. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  279. data/ui/views/newrelic/show_sample.rhtml +1 -1
  280. data/ui/views/newrelic/threads.rhtml +2 -10
  281. data/vendor/gems/metric_parser-0.1.0.pre1/.specification +116 -0
  282. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_init.rb +7 -0
  283. metadata +191 -65
  284. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  285. data/lib/new_relic/control/configuration.rb +0 -206
  286. data/lib/new_relic/control/logging_methods.rb +0 -143
  287. data/lib/new_relic/data_serialization.rb +0 -151
  288. data/test/new_relic/control/configuration_test.rb +0 -84
  289. data/test/new_relic/control/logging_methods_test.rb +0 -185
  290. data/test/new_relic/data_serialization_test.rb +0 -208
@@ -16,11 +16,6 @@ module NewRelic
16
16
  Thread::current[:newrelic_scope_stack] = nil
17
17
  start_sampler_thread
18
18
  end
19
-
20
- def log
21
- NewRelic::Control.instance.log
22
- end
23
-
24
19
  end
25
20
  end
26
21
  end
@@ -7,12 +7,13 @@ module NewRelic
7
7
  @profiler = RailsBench.new if RailsBench.enabled?
8
8
  @profiler = Ruby19.new if Ruby19.enabled?
9
9
  @profiler = Rubinius.new if Rubinius.enabled?
10
+ @profiler = RubiniusAgent.new if RubiniusAgent.enabled?
10
11
  end
11
12
 
12
13
  def self.capture
13
14
  @profiler.capture if @profiler
14
15
  end
15
-
16
+
16
17
  class Profiler
17
18
  def initialize
18
19
  if self.class.enabled?
@@ -20,75 +21,76 @@ module NewRelic
20
21
  @last_count = call_count
21
22
  end
22
23
  end
23
-
24
+
24
25
  def capture
25
26
  return unless self.class.enabled?
26
27
  return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
27
-
28
+
28
29
  num_calls = call_count - @last_count
29
- elapsed = (call_time - @last_timestamp).to_f
30
+ # microseconds to seconds
31
+ elapsed = (call_time - @last_timestamp).to_f / 1_000_000.0
30
32
  @last_timestamp = call_time
31
33
  @last_count = call_count
32
34
  reset
33
-
35
+
34
36
  record_gc_metric(num_calls, elapsed)
37
+ elapsed
35
38
  end
36
39
 
37
40
  def reset; end
38
-
41
+
39
42
  protected
40
-
43
+
41
44
  def record_gc_metric(num_calls, elapsed)
42
45
  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
46
  # GC stats are collected into a blamed metric which allows
50
47
  # us to show the stats controller by controller
51
- gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
48
+ gc_stats = NewRelic::Agent.instance.stats_engine \
49
+ .get_stats('GC/cumulative', true, false,
50
+ NewRelic::Agent::TransactionInfo.get.transaction_name)
52
51
  gc_stats.record_multiple_data_points(elapsed, num_calls)
53
- NewRelic::Agent.instance.stats_engine.pop_scope(gc_scope, elapsed, time)
54
52
  end
55
53
  end
56
54
 
57
55
  def scope_stack
58
56
  Thread::current[:newrelic_scope_stack] ||= []
59
- end
57
+ end
60
58
  end
61
-
59
+
62
60
  class RailsBench < Profiler
63
61
  def self.enabled?
64
62
  ::GC.respond_to?(:time) && ::GC.respond_to?(:collections)
65
63
  end
66
-
64
+
67
65
  # microseconds spent in GC
68
66
  def call_time
69
67
  ::GC.time # this should already be microseconds
70
68
  end
71
-
69
+
72
70
  def call_count
73
71
  ::GC.collections
74
72
  end
73
+
74
+ def reset
75
+ ::GC.clear_stats
76
+ end
75
77
  end
76
-
78
+
77
79
  class Ruby19 < Profiler
78
80
  def self.enabled?
79
81
  defined?(::GC::Profiler) && ::GC::Profiler.enabled?
80
82
  end
81
-
83
+
82
84
  # microseconds spent in GC
83
85
  # 1.9 total_time returns seconds. Don't trust the docs. It's seconds.
84
86
  def call_time
85
87
  ::GC::Profiler.total_time * 1_000_000.0 # convert seconds to microseconds
86
88
  end
87
-
89
+
88
90
  def call_count
89
91
  ::GC.count
90
92
  end
91
-
93
+
92
94
  def reset
93
95
  ::GC::Profiler.clear
94
96
  @last_timestamp = 0
@@ -96,6 +98,20 @@ module NewRelic
96
98
  end
97
99
 
98
100
  class Rubinius < Profiler
101
+ def self.enabled?
102
+ defined?(::Rubinius) && defined?(::Rubinius::GC) && ::Rubinius::GC.respond_to?(:count)
103
+ end
104
+
105
+ def call_time
106
+ ::Rubinius::GC.time * 1000
107
+ end
108
+
109
+ def call_count
110
+ ::Rubinius::GC.count
111
+ end
112
+ end
113
+
114
+ class RubiniusAgent < Profiler
99
115
  def self.enabled?
100
116
  if NewRelic::LanguageSupport.using_engine?('rbx')
101
117
  require 'rubinius/agent'
@@ -115,7 +131,7 @@ module NewRelic
115
131
  agent = ::Rubinius::Agent.loopback
116
132
  agent.get('system.gc.young.count')[1] +
117
133
  agent.get('system.gc.full.count')[1]
118
- end
134
+ end
119
135
  end
120
136
  end
121
137
  end
@@ -8,34 +8,82 @@ module NewRelic
8
8
  # A simple mutex-synchronized hash to make sure our statistics
9
9
  # are internally consistent even in truly-threaded rubies like JRuby
10
10
  class SynchronizedHash < ::Hash
11
- include NewRelic::LanguageSupport::SynchronizedHash
12
-
11
+ attr_reader :lock
12
+
13
13
  def initialize
14
14
  @lock = Mutex.new
15
15
  end
16
-
16
+
17
+ def initialize_copy(old)
18
+ super
19
+ old.each do |key, value|
20
+ self.store(key, value.dup)
21
+ end
22
+ end
23
+
17
24
  def []=(*args)
18
25
  @lock.synchronize { super }
26
+ rescue => e
27
+ log_error(e)
19
28
  end
20
29
 
21
30
  def clear(*args)
22
31
  @lock.synchronize { super }
32
+ rescue => e
33
+ log_error(e)
23
34
  end
24
35
 
25
36
  def delete(*args)
26
37
  @lock.synchronize { super }
38
+ rescue => e
39
+ log_error(e)
27
40
  end
28
41
 
29
42
  def delete_if(*args)
30
43
  @lock.synchronize { super }
44
+ rescue => e
45
+ log_error(e)
46
+ end
47
+
48
+ def reset
49
+ values.each { |s| s.reset }
50
+ end
51
+
52
+ def log_error(e)
53
+ backtraces = Thread.list.map { |t| log_thread(t) }.join("\n\n")
54
+ ::NewRelic::Agent.logger.warn(
55
+ "SynchronizedHash failure: #{e.class.name}: #{e.message}\n#{backtraces}")
56
+ end
57
+
58
+ def log_thread(t)
59
+ # Ruby 1.8 doesn't expose backtrace properly, so make sure it's there
60
+ if t.nil? || !t.respond_to?(:backtrace) || t.backtrace.nil?
61
+ return "#{t}\n\tNo backtrace for thread"
62
+ end
63
+
64
+ backtrace = t.backtrace.map { |b| "\t#{b}" }.join("\n")
65
+ "\t#{t}\n#{backtrace}"
66
+
67
+ rescue Exception => e
68
+ # JRuby 1.7.0 has a nasty habit of raising a
69
+ # java.lang.NullPointerException when we iterate through threads
70
+ # asking for backtraces. This line allows us to swallow java
71
+ # exceptions without referencing their classes (since they don't
72
+ # exist in MRI). It also prevents us from swallowing signals or
73
+ # other nasty things that can happen when you rescue Exception.
74
+ ::NewRelic::Agent.logger.warn(
75
+ "Error collecting thread backtraces: #{e.class.name}: #{e.message}")
76
+ ::NewRelic::Agent.logger.debug( e.backtrace.join("\n") )
77
+
78
+ raise e if e.class.ancestors.include? Exception
31
79
  end
32
80
  end
33
-
81
+
34
82
  # Returns all of the metric names of all the stats in the engine
35
83
  def metrics
36
84
  stats_hash.keys.map(&:to_s)
37
85
  end
38
-
86
+
39
87
  # a simple accessor for looking up a stat with no scope -
40
88
  # returns a new stats object if no stats object for that
41
89
  # metric exists yet
@@ -64,17 +112,43 @@ module NewRelic
64
112
  end
65
113
  stats
66
114
  end
67
-
115
+
68
116
  # Returns a stat if one exists, otherwise returns nil. If you
69
117
  # want auto-initialization, use one of get_stats or get_stats_no_scope
70
118
  def lookup_stats(metric_name, scope_name = '')
71
119
  stats_hash[NewRelic::MetricSpec.new(metric_name, scope_name)]
72
120
  end
73
-
121
+
122
+
123
+ # Helper method for timing supportability metrics
124
+ def record_supportability_metrics_timed(metrics)
125
+ start_time = Time.now
126
+ yield
127
+ end_time = Time.now
128
+ duration = (end_time - start_time).to_f
129
+ ensure
130
+ record_supportability_metrics(duration, metrics) do |value, metric|
131
+ metric.record_data_point(value)
132
+ end
133
+ end
134
+
135
+ # Helper for recording a straight value into the count
136
+ def record_supportability_metrics_count(value, *metrics)
137
+ record_supportability_metrics(value, *metrics) do |value, metric|
138
+ metric.call_count = value
139
+ end
140
+ end
141
+
142
+ # Helper method for recording supportability metrics consistently
143
+ def record_supportability_metrics(value, *metrics)
144
+ metrics.each do |metric|
145
+ yield(value, get_stats_no_scope("Supportability/#{metric}"))
146
+ end
147
+ end
148
+
74
149
  # This module was extracted from the harvest method and should
75
150
  # be refactored
76
151
  module Harvest
77
-
78
152
  # merge data from previous harvests into this stats engine -
79
153
  # takes into account the case where there are new stats for
80
154
  # that metric, and the case where there is no current data
@@ -135,10 +209,12 @@ module NewRelic
135
209
 
136
210
  def merge_stats(other_engine_or_hash, metric_ids)
137
211
  old_data = get_stats_hash_from(other_engine_or_hash)
138
-
139
212
  timeslice_data = {}
140
- stats_hash.each do | metric_spec, stats |
141
-
213
+ stats_hash.lock.synchronize do
214
+ Thread.current['newrelic_stats_hash'] = stats_hash.clone
215
+ stats_hash.reset
216
+ end
217
+ Thread.current['newrelic_stats_hash'].each do |metric_spec, stats|
142
218
  metric_spec = coerce_to_metric_spec(metric_spec)
143
219
  stats_copy = clone_and_reset_stats(metric_spec, stats)
144
220
  merge_old_data!(metric_spec, stats_copy, old_data)
@@ -160,7 +236,6 @@ module NewRelic
160
236
  # sacrificing efficiency.
161
237
  # +++
162
238
  def harvest_timeslice_data(previous_timeslice_data, metric_ids)
163
-
164
239
  poll harvest_samplers
165
240
  merge_stats(previous_timeslice_data, metric_ids)
166
241
  end
@@ -173,9 +248,9 @@ module NewRelic
173
248
 
174
249
  # Reset each of the stats, such as when a new passenger instance starts up.
175
250
  def reset_stats
176
- stats_hash.values.each { |s| s.reset }
251
+ stats_hash.reset
177
252
  end
178
-
253
+
179
254
  # returns a memoized SynchronizedHash that holds the actual
180
255
  # instances of Stats keyed off their MetricName
181
256
  def stats_hash
@@ -27,15 +27,19 @@ module Agent
27
27
  # start up a thread that will periodically poll for metric samples
28
28
  return if periodic_samplers.empty?
29
29
 
30
- @sampler_thread = Thread.new do
31
- while true do
30
+ @sampler_thread = NewRelic::Agent::AgentThread.new('Sampler Tasks') do
31
+ loop do
32
+ now = Time.now
32
33
  begin
33
34
  sleep POLL_PERIOD
34
35
  poll periodic_samplers
36
+ ensure
37
+ NewRelic::Agent.instance.stats_engine \
38
+ .get_stats_no_scope('Supportability/Samplers') \
39
+ .record_data_point((Time.now - now).to_f)
35
40
  end
36
41
  end
37
42
  end
38
- @sampler_thread['newrelic_label'] = 'Sampler Tasks'
39
43
  end
40
44
 
41
45
  private
@@ -47,7 +51,7 @@ module Agent
47
51
  end
48
52
 
49
53
  def log_added_sampler(type, sampler)
50
- log.debug "Adding #{type} sampler: #{sampler.inspect}"
54
+ ::NewRelic::Agent.logger.debug "Adding #{type} sampler: #{sampler.id}"
51
55
  end
52
56
 
53
57
  public
@@ -75,9 +79,7 @@ module Agent
75
79
  sampled_item.poll
76
80
  false # it's okay. don't delete it.
77
81
  rescue => e
78
- log.error "Removing #{sampled_item} from list"
79
- log.error e
80
- log.debug e.backtrace.to_s
82
+ ::NewRelic::Agent.logger.warn "Removing #{sampled_item} from list", e
81
83
  true # remove the sampler
82
84
  end
83
85
  end
@@ -13,11 +13,11 @@ module Agent
13
13
  @children_time = 0
14
14
  end
15
15
  end
16
-
16
+
17
17
  # Handles pushing and popping elements onto an internal stack that
18
18
  # tracks where time should be allocated in Transaction Traces
19
19
  module Transactions
20
-
20
+
21
21
  # Defines methods that stub out the stats engine methods
22
22
  # when the agent is disabled
23
23
  module Shim # :nodoc:
@@ -29,34 +29,32 @@ module Agent
29
29
  def scope_name; end
30
30
  def pop_scope(*args); end
31
31
  end
32
-
32
+
33
33
  # add a new transaction sampler, unless we're currently in a
34
34
  # transaction (then we fail)
35
35
  def transaction_sampler= sampler
36
36
  fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
37
37
  @transaction_sampler = sampler
38
38
  end
39
-
39
+
40
40
  # removes a transaction sampler
41
41
  def remove_transaction_sampler(l)
42
42
  @transaction_sampler = nil
43
43
  end
44
-
44
+
45
45
  # Pushes a scope onto the transaction stack - this generates a
46
46
  # TransactionSample::Segment at the end of transaction execution
47
47
  def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
48
48
  stack = scope_stack
49
- stack.empty? ? GCProfiler.init : GCProfiler.capture
50
- @transaction_sampler.notice_push_scope metric, time if @transaction_sampler
49
+ @transaction_sampler.notice_push_scope metric, time if sampler_enabled?
51
50
  scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
52
51
  stack.push scope
53
52
  scope
54
53
  end
55
-
54
+
56
55
  # Pops a scope off the transaction stack - this updates the
57
56
  # transaction sampler that we've finished execution of a traced method
58
57
  def pop_scope(expected_scope, duration, time=Time.now.to_f)
59
- GCProfiler.capture
60
58
  stack = scope_stack
61
59
  scope = stack.pop
62
60
  fail "unbalanced pop from blame stack, got #{scope ? scope.name : 'nil'}, expected #{expected_scope ? expected_scope.name : 'nil'}" if scope != expected_scope
@@ -68,10 +66,14 @@ module Agent
68
66
  stack.last.children_time += scope.children_time
69
67
  end
70
68
  end
71
- @transaction_sampler.notice_pop_scope(scope.name, time) if @transaction_sampler
69
+ @transaction_sampler.notice_pop_scope(scope.name, time) if sampler_enabled?
72
70
  scope
73
71
  end
74
72
 
73
+ def sampler_enabled?
74
+ @transaction_sampler && Agent.config[:'transaction_tracer.enabled']
75
+ end
76
+
75
77
  # Returns the latest ScopeStackElement
76
78
  def peek_scope
77
79
  scope_stack.last
@@ -88,7 +90,7 @@ module Agent
88
90
  def scope_name=(transaction)
89
91
  Thread::current[:newrelic_scope_name] = transaction
90
92
  end
91
-
93
+
92
94
  # Returns the current scope name from the thread local
93
95
  def scope_name
94
96
  Thread::current[:newrelic_scope_name]
@@ -98,6 +100,7 @@ module Agent
98
100
  def start_transaction(name = nil)
99
101
  Thread::current[:newrelic_scope_stack] ||= []
100
102
  self.scope_name = name if name
103
+ GCProfiler.init
101
104
  end
102
105
 
103
106
  # Try to clean up gracefully, otherwise we leave things hanging around on thread locals.
@@ -105,6 +108,11 @@ module Agent
105
108
  # and is ignored.
106
109
  #
107
110
  def end_transaction
111
+ elapsed = GCProfiler.capture
112
+ if @transaction_sampler && @transaction_sampler.last_sample
113
+ @transaction_sampler.last_sample.params[:custom_params] ||= {}
114
+ @transaction_sampler.last_sample.params[:custom_params][:gc_time] = elapsed
115
+ end
108
116
  stack = scope_stack
109
117
 
110
118
  if stack && stack.empty?
@@ -114,7 +122,7 @@ module Agent
114
122
  end
115
123
 
116
124
  private
117
-
125
+
118
126
  # Returns the current scope stack, memoized to a thread local variable
119
127
  def scope_stack
120
128
  Thread::current[:newrelic_scope_stack] ||= []