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,32 @@
1
+ module NewRelic
2
+ module Agent
3
+
4
+ class AgentThread < ::Thread
5
+ def initialize(label)
6
+ ::NewRelic::Agent.logger.debug("Creating New Relic thread: #{label}")
7
+ self[:newrelic_label] = label
8
+ super
9
+ end
10
+
11
+ def self.bucket_thread(thread, profile_agent_code)
12
+ if thread.key?(:newrelic_label)
13
+ return profile_agent_code ? :agent : :ignore
14
+ elsif !thread[:newrelic_metric_frame].nil?
15
+ thread[:newrelic_metric_frame].request.nil? ? :background : :request
16
+ else
17
+ :other
18
+ end
19
+ end
20
+
21
+ def self.scrub_backtrace(thread, profile_agent_code)
22
+ begin
23
+ bt = thread.backtrace
24
+ rescue Exception => e
25
+ ::NewRelic::Agent.logger.debug("Failed to backtrace #{thread.inspect}: #{e.class.name}: #{e.to_s}")
26
+ end
27
+ return nil unless bt
28
+ profile_agent_code ? bt : bt.select { |t| t !~ /\/newrelic_rpm-\d/ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,306 @@
1
+ require 'new_relic/agent/thread'
2
+ require 'new_relic/agent/worker_loop'
3
+
4
+ module NewRelic
5
+ module Agent
6
+
7
+ class ThreadProfiler
8
+
9
+ attr_reader :profile
10
+
11
+ def self.is_supported?
12
+ RUBY_VERSION >= "1.9.2"
13
+ end
14
+
15
+ def start(profile_id, duration, interval, profile_agent_code)
16
+ if !ThreadProfiler.is_supported?
17
+ ::NewRelic::Agent.logger.debug("Not starting thread profile as it isn't supported on this environment")
18
+ @profile = nil
19
+ else
20
+ ::NewRelic::Agent.logger.debug("Starting thread profile. profile_id=#{profile_id}, duration=#{duration}")
21
+ @profile = ThreadProfile.new(profile_id, duration, interval, profile_agent_code)
22
+ @profile.run
23
+ end
24
+ end
25
+
26
+ def stop(report_data)
27
+ @profile.stop unless @profile.nil?
28
+ @profile = nil if !report_data
29
+ end
30
+
31
+ def harvest
32
+ profile = @profile
33
+ @profile = nil
34
+ profile
35
+ end
36
+
37
+ def respond_to_commands(commands, &notify_results)
38
+ return if commands.empty? || commands.first.size < 2
39
+
40
+ # Doesn't deal with multiple commands in the return set as
41
+ # we currently only have start/stop of thread profiling
42
+ command_id = commands.first[0]
43
+ command = commands.first[1]
44
+
45
+ name = command["name"]
46
+ arguments = command["arguments"]
47
+
48
+ if (ThreadProfiler.is_supported?)
49
+ case name
50
+ when "start_profiler"
51
+ start_unless_running_and_notify(command_id, arguments, &notify_results)
52
+
53
+ when "stop_profiler"
54
+ stop_and_notify(command_id, arguments, &notify_results)
55
+ end
56
+ else
57
+ msg = <<-EOF
58
+ Thread profiling is only supported on 1.9.2 and greater versions of Ruby.
59
+ We detected running agents capable of profiling, but the profile started with
60
+ an agent running Ruby #{RUBY_VERSION}.
61
+
62
+ Profiling again might select an appropriate agent, but we recommend running a
63
+ consistent version of Ruby across your application for better results.
64
+ EOF
65
+ ::NewRelic::Agent.logger.debug(msg)
66
+ notify_results.call(command_id, msg) if !notify_results.nil?
67
+ end
68
+ end
69
+
70
+ def running?
71
+ !@profile.nil?
72
+ end
73
+
74
+ def finished?
75
+ @profile && @profile.finished?
76
+ end
77
+
78
+ private
79
+
80
+ def start_unless_running_and_notify(command_id, arguments)
81
+ profile_id = arguments.fetch("profile_id", -1)
82
+ duration = arguments.fetch("duration", 120)
83
+ interval = arguments.fetch("sample_period", 0.1)
84
+ profile_agent_code = arguments.fetch("profile_agent_code", true)
85
+
86
+ if running?
87
+ msg = "Profile already in progress. Ignoring agent command to start another."
88
+ ::NewRelic::Agent.logger.debug(msg)
89
+ yield(command_id, msg) if block_given?
90
+ else
91
+ start(profile_id, duration, interval, profile_agent_code)
92
+ yield(command_id) if block_given?
93
+ end
94
+ end
95
+
96
+ def stop_and_notify(command_id, arguments)
97
+ report_data = arguments.fetch("report_data", true)
98
+ stop(report_data)
99
+ yield(command_id) if block_given?
100
+ end
101
+
102
+ end
103
+
104
+ class ThreadProfile
105
+
106
+ attr_reader :profile_id,
107
+ :traces,
108
+ :profile_agent_code, :interval,
109
+ :poll_count, :sample_count,
110
+ :start_time, :stop_time
111
+
112
+ def initialize(profile_id, duration, interval, profile_agent_code)
113
+ @profile_id = profile_id
114
+ @profile_agent_code = profile_agent_code
115
+
116
+ @worker_loop = NewRelic::Agent::WorkerLoop.new(:duration => duration)
117
+ @interval = interval
118
+ @finished = false
119
+
120
+ @traces = {
121
+ :agent => [],
122
+ :background => [],
123
+ :other => [],
124
+ :request => []
125
+ }
126
+ @flattened_nodes = []
127
+
128
+ @poll_count = 0
129
+ @sample_count = 0
130
+ @failure_count = 0
131
+ end
132
+
133
+ def run
134
+ AgentThread.new('Thread Profiler') do
135
+ @start_time = now_in_millis
136
+
137
+ @worker_loop.run(@interval) do
138
+ NewRelic::Agent.instance.stats_engine.
139
+ record_supportability_metrics_timed("ThreadProfiler/PollingTime") do
140
+
141
+ @poll_count += 1
142
+ AgentThread.list.each do |t|
143
+ bucket = AgentThread.bucket_thread(t, @profile_agent_code)
144
+ if bucket != :ignore
145
+ backtrace = AgentThread.scrub_backtrace(t, @profile_agent_code)
146
+ if backtrace.nil?
147
+ @failure_count += 1
148
+ else
149
+ @sample_count += 1
150
+ aggregate(backtrace, @traces[bucket])
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ mark_done
158
+ ::NewRelic::Agent.logger.debug("Finished thread profile. #{@sample_count} backtraces, #{@failure_count} failures. Will send with next harvest.")
159
+ NewRelic::Agent.instance.stats_engine.
160
+ record_supportability_metrics_count(@failure_count, "ThreadProfiler/BacktraceFailures")
161
+ end
162
+ end
163
+
164
+ def stop
165
+ @worker_loop.stop
166
+ mark_done
167
+ ::NewRelic::Agent.logger.debug("Stopping thread profile.")
168
+ end
169
+
170
+ def aggregate(trace, trees=@traces[:request], parent=nil)
171
+ return nil if trace.nil? || trace.empty?
172
+ node = Node.new(trace.last)
173
+ existing = trees.find {|n| n == node}
174
+
175
+ if existing.nil?
176
+ existing = node
177
+ @flattened_nodes << node
178
+ end
179
+
180
+ if parent
181
+ parent.add_child(node)
182
+ else
183
+ trees << node unless trees.include? node
184
+ end
185
+
186
+ existing.runnable_count += 1
187
+ aggregate(trace[0..-2], existing.children, existing)
188
+
189
+ existing
190
+ end
191
+
192
+ def prune!(count_to_keep)
193
+ @flattened_nodes.sort!(&:order_for_pruning)
194
+
195
+ NewRelic::Agent.instance.stats_engine.
196
+ record_supportability_metrics_count(@flattened_nodes.size, "ThreadProfiler/NodeCount")
197
+
198
+ mark_for_pruning(@flattened_nodes, count_to_keep)
199
+
200
+ traces.each { |_, nodes| Node.prune!(nodes) }
201
+ end
202
+
203
+ THREAD_PROFILER_NODES = 20_000
204
+
205
+ def to_collector_array(encoder)
206
+ prune!(THREAD_PROFILER_NODES)
207
+
208
+ traces = {
209
+ "OTHER" => @traces[:other].map{|t| t.to_array },
210
+ "REQUEST" => @traces[:request].map{|t| t.to_array },
211
+ "AGENT" => @traces[:agent].map{|t| t.to_array },
212
+ "BACKGROUND" => @traces[:background].map{|t| t.to_array }
213
+ }
214
+
215
+ [[@profile_id,
216
+ @start_time.to_f, @stop_time.to_f,
217
+ @poll_count,
218
+ encoder.encode(traces),
219
+ @sample_count, 0]]
220
+ end
221
+
222
+ def now_in_millis
223
+ Time.now.to_f * 1_000
224
+ end
225
+
226
+ def finished?
227
+ @finished
228
+ end
229
+
230
+ def mark_done
231
+ @finished = true
232
+ @stop_time = now_in_millis
233
+ end
234
+
235
+ def mark_for_pruning(nodes, count_to_keep)
236
+ to_prune = nodes[count_to_keep..-1] || []
237
+ to_prune.each { |n| n.to_prune = true }
238
+ end
239
+
240
+ def self.flattened_nodes(nodes)
241
+ nodes.map { |n| [n, flattened_nodes(n.children)] }.flatten
242
+ end
243
+
244
+ def self.parse_backtrace(trace)
245
+ trace.map do |line|
246
+ line =~ /(.*)\:(\d+)\:in `(.*)'/
247
+ { :method => $3, :line_no => $2.to_i, :file => $1 }
248
+ end
249
+ end
250
+
251
+ class Node
252
+ attr_reader :file, :method, :line_no, :children
253
+ attr_accessor :runnable_count, :to_prune, :depth
254
+
255
+ def initialize(line, parent=nil)
256
+ line =~ /(.*)\:(\d+)\:in `(.*)'/
257
+ @file = $1
258
+ @method = $3
259
+ @line_no = $2.to_i
260
+ @children = []
261
+ @runnable_count = 0
262
+ @to_prune = false
263
+ @depth = 0
264
+
265
+ parent.add_child(self) if parent
266
+ end
267
+
268
+ def ==(other)
269
+ @file == other.file &&
270
+ @method == other.method &&
271
+ @line_no == other.line_no
272
+ end
273
+
274
+ def total_count
275
+ @runnable_count
276
+ end
277
+
278
+ # Descending order on count, ascending on depth of nodes
279
+ def order_for_pruning(y)
280
+ [-runnable_count, depth] <=> [-y.runnable_count, y.depth]
281
+ end
282
+
283
+ def to_array
284
+ [[@file, @method, @line_no],
285
+ @runnable_count, 0,
286
+ @children.map {|c| c.to_array}]
287
+ end
288
+
289
+ def add_child(child)
290
+ child.depth = @depth + 1
291
+ @children << child unless @children.include? child
292
+ end
293
+
294
+ def prune!
295
+ Node.prune!(@children)
296
+ end
297
+
298
+ def self.prune!(kids)
299
+ kids.delete_if { |child| child.to_prune }
300
+ kids.each { |child| child.prune! }
301
+ end
302
+ end
303
+
304
+ end
305
+ end
306
+ end
@@ -11,14 +11,15 @@ module NewRelic
11
11
  @guid = ""
12
12
  @transaction_name = "(unknown)"
13
13
  @start_time = Time.now
14
+ @ignore_end_user = false
14
15
  end
15
16
 
16
17
  def force_persist_sample?(sample)
17
- token && sample.duration > NewRelic::Control.instance.apdex_t
18
+ token && sample.duration > Agent.config[:apdex_t]
18
19
  end
19
20
 
20
21
  def include_guid?
21
- token && duration > NewRelic::Control.instance.apdex_t
22
+ token && duration > Agent.config[:apdex_t]
22
23
  end
23
24
 
24
25
  def guid
@@ -33,6 +34,14 @@ module NewRelic
33
34
  Time.now - start_time
34
35
  end
35
36
 
37
+ def ignore_end_user?
38
+ @ignore_end_user
39
+ end
40
+
41
+ def ignore_end_user=(value)
42
+ @ignore_end_user = value
43
+ end
44
+
36
45
  def self.get()
37
46
  Thread.current[:newrelic_transaction_info] ||= TransactionInfo.new
38
47
  end
@@ -57,17 +66,28 @@ module NewRelic
57
66
  return nil unless request
58
67
 
59
68
  agent_flag = request.cookies['NRAGENT']
60
- if agent_flag
69
+ if agent_flag and agent_flag.instance_of? String
61
70
  s = agent_flag.split("=")
62
71
  if s.length == 2
63
72
  if s[0] == "tk" && s[1]
64
- ERB::Util.h(s[1])
73
+ ERB::Util.h(sanitize_token(s[1]))
65
74
  end
66
75
  end
67
76
  else
68
77
  nil
69
78
  end
70
79
  end
80
+
81
+ # Run through a collection of unsafe characters ( in the context of the token )
82
+ # and set the token to an empty string if any of them are found in the token so that
83
+ # potential XSS attacks via the token are avoided
84
+ def self.sanitize_token(token)
85
+
86
+ if ( /[<>'"]/ =~ token )
87
+ token.replace("")
88
+ end
89
+ token
90
+ end
71
91
  end
72
92
  end
73
93
  end
@@ -9,7 +9,6 @@ module NewRelic
9
9
  # accessed by any other thread so no need for synchronization.
10
10
  class TransactionSampleBuilder
11
11
  attr_reader :current_segment, :sample
12
- attr_accessor :segment_limit
13
12
 
14
13
  include NewRelic::CollectionHelper
15
14
 
@@ -17,8 +16,6 @@ module NewRelic
17
16
  @sample = NewRelic::TransactionSample.new(time.to_f)
18
17
  @sample_start = time.to_f
19
18
  @current_segment = @sample.root_segment
20
- @segment_limit = NewRelic::Control.instance.fetch('transaction_tracer', {}) \
21
- .fetch('limit_segments', 4000)
22
19
  end
23
20
 
24
21
  def sample_id
@@ -34,19 +31,20 @@ module NewRelic
34
31
  end
35
32
 
36
33
  def trace_entry(metric_name, time)
37
- if @sample.count_segments < @segment_limit
34
+ segment_limit = Agent.config[:'transaction_tracer.limit_segments']
35
+ if @sample.count_segments < segment_limit
38
36
  segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
39
37
  @current_segment.add_called_segment(segment)
40
38
  @current_segment = segment
41
- if @sample.count_segments == @segment_limit
42
- NewRelic::Control.instance.log.debug("Segment limit of #{@segment_limit} reached, ceasing collection.")
39
+ if @sample.count_segments == segment_limit
40
+ ::NewRelic::Agent.logger.debug("Segment limit of #{segment_limit} reached, ceasing collection.")
43
41
  end
44
42
  @current_segment
45
43
  end
46
44
  end
47
45
 
48
46
  def trace_exit(metric_name, time)
49
- return unless @sample.count_segments < @segment_limit
47
+ return unless @sample.count_segments < Agent.config[:'transaction_tracer.limit_segments']
50
48
  if metric_name != @current_segment.metric_name
51
49
  fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
52
50
  end
@@ -58,13 +56,13 @@ module NewRelic
58
56
  # This should never get called twice, but in a rare case that we can't reproduce in house it does.
59
57
  # log forensics and return gracefully
60
58
  if @sample.frozen?
61
- log = NewRelic::Control.instance.log
62
- log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
59
+ ::NewRelic::Agent.logger.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
63
60
  return
64
61
  end
65
62
  @sample.root_segment.end_trace(time.to_f - @sample_start)
66
- @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
67
-
63
+ @sample.params[:custom_params] ||= {}
64
+ @sample.params[:custom_params].merge!(normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters))
65
+
68
66
  @sample.force_persist = NewRelic::Agent::TransactionInfo.get.force_persist_sample?(sample)
69
67
  @sample.freeze
70
68
  @current_segment = nil
@@ -93,7 +91,7 @@ module NewRelic
93
91
  def set_transaction_info(path, uri, params)
94
92
  @sample.params[:path] = path
95
93
 
96
- if NewRelic::Control.instance.capture_params
94
+ if Agent.config[:capture_params]
97
95
  params = normalize_params params
98
96
 
99
97
  @sample.params[:request_params].merge!(params)
@@ -104,7 +102,8 @@ module NewRelic
104
102
  end
105
103
 
106
104
  def set_transaction_cpu_time(cpu_time)
107
- @sample.params[:cpu_time] = cpu_time
105
+ @sample.params[:custom_params] ||= {}
106
+ @sample.params[:custom_params][:cpu_time] = cpu_time
108
107
  end
109
108
 
110
109
  def sample