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
@@ -0,0 +1,175 @@
1
+ require 'base64'
2
+
3
+ module NewRelic
4
+ module Agent
5
+
6
+ #--
7
+ # Manages the registering and servicing of pipes used by child
8
+ # processes to report data to their parent, rather than directly
9
+ # to the collector.
10
+ module PipeChannelManager
11
+ extend self
12
+
13
+ def register_report_channel(id)
14
+ listener.register_pipe(id)
15
+ end
16
+
17
+ def channels
18
+ listener.pipes
19
+ end
20
+
21
+ def listener
22
+ @listener ||= Listener.new
23
+ end
24
+
25
+ class Pipe
26
+ attr_accessor :in, :out
27
+ attr_reader :last_read
28
+
29
+ def initialize
30
+ @out, @in = IO.pipe
31
+ if defined?(::Encoding::ASCII_8BIT)
32
+ @in.set_encoding(::Encoding::ASCII_8BIT)
33
+ end
34
+ @last_read = Time.now
35
+ end
36
+
37
+ def close
38
+ @out.close unless @out.closed?
39
+ @in.close unless @in.closed?
40
+ end
41
+
42
+ def write(data)
43
+ @out.close unless @out.closed?
44
+ @in << NewRelic::LanguageSupport.with_cautious_gc do
45
+ Marshal.dump(data)
46
+ end
47
+ @in << "\n\n"
48
+ end
49
+
50
+ def read
51
+ @in.close unless @in.closed?
52
+ @last_read = Time.now
53
+ @out.gets("\n\n")
54
+ end
55
+
56
+ def closed?
57
+ @out.closed? && @in.closed?
58
+ end
59
+ end
60
+
61
+ class Listener
62
+ attr_reader :thread
63
+ attr_accessor :pipes, :timeout, :select_timeout
64
+
65
+ def initialize
66
+ @pipes = {}
67
+ @timeout = 360
68
+ @select_timeout = 60
69
+ end
70
+
71
+ def register_pipe(id)
72
+ @pipes[id] = Pipe.new
73
+ wake.in << '.'
74
+ end
75
+
76
+ def start
77
+ return if @started == true
78
+ @started = true
79
+ @thread = NewRelic::Agent::AgentThread.new('Pipe Channel Manager') do
80
+ now = nil
81
+ loop do
82
+ clean_up_pipes
83
+ pipes_to_listen_to = @pipes.values.map{|pipe| pipe.out} + [wake.out]
84
+ NewRelic::Agent.instance.stats_engine \
85
+ .get_stats_no_scope('Supportability/Listeners') \
86
+ .record_data_point((Time.now - now).to_f) if now
87
+ if ready = IO.select(pipes_to_listen_to, [], [], @select_timeout)
88
+ now = Time.now
89
+ pipe = ready[0][0]
90
+ if pipe == wake.out
91
+ pipe.read(1)
92
+ else
93
+ merge_data_from_pipe(pipe)
94
+ end
95
+ end
96
+
97
+ break if !should_keep_listening?
98
+ end
99
+ end
100
+ sleep 0.001 # give time for the thread to spawn
101
+ end
102
+
103
+ def stop
104
+ return unless @started == true
105
+ @started = false
106
+ wake.in << '.' unless wake.in.closed?
107
+ @thread.join # make sure we wait for the thread to exit
108
+ close_all_pipes
109
+ @wake.close
110
+ @wake = nil
111
+ end
112
+
113
+ def close_all_pipes
114
+ @pipes.each do |id, pipe|
115
+ pipe.close if pipe
116
+ end
117
+ @pipes = {}
118
+ end
119
+
120
+ def wake
121
+ @wake ||= Pipe.new
122
+ end
123
+
124
+ def started?
125
+ @started
126
+ end
127
+
128
+ protected
129
+
130
+ def merge_data_from_pipe(pipe_handle)
131
+ pipe = find_pipe_for_handle(pipe_handle)
132
+ got = pipe.read
133
+
134
+ if got && !got.empty?
135
+ payload = unmarshal(got)
136
+ if payload == 'EOF'
137
+ pipe.close
138
+ elsif payload
139
+ NewRelic::Agent.agent.merge_data_from([payload[:stats],
140
+ payload[:transaction_traces],
141
+ payload[:error_traces]])
142
+ end
143
+ end
144
+ end
145
+
146
+ def unmarshal(data)
147
+ NewRelic::LanguageSupport.with_cautious_gc do
148
+ Marshal.load(data)
149
+ end
150
+ rescue StandardError => e
151
+ msg = "#{e.class.name} '#{e.message}' trying to load #{Base64.encode64(data)}"
152
+ ::NewRelic::Agent.logger.debug(msg)
153
+ nil
154
+ end
155
+
156
+ def should_keep_listening?
157
+ @started || @pipes.values.find{|pipe| !pipe.in.closed?}
158
+ end
159
+
160
+ def clean_up_pipes
161
+ @pipes.values.each do |pipe|
162
+ if pipe.last_read.to_f + @timeout < Time.now.to_f
163
+ pipe.close unless pipe.closed?
164
+ end
165
+ end
166
+ @pipes.reject! {|id, pipe| pipe.out.closed? }
167
+ end
168
+
169
+ def find_pipe_for_handle(out_handle)
170
+ @pipes.values.find{|pipe| pipe.out == out_handle }
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,58 @@
1
+ module NewRelic
2
+ module Agent
3
+ class PipeService
4
+ attr_reader :channel_id, :buffer
5
+ attr_accessor :request_timeout, :agent_id, :collector
6
+
7
+ def initialize(channel_id)
8
+ @channel_id = channel_id
9
+ @collector = NewRelic::Control::Server.new(:name => 'parent',
10
+ :port => 0)
11
+ end
12
+
13
+ def connect(config)
14
+ nil
15
+ end
16
+
17
+ def get_agent_commands
18
+ []
19
+ end
20
+
21
+ def metric_data(last_harvest_time, now, unsent_timeslice_data)
22
+ write_to_pipe(:stats => hash_from_metric_data(unsent_timeslice_data))
23
+ {}
24
+ end
25
+
26
+ def transaction_sample_data(transactions)
27
+ write_to_pipe(:transaction_traces => transactions) if transactions
28
+ end
29
+
30
+ def error_data(errors)
31
+ write_to_pipe(:error_traces => errors) if errors
32
+ end
33
+
34
+ def sql_trace_data(sql)
35
+ write_to_pipe(:sql_traces => sql) if sql
36
+ end
37
+
38
+ def shutdown(time)
39
+ write_to_pipe('EOF')
40
+ NewRelic::Agent::PipeChannelManager.channels[@channel_id].close
41
+ end
42
+
43
+ private
44
+
45
+ def hash_from_metric_data(metric_data)
46
+ metric_hash = {}
47
+ metric_data.each do |metric_entry|
48
+ metric_hash[metric_entry.metric_spec] = metric_entry
49
+ end
50
+ metric_hash
51
+ end
52
+
53
+ def write_to_pipe(data)
54
+ NewRelic::Agent::PipeChannelManager.channels[@channel_id].write(data)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,86 @@
1
+ require 'new_relic/agent/sampler'
2
+ require 'new_relic/delayed_job_injection'
3
+
4
+ module NewRelic
5
+ module Agent
6
+ module Samplers
7
+ # This sampler records the status of your delayed job table once a minute.
8
+ # It assumes jobs are cleared after being run, and failed jobs are not (otherwise
9
+ # the failed job metric is useless).
10
+ #
11
+ # In earlier versions it will break out the queue length by priority. In later
12
+ # versions of DJ where distinct queues are supported, it breaks it out by queue name.
13
+ #
14
+ class DelayedJobSampler < NewRelic::Agent::Sampler
15
+ def initialize
16
+ super :delayed_job_queue
17
+ raise Unsupported, "DJ instrumentation disabled" if Agent.config[:disable_dj]
18
+ raise Unsupported, "No DJ worker present" unless NewRelic::DelayedJobInjection.worker_name
19
+ end
20
+
21
+ def error_stats
22
+ stats_engine.get_stats("Workers/DelayedJob/failed_jobs", false)
23
+ end
24
+ def locked_job_stats
25
+ stats_engine.get_stats("Workers/DelayedJob/locked_jobs", false)
26
+ end
27
+
28
+ def local_env
29
+ NewRelic::Control.instance.local_env
30
+ end
31
+
32
+ def worker_name
33
+ local_env.dispatcher_instance_id
34
+ end
35
+
36
+ def failed_jobs
37
+ Delayed::Job.count(:conditions => 'failed_at is not NULL')
38
+ end
39
+ def locked_jobs
40
+ Delayed::Job.count(:conditions => 'locked_by is not NULL')
41
+ end
42
+
43
+ def self.supported_on_this_platform?
44
+ defined?(Delayed::Job)
45
+ end
46
+
47
+ def poll
48
+ record error_stats, failed_jobs
49
+ record locked_job_stats, locked_jobs
50
+
51
+ if @queue
52
+ record_queue_length_across_dimension('queue')
53
+ else
54
+ record_queue_length_across_dimension('priority')
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def record_queue_length_across_dimension(column)
61
+ all_count = 0
62
+ Delayed::Job.count(:group => column, :conditions => ['run_at < ? and failed_at is NULL', Time.now]).each do | column_val, count |
63
+ all_count += count
64
+ record stats_engine.get_stats("Workers/DelayedJob/queue_length/#{column == 'queue' ? 'name' : column}/#{column_val}", false), count
65
+ end
66
+ record(stats_engine.get_stats("Workers/DelayedJob/queue_length/all", false), all_count)
67
+ end
68
+
69
+ # Figure out if we get the queues.
70
+ def setup
71
+ return unless @queue.nil?
72
+ @setup = true
73
+ columns = Delayed::Job.columns
74
+ columns.each do | c |
75
+ @queue = true if c.name.to_s == 'priority'
76
+ end
77
+ @queue ||= false
78
+ end
79
+
80
+ def record(stat, size)
81
+ stat.record_data_point size
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -15,14 +15,14 @@ module NewRelic
15
15
  elsif platform =~ /linux/
16
16
  @sampler = ProcStatus.new
17
17
  if !@sampler.can_run?
18
- NewRelic::Agent.instance.warn.debug "Error attempting to use /proc/#{$$}/status file for reading memory. Using ps command instead."
18
+ ::NewRelic::Agent.logger.debug "Error attempting to use /proc/#{$$}/status file for reading memory. Using ps command instead."
19
19
  @sampler = ShellPS.new("ps -o rsz")
20
20
  else
21
- NewRelic::Agent.instance.log.debug "Using /proc/#{$$}/status for reading process memory."
21
+ ::NewRelic::Agent.logger.debug "Using /proc/#{$$}/status for reading process memory."
22
22
  end
23
23
  elsif platform =~ /darwin9/ # 10.5
24
24
  @sampler = ShellPS.new("ps -o rsz")
25
- elsif platform =~ /darwin1[01]/ # 10.6 & 10.7
25
+ elsif platform =~ /darwin1\d+/ # >= 10.6
26
26
  @sampler = ShellPS.new("ps -o rss")
27
27
  elsif platform =~ /freebsd/
28
28
  @sampler = ShellPS.new("ps -o rss")
@@ -35,7 +35,7 @@ module NewRelic
35
35
  end
36
36
 
37
37
  def self.supported_on_this_platform?
38
- defined?(JRuby) or platform =~ /linux|darwin9|darwin10|freebsd|solaris/
38
+ defined?(JRuby) or platform =~ /linux|darwin|freebsd|solaris/
39
39
  end
40
40
 
41
41
  def self.platform
@@ -68,14 +68,12 @@ module NewRelic
68
68
  begin
69
69
  m = get_memory
70
70
  if m.nil?
71
- NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. Disabling memory sampler."
71
+ ::NewRelic::Agent.logger.warn "Unable to get the resident memory for process #{$$}. Disabling memory sampler."
72
72
  @broken = true
73
73
  end
74
74
  return m
75
75
  rescue => e
76
- NewRelic::Agent.instance.log.error "Unable to get the resident memory for process #{$$}. (#{e})"
77
- NewRelic::Agent.instance.log.debug e.backtrace.join("\n ")
78
- NewRelic::Agent.instance.log.error "Disabling memory sampler."
76
+ ::NewRelic::Agent.logger.warn "Unable to get the resident memory for process #{$$}. Disabling memory sampler.", e
79
77
  @broken = true
80
78
  return nil
81
79
  end
@@ -1,5 +1,10 @@
1
+ require 'zlib'
2
+ require 'base64'
3
+ require 'digest/md5'
4
+
1
5
  require 'new_relic/agent'
2
6
  require 'new_relic/control'
7
+
3
8
  module NewRelic
4
9
  module Agent
5
10
 
@@ -18,7 +23,6 @@ module NewRelic
18
23
  attr_reader :sql_traces
19
24
 
20
25
  def initialize
21
- configure!
22
26
  @sql_traces = {}
23
27
  clear_transaction_data
24
28
 
@@ -27,59 +31,21 @@ module NewRelic
27
31
  # any 'honest-to-god'-multithreaded system
28
32
  @samples_lock = Mutex.new
29
33
  end
30
-
31
- def configure!
32
- @explain_threshold = config.fetch('explain_threshold', 0.5).to_f
33
- @explain_enabled = config.fetch('explain_enabled', true)
34
- @stack_trace_threshold = config.fetch('stack_trace_threshold',
35
- 0.5).to_f
36
- if config.fetch('enabled', true) &&
37
- NewRelic::Control.instance['transaction_tracer'] &&
38
- NewRelic::Control.instance['transaction_tracer'].fetch('enabled',
39
- true) &&
40
- NewRelic::Control.instance.fetch('collect_traces', true)
41
- enable
42
- else
43
- disable
44
- end
45
- end
46
-
47
- def config
48
- self.class.config
49
- end
50
-
51
- def self.config
52
- control = NewRelic::Control.instance
53
- txn_config = control.fetch('transaction_tracer', {})
54
-
55
- if txn_config.fetch('enabled', true) && control.has_slow_sql_config?
56
- txn_config['enabled'] = control['slow_sql']['enabled']
57
- end
58
-
59
- txn_config
60
- end
61
-
62
- # Enable the sql sampler - this also registers it with
63
- # the statistics engine.
64
- def enable
65
- @disabled = false
66
- end
67
-
68
- # Disable the sql sampler - this also deregisters it
69
- # with the statistics engine.
70
- def disable
71
- @disabled = true
72
- end
73
34
 
74
35
  def enabled?
75
- !@disabled
36
+ Agent.config[:'slow_sql.enabled'] &&
37
+ (Agent.config[:'slow_sql.record_sql'] == 'raw' ||
38
+ Agent.config[:'slow_sql.record_sql'] == 'obfuscated') &&
39
+ Agent.config[:'transaction_tracer.enabled']
76
40
  end
77
41
 
78
42
  def notice_transaction(path, uri=nil, params={})
79
43
  if NewRelic::Agent.instance.transaction_sampler.builder
80
44
  guid = NewRelic::Agent.instance.transaction_sampler.builder.sample.guid
81
45
  end
82
- transaction_data.set_transaction_info(path, uri, params, guid) if !disabled && transaction_data
46
+ if Agent.config[:'slow_sql.enabled'] && transaction_data
47
+ transaction_data.set_transaction_info(path, uri, params, guid)
48
+ end
83
49
  end
84
50
 
85
51
  def notice_first_scope_push(time)
@@ -105,7 +71,7 @@ module NewRelic
105
71
 
106
72
  if data.sql_data.size > 0
107
73
  @samples_lock.synchronize do
108
- NewRelic::Agent.instance.log.debug "Harvesting #{data.sql_data.size} slow transaction sql statement(s)"
74
+ ::NewRelic::Agent.logger.debug "Harvesting #{data.sql_data.size} slow transaction sql statement(s)"
109
75
  #FIXME get tx name and uri
110
76
  harvest_slow_sql data
111
77
  end
@@ -131,7 +97,7 @@ module NewRelic
131
97
  def notice_sql(sql, metric_name, config, duration)
132
98
  return unless transaction_data
133
99
  if NewRelic::Agent.is_sql_recorded?
134
- if duration > @explain_threshold
100
+ if duration > Agent.config[:'slow_sql.explain_threshold']
135
101
  backtrace = caller.join("\n")
136
102
  transaction_data.sql_data << SlowSql.new(sql, metric_name, config,
137
103
  duration, backtrace)
@@ -147,12 +113,12 @@ module NewRelic
147
113
  end
148
114
 
149
115
  def harvest
150
- return [] if disabled
116
+ return [] if !Agent.config[:'slow_sql.enabled']
151
117
  result = []
152
118
  @samples_lock.synchronize do
153
119
  result = @sql_traces.values
154
120
  @sql_traces = {}
155
- end
121
+ end
156
122
  slowest = result.sort{|a,b| b.max_call_time <=> a.max_call_time}[0,10]
157
123
  slowest.each {|trace| trace.prepare_to_send }
158
124
  slowest
@@ -243,43 +209,34 @@ module NewRelic
243
209
 
244
210
  record_data_point slow_sql.duration
245
211
  end
246
-
212
+
247
213
  def prepare_to_send
248
- begin
249
- params[:explain_plan] = @slow_sql.explain if need_to_explain?
250
- ensure
251
- NewRelic::Agent::Database.close_connections
252
- end
214
+ params[:explain_plan] = @slow_sql.explain if need_to_explain?
253
215
  @sql = @slow_sql.obfuscate if need_to_obfuscate?
254
216
  end
255
-
256
- def agent_config
257
- NewRelic::Agent::SqlSampler.config
258
- end
259
-
217
+
260
218
  def need_to_obfuscate?
261
- agent_config['record_sql'] == 'obfuscated'
219
+ Agent.config[:'slow_sql.record_sql'].to_s == 'obfuscated'
262
220
  end
263
221
 
264
222
  def need_to_explain?
265
- agent_config['explain_enabled']
223
+ Agent.config[:'slow_sql.explain_enabled']
266
224
  end
267
-
268
- def to_json(*a)
269
- [@path, @url, @sql_id, @sql, @database_metric_name, @call_count, @total_call_time, @min_call_time, @max_call_time, @params].to_json(*a)
225
+
226
+ def to_collector_array(encoder)
227
+ [ @path, @url, @sql_id, @sql, @database_metric_name, @call_count,
228
+ Helper.time_to_millis(@total_call_time),
229
+ Helper.time_to_millis(@min_call_time),
230
+ Helper.time_to_millis(@max_call_time),
231
+ encoder.encode(@params) ]
270
232
  end
271
233
 
272
234
  private
273
235
 
274
236
  def consistent_hash(string)
275
- if NewRelic::LanguageSupport.using_version?('1.9.2')
276
- # String#hash is salted differently on every VM start in 1.9
277
- require 'digest/md5'
278
- Digest::MD5.hexdigest(string).hex
279
- else
280
- string.hash
281
- end.modulo(2**31-1)
282
- # modulo ensures sql_id fits in an INT(11)
237
+ # need to hash the same way in every process
238
+ Digest::MD5.hexdigest(string).hex \
239
+ .modulo(2**31-1) # ensure sql_id fits in an INT(11)
283
240
  end
284
241
  end
285
242
  end