newrelic_rpm 3.8.1.221 → 3.9.0.229

Sign up to get free protection for your applications and to get access to all the features.
Files changed (288) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.gitignore +1 -0
  3. data/.yardopts +2 -0
  4. data/CHANGELOG +95 -0
  5. data/README.md +9 -3
  6. data/Rakefile +6 -0
  7. data/lib/new_relic/agent.rb +37 -52
  8. data/lib/new_relic/agent/agent.rb +32 -64
  9. data/lib/new_relic/agent/agent_logger.rb +3 -2
  10. data/lib/new_relic/agent/audit_logger.rb +2 -1
  11. data/lib/new_relic/agent/busy_calculator.rb +10 -8
  12. data/lib/new_relic/agent/configuration.rb +0 -13
  13. data/lib/new_relic/agent/configuration/default_source.rb +27 -20
  14. data/lib/new_relic/agent/configuration/manager.rb +101 -27
  15. data/lib/new_relic/agent/cross_app_monitor.rb +43 -50
  16. data/lib/new_relic/agent/cross_app_tracing.rb +13 -12
  17. data/lib/new_relic/agent/error_collector.rb +31 -35
  18. data/lib/new_relic/agent/harvester.rb +5 -1
  19. data/lib/new_relic/agent/hostname.rb +17 -0
  20. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +1 -1
  21. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +1 -1
  22. data/lib/new_relic/agent/http_clients/uri_util.rb +13 -0
  23. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +18 -32
  24. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +15 -15
  25. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  26. data/lib/new_relic/agent/instrumentation/active_record.rb +6 -4
  27. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +3 -2
  28. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +18 -20
  29. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +79 -93
  30. data/lib/new_relic/agent/instrumentation/curb.rb +3 -3
  31. data/lib/new_relic/agent/instrumentation/data_mapper.rb +5 -4
  32. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +96 -0
  33. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +69 -0
  34. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  35. data/lib/new_relic/agent/instrumentation/queue_time.rb +21 -13
  36. data/lib/new_relic/agent/instrumentation/rack.rb +85 -74
  37. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +3 -1
  38. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +39 -0
  39. data/lib/new_relic/agent/instrumentation/rubyprof.rb +3 -3
  40. data/lib/new_relic/agent/instrumentation/sidekiq.rb +28 -5
  41. data/lib/new_relic/agent/instrumentation/sinatra.rb +4 -4
  42. data/lib/new_relic/agent/instrumentation/typhoeus.rb +4 -2
  43. data/lib/new_relic/agent/javascript_instrumentor.rb +34 -30
  44. data/lib/new_relic/agent/memory_logger.rb +12 -12
  45. data/lib/new_relic/agent/method_tracer.rb +34 -74
  46. data/lib/new_relic/agent/new_relic_service.rb +1 -1
  47. data/lib/new_relic/agent/pipe_channel_manager.rb +3 -3
  48. data/lib/new_relic/agent/request_sampler.rb +10 -11
  49. data/lib/new_relic/agent/samplers/vm_sampler.rb +6 -6
  50. data/lib/new_relic/agent/shim_agent.rb +2 -1
  51. data/lib/new_relic/agent/sql_sampler.rb +52 -27
  52. data/lib/new_relic/agent/stats.rb +24 -10
  53. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +5 -17
  54. data/lib/new_relic/agent/stats_engine/metric_stats.rb +106 -58
  55. data/lib/new_relic/agent/stats_engine/stats_hash.rb +20 -24
  56. data/lib/new_relic/agent/supported_versions.rb +3 -1
  57. data/lib/new_relic/agent/threading/agent_thread.rb +42 -11
  58. data/lib/new_relic/agent/threading/backtrace_service.rb +3 -7
  59. data/lib/new_relic/agent/threading/thread_profile.rb +2 -2
  60. data/lib/new_relic/agent/traced_method_stack.rb +28 -18
  61. data/lib/new_relic/agent/transaction.rb +249 -196
  62. data/lib/new_relic/agent/transaction_metrics.rb +57 -0
  63. data/lib/new_relic/agent/transaction_sample_builder.rb +10 -7
  64. data/lib/new_relic/agent/transaction_sampler.rb +81 -45
  65. data/lib/new_relic/agent/transaction_state.rb +38 -49
  66. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +15 -18
  67. data/lib/new_relic/agent/vm/rubinius_vm.rb +4 -2
  68. data/lib/new_relic/cli/commands/deployments.rb +3 -2
  69. data/lib/new_relic/control/frameworks/ruby.rb +2 -3
  70. data/lib/new_relic/control/frameworks/sinatra.rb +0 -7
  71. data/lib/new_relic/control/instance_methods.rb +3 -5
  72. data/lib/new_relic/json_wrapper.rb +2 -0
  73. data/lib/new_relic/language_support.rb +1 -1
  74. data/lib/new_relic/local_environment.rb +0 -16
  75. data/lib/new_relic/metric_spec.rb +10 -38
  76. data/lib/new_relic/noticed_error.rb +16 -11
  77. data/lib/new_relic/rack/agent_hooks.rb +4 -10
  78. data/lib/new_relic/rack/agent_middleware.rb +31 -0
  79. data/lib/new_relic/rack/browser_monitoring.rb +7 -13
  80. data/lib/new_relic/rack/developer_mode.rb +16 -59
  81. data/lib/new_relic/rack/error_collector.rb +16 -54
  82. data/lib/new_relic/recipes.rb +8 -101
  83. data/lib/new_relic/recipes/capistrano3.rb +66 -0
  84. data/lib/new_relic/recipes/capistrano_legacy.rb +98 -0
  85. data/lib/new_relic/transaction_sample.rb +6 -54
  86. data/lib/new_relic/transaction_sample/composite_segment.rb +1 -1
  87. data/lib/new_relic/transaction_sample/segment.rb +12 -4
  88. data/lib/new_relic/transaction_sample/summary_segment.rb +1 -1
  89. data/lib/new_relic/version.rb +2 -2
  90. data/lib/newrelic_rpm.rb +1 -1
  91. data/lib/sequel/extensions/newrelic_instrumentation.rb +19 -19
  92. data/lib/tasks/tests.rake +20 -1
  93. data/lib/tasks/versions.html.erb +0 -4
  94. data/lib/tasks/versions.rake +4 -3
  95. data/newrelic.yml +4 -12
  96. data/newrelic_rpm.gemspec +1 -1
  97. data/test/agent_helper.rb +146 -44
  98. data/test/config/newrelic.yml +0 -1
  99. data/test/environments/norails/Gemfile +1 -1
  100. data/test/environments/rails21/Gemfile +1 -1
  101. data/test/environments/rails22/Gemfile +1 -1
  102. data/test/environments/rails23/Gemfile +1 -1
  103. data/test/environments/rails30/Gemfile +1 -1
  104. data/test/environments/rails31/Gemfile +1 -1
  105. data/test/environments/rails32/Gemfile +1 -1
  106. data/test/environments/rails40/Gemfile +1 -1
  107. data/test/environments/rails41/Gemfile +1 -1
  108. data/test/multiverse/lib/multiverse/runner.rb +13 -1
  109. data/test/multiverse/lib/multiverse/suite.rb +26 -9
  110. data/test/multiverse/suites/active_record/config/newrelic.yml +0 -1
  111. data/test/multiverse/suites/activemerchant/Envfile +18 -1
  112. data/test/multiverse/suites/agent_only/audit_log_test.rb +4 -3
  113. data/test/multiverse/suites/agent_only/collector_exception_handling_test.rb +35 -0
  114. data/test/multiverse/suites/agent_only/config/newrelic.yml +0 -1
  115. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +1 -0
  116. data/test/multiverse/suites/agent_only/encoding_handling_test.rb +3 -2
  117. data/test/multiverse/suites/agent_only/exclusive_time_test.rb +178 -0
  118. data/test/multiverse/suites/agent_only/logging_test.rb +10 -6
  119. data/test/multiverse/suites/agent_only/marshaling_test.rb +11 -9
  120. data/test/multiverse/suites/agent_only/script/loading.rb +1 -1
  121. data/test/multiverse/suites/agent_only/service_timeout_test.rb +5 -1
  122. data/test/multiverse/suites/agent_only/transaction_ignoring_test.rb +2 -1
  123. data/test/multiverse/suites/agent_only/xray_sessions_test.rb +9 -9
  124. data/test/multiverse/suites/capistrano/Capfile +26 -0
  125. data/test/multiverse/suites/capistrano/Envfile +18 -0
  126. data/test/multiverse/suites/capistrano/config/deploy.rb +10 -0
  127. data/test/multiverse/suites/capistrano/config/deploy/production.rb +9 -0
  128. data/test/multiverse/suites/capistrano/config/newrelic.yml +21 -0
  129. data/test/multiverse/suites/capistrano/deployment_test.rb +47 -0
  130. data/test/multiverse/suites/capistrano2/Capfile +4 -0
  131. data/test/multiverse/suites/capistrano2/Envfile +4 -0
  132. data/test/multiverse/suites/capistrano2/config/deploy.rb +19 -0
  133. data/test/multiverse/suites/capistrano2/config/newrelic.yml +21 -0
  134. data/test/multiverse/suites/capistrano2/deployment_test.rb +38 -0
  135. data/test/multiverse/suites/curb/Envfile +10 -1
  136. data/test/multiverse/suites/curb/config/newrelic.yml +0 -1
  137. data/test/multiverse/suites/datamapper/config/newrelic.yml +0 -1
  138. data/test/multiverse/suites/deferred_instrumentation/config/newrelic.yml +0 -1
  139. data/test/multiverse/suites/excon/config/newrelic.yml +0 -1
  140. data/test/multiverse/suites/httpclient/config/newrelic.yml +0 -1
  141. data/test/multiverse/suites/mongo/config/newrelic.yml +0 -1
  142. data/test/multiverse/suites/net_http/config/newrelic.yml +0 -1
  143. data/test/multiverse/suites/padrino/config/newrelic.yml +0 -1
  144. data/test/multiverse/suites/rack/Envfile +25 -0
  145. data/test/multiverse/suites/rack/example_app.rb +50 -0
  146. data/test/multiverse/suites/rack/nested_non_rack_app_test.rb +66 -0
  147. data/test/multiverse/suites/rack/rack_auto_instrumentation_test.rb +143 -0
  148. data/test/multiverse/suites/rack/rack_unsupported_version_test.rb +45 -0
  149. data/test/multiverse/suites/rack/url_map_test.rb +120 -0
  150. data/test/multiverse/suites/rails/Envfile +10 -0
  151. data/test/multiverse/suites/rails/app.rb +28 -63
  152. data/test/multiverse/suites/rails/bad_instrumentation_test.rb +2 -4
  153. data/test/multiverse/suites/rails/config/newrelic.yml +1 -2
  154. data/test/multiverse/suites/rails/dummy.txt +1 -0
  155. data/test/multiverse/suites/rails/error_tracing_test.rb +46 -31
  156. data/test/multiverse/suites/rails/gc_instrumentation_test.rb +0 -1
  157. data/test/multiverse/suites/rails/ignore_test.rb +9 -3
  158. data/test/multiverse/suites/rails/middleware_instrumentation_test.rb +41 -0
  159. data/test/multiverse/suites/rails/middlewares.rb +19 -0
  160. data/test/multiverse/suites/rails/parameter_capture_test.rb +169 -0
  161. data/test/multiverse/suites/rails/queue_time_test.rb +14 -4
  162. data/test/multiverse/suites/rails/rails2_app/app/controllers/application.rb +7 -0
  163. data/test/multiverse/suites/rails/rails2_app/config/boot.rb +127 -0
  164. data/test/multiverse/suites/rails/rails2_app/config/database.yml +18 -0
  165. data/test/multiverse/suites/rails/rails2_app/config/environment.rb +16 -0
  166. data/test/multiverse/suites/rails/rails2_app/config/environments/development.rb +10 -0
  167. data/test/multiverse/suites/rails/rails2_app/config/initializers/load_newrelic_rpm.rb +9 -0
  168. data/test/multiverse/suites/rails/rails2_app/config/preinitializer.rb +25 -0
  169. data/test/multiverse/suites/rails/rails2_app/config/routes.rb +18 -0
  170. data/test/multiverse/suites/rails/rails2_app/db/schema.rb +5 -0
  171. data/test/multiverse/suites/rails/rails3_app/app_rails3_plus.rb +76 -0
  172. data/test/multiverse/suites/rails/request_statistics_test.rb +2 -4
  173. data/test/multiverse/suites/rails/transaction_ignoring_test.rb +3 -5
  174. data/test/multiverse/suites/rails/view_instrumentation_test.rb +73 -42
  175. data/test/multiverse/suites/resque/config/newrelic.yml +0 -1
  176. data/test/multiverse/suites/sequel/config/newrelic.yml +0 -1
  177. data/test/multiverse/suites/sidekiq/Envfile +4 -0
  178. data/test/multiverse/suites/sidekiq/after_suite.rb +7 -0
  179. data/test/multiverse/suites/sidekiq/config/newrelic.yml +0 -1
  180. data/test/multiverse/suites/sidekiq/sidekiq_instrumentation_test.rb +55 -34
  181. data/test/multiverse/suites/sidekiq/sidekiq_server.rb +30 -0
  182. data/test/multiverse/suites/sidekiq/test_worker.rb +60 -0
  183. data/test/multiverse/suites/sinatra/config/newrelic.yml +0 -1
  184. data/test/multiverse/suites/sinatra/ignoring_test.rb +33 -11
  185. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +3 -1
  186. data/test/multiverse/suites/typhoeus/Envfile +9 -0
  187. data/test/multiverse/suites/typhoeus/config/newrelic.yml +0 -1
  188. data/test/multiverse/suites/typhoeus/typhoeus_test.rb +10 -0
  189. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +0 -1
  190. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +0 -1
  191. data/test/new_relic/agent/agent/connect_test.rb +3 -10
  192. data/test/new_relic/agent/agent_logger_test.rb +24 -6
  193. data/test/new_relic/agent/agent_test.rb +7 -8
  194. data/test/new_relic/agent/agent_test_controller.rb +2 -2
  195. data/test/new_relic/agent/audit_logger_test.rb +5 -1
  196. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  197. data/test/new_relic/agent/configuration/manager_test.rb +68 -69
  198. data/test/new_relic/agent/cross_app_monitor_test.rb +32 -14
  199. data/test/new_relic/agent/cross_app_tracing_test.rb +2 -2
  200. data/test/new_relic/agent/error_collector/notice_error_test.rb +9 -33
  201. data/test/new_relic/agent/error_collector_test.rb +45 -14
  202. data/test/new_relic/agent/harvester_test.rb +9 -0
  203. data/test/new_relic/agent/hostname_test.rb +41 -0
  204. data/test/new_relic/agent/instrumentation/action_controller_subscriber_test.rb +27 -19
  205. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +1 -1
  206. data/test/new_relic/agent/instrumentation/active_record_test.rb +3 -3
  207. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +95 -14
  208. data/test/new_relic/agent/instrumentation/middleware_proxy_test.rb +189 -0
  209. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +2 -2
  210. data/test/new_relic/agent/instrumentation/queue_time_test.rb +18 -1
  211. data/test/new_relic/agent/instrumentation/rack_test.rb +10 -1
  212. data/test/new_relic/agent/instrumentation/sinatra_test.rb +3 -1
  213. data/test/new_relic/agent/javascript_instrumentor_test.rb +28 -41
  214. data/test/new_relic/agent/memory_logger_test.rb +14 -0
  215. data/test/new_relic/agent/method_interrobang_test.rb +1 -1
  216. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -30
  217. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +5 -21
  218. data/test/new_relic/agent/method_tracer_test.rb +5 -4
  219. data/test/new_relic/agent/mock_scope_listener.rb +2 -2
  220. data/test/new_relic/agent/obfuscator_test.rb +1 -1
  221. data/test/new_relic/agent/pipe_channel_manager_test.rb +17 -5
  222. data/test/new_relic/agent/request_sampler_test.rb +16 -16
  223. data/test/new_relic/agent/rpm_agent_test.rb +23 -29
  224. data/test/new_relic/agent/sql_sampler_test.rb +39 -31
  225. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +189 -117
  226. data/test/new_relic/agent/stats_engine_test.rb +1 -1
  227. data/test/new_relic/agent/stats_hash_test.rb +28 -1
  228. data/test/new_relic/agent/stats_test.rb +1 -12
  229. data/test/new_relic/agent/threading/agent_thread_test.rb +23 -9
  230. data/test/new_relic/agent/threading/backtrace_service_test.rb +33 -32
  231. data/test/new_relic/agent/threading/fake_thread.rb +4 -8
  232. data/test/new_relic/agent/threading/threaded_test_case.rb +4 -14
  233. data/test/new_relic/agent/traced_method_stack_test.rb +43 -27
  234. data/test/new_relic/agent/transaction_interrobang_test.rb +1 -1
  235. data/test/new_relic/agent/transaction_metrics_test.rb +113 -0
  236. data/test/new_relic/agent/transaction_sample_builder_test.rb +1 -61
  237. data/test/new_relic/agent/transaction_sampler_test.rb +176 -228
  238. data/test/new_relic/agent/transaction_state_test.rb +62 -26
  239. data/test/new_relic/agent/transaction_test.rb +198 -80
  240. data/test/new_relic/agent/vm/monotonic_gc_profiler_test.rb +4 -4
  241. data/test/new_relic/agent/vm/rubinius_vm_test.rb +68 -0
  242. data/test/new_relic/agent_test.rb +31 -27
  243. data/test/new_relic/cli/commands/deployments_test.rb +7 -2
  244. data/test/new_relic/control/instance_methods_test.rb +4 -4
  245. data/test/new_relic/control_test.rb +28 -22
  246. data/test/new_relic/dependency_detection_test.rb +14 -0
  247. data/test/new_relic/fake_external_server.rb +1 -0
  248. data/test/new_relic/fake_rpm_site.rb +35 -0
  249. data/test/new_relic/http_client_test_cases.rb +12 -3
  250. data/test/new_relic/json_wrapper_test.rb +5 -0
  251. data/test/new_relic/language_support_test.rb +7 -0
  252. data/test/new_relic/license_test.rb +11 -5
  253. data/test/new_relic/local_environment_test.rb +0 -18
  254. data/test/new_relic/metric_data_test.rb +2 -2
  255. data/test/new_relic/metric_spec_test.rb +4 -23
  256. data/test/new_relic/multiverse_helpers.rb +1 -3
  257. data/test/new_relic/noticed_error_test.rb +6 -22
  258. data/test/new_relic/rack/agent_hooks_test.rb +5 -1
  259. data/test/new_relic/rack/agent_middleware_test.rb +32 -0
  260. data/test/new_relic/rack/browser_monitoring_test.rb +14 -1
  261. data/test/new_relic/rack/developer_mode_helper_test.rb +0 -8
  262. data/test/new_relic/rack/developer_mode_test.rb +1 -1
  263. data/test/new_relic/rack/error_collector_test.rb +6 -30
  264. data/test/new_relic/transaction_sample/fake_segment_test.rb +2 -2
  265. data/test/new_relic/transaction_sample/segment_test.rb +47 -47
  266. data/test/new_relic/transaction_sample_test.rb +9 -8
  267. data/test/performance/lib/performance/instrumentation/stackprof.rb +11 -8
  268. data/test/performance/script/runner +13 -0
  269. data/test/performance/suites/config.rb +5 -2
  270. data/test/performance/suites/rack_middleware.rb +84 -0
  271. data/test/performance/suites/rum_autoinsertion.rb +1 -1
  272. data/test/performance/suites/thread_profiling.rb +1 -1
  273. data/test/test_helper.rb +12 -10
  274. data/ui/helpers/developer_mode_helper.rb +3 -43
  275. data/ui/views/layouts/newrelic_default.rhtml +2 -2
  276. data/ui/views/newrelic/_sample.rhtml +2 -2
  277. data/ui/views/newrelic/_sql_row.rhtml +11 -11
  278. data/ui/views/newrelic/index.rhtml +21 -22
  279. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +15 -10
  280. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +2 -0
  281. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/middleware.rb +34 -0
  282. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/nested.rb +24 -0
  283. metadata +108 -31
  284. metadata.gz.sig +0 -0
  285. data/lib/new_relic/rack/transaction_reset.rb +0 -20
  286. data/test/multiverse/suites/rails/mongrel_queue_depth_test.rb +0 -42
  287. data/test/new_relic/rack/transaction_reset_test.rb +0 -35
  288. data/ui/views/newrelic/show_source.rhtml +0 -3
@@ -26,7 +26,7 @@ DependencyDetection.defer do
26
26
  gateway_name = self.name.split('::').last
27
27
  [:authorize, :purchase, :credit, :void, :capture, :recurring, :store, :unstore, :update].each do |operation|
28
28
  if implemented_methods.include?(operation)
29
- add_method_tracer operation, "ActiveMerchant/gateway/#{gateway_name}/#{operation}", :scoped_metric_only => true
29
+ add_method_tracer operation, "ActiveMerchant/gateway/#{gateway_name}/#{operation}"
30
30
  add_method_tracer operation, "ActiveMerchant/gateway/#{gateway_name}", :push_scope => false
31
31
  add_method_tracer operation, "ActiveMerchant/operation/#{operation}", :push_scope => false
32
32
  end
@@ -26,8 +26,10 @@ module NewRelic
26
26
  end
27
27
  end
28
28
 
29
- def log_with_newrelic_instrumentation(*args, &block)
30
- if !NewRelic::Agent.is_execution_traced?
29
+ def log_with_newrelic_instrumentation(*args, &block) #THREAD_LOCAL_ACCESS
30
+ state = NewRelic::Agent::TransactionState.tl_get
31
+
32
+ if !state.is_execution_traced?
31
33
  return log_without_newrelic_instrumentation(*args, &block)
32
34
  end
33
35
 
@@ -49,10 +51,10 @@ module NewRelic
49
51
 
50
52
  NewRelic::Agent.instance.transaction_sampler.notice_sql(sql,
51
53
  @config, elapsed_time,
52
- &EXPLAINER)
54
+ state, &EXPLAINER)
53
55
  NewRelic::Agent.instance.sql_sampler.notice_sql(sql, metric,
54
56
  @config, elapsed_time,
55
- &EXPLAINER)
57
+ state, &EXPLAINER)
56
58
  end
57
59
  end
58
60
  end
@@ -31,8 +31,9 @@ module NewRelic
31
31
  end
32
32
  end
33
33
 
34
- def metric_for_sql(sql)
35
- metric = NewRelic::Agent::Transaction.database_metric_name
34
+ def metric_for_sql(sql) #THREAD_LOCAL_ACCESS
35
+ txn = NewRelic::Agent::Transaction.tl_current
36
+ metric = txn && txn.database_metric_name
36
37
  if metric.nil?
37
38
  operation = NewRelic::Agent::Database.parse_operation_from_query(sql)
38
39
  if operation
@@ -11,18 +11,19 @@ module NewRelic
11
11
  module Agent
12
12
  module Instrumentation
13
13
  class ActiveRecordSubscriber < EventedSubscriber
14
- def start(name, id, payload)
15
- return unless NewRelic::Agent.is_execution_traced?
14
+ def start(name, id, payload) #THREAD_LOCAL_ACCESS
15
+ return unless NewRelic::Agent.tl_is_execution_traced?
16
16
  super
17
17
  rescue => e
18
18
  log_notification_error(e, name, 'start')
19
19
  end
20
20
 
21
- def finish(name, id, payload)
22
- return unless NewRelic::Agent.is_execution_traced?
21
+ def finish(name, id, payload) #THREAD_LOCAL_ACCESS
22
+ state = NewRelic::Agent::TransactionState.tl_get
23
+ return unless state.is_execution_traced?
23
24
  event = pop_event(id)
24
25
  record_metrics(event)
25
- notice_sql(event)
26
+ notice_sql(state, event)
26
27
  rescue => e
27
28
  log_notification_error(e, name, 'finish')
28
29
  end
@@ -37,44 +38,41 @@ module NewRelic
37
38
  end
38
39
  end
39
40
 
40
- def notice_sql(event)
41
+ def notice_sql(state, event)
42
+ stack = state.traced_method_stack
41
43
  config = active_record_config_for_event(event)
42
44
  metric = base_metric(event)
43
45
 
44
46
  # enter transaction trace segment
45
- frame = NewRelic::Agent::TracedMethodStack.push_frame(:active_record, event.time)
47
+ frame = stack.push_frame(state, :active_record, event.time)
46
48
 
47
49
  NewRelic::Agent.instance.transaction_sampler \
48
50
  .notice_sql(event.payload[:sql], config,
49
51
  Helper.milliseconds_to_seconds(event.duration),
50
- &method(:get_explain_plan))
52
+ state, &method(:get_explain_plan))
51
53
 
52
54
  NewRelic::Agent.instance.sql_sampler \
53
55
  .notice_sql(event.payload[:sql], metric, config,
54
56
  Helper.milliseconds_to_seconds(event.duration),
55
- &method(:get_explain_plan))
57
+ state, &method(:get_explain_plan))
56
58
 
57
59
  # exit transaction trace segment
58
- NewRelic::Agent::TracedMethodStack.pop_frame(frame, metric, event.end)
60
+ stack.pop_frame(state, frame, metric, event.end)
59
61
  end
60
62
 
61
- def record_metrics(event)
63
+ def record_metrics(event) #THREAD_LOCAL_ACCESS
62
64
  base = base_metric(event)
63
- NewRelic::Agent.instance.stats_engine.record_metrics(base,
64
- Helper.milliseconds_to_seconds(event.duration),
65
- :scoped => true)
66
65
 
67
66
  other_metrics = ActiveRecordHelper.rollup_metrics_for(base)
68
-
69
67
  if config = active_record_config_for_event(event)
70
68
  other_metrics << ActiveRecordHelper.remote_service_metric(config[:adapter], config[:host])
71
69
  end
70
+ other_metrics.compact!
72
71
 
73
- other_metrics.compact.each do |metric_name|
74
- NewRelic::Agent.instance.stats_engine.record_metrics(metric_name,
75
- Helper.milliseconds_to_seconds(event.duration),
76
- :scoped => false)
77
- end
72
+ NewRelic::Agent.instance.stats_engine.tl_record_scoped_and_unscoped_metrics(
73
+ base, other_metrics,
74
+ Helper.milliseconds_to_seconds(event.duration)
75
+ )
78
76
  end
79
77
 
80
78
  def base_metric(event)
@@ -45,6 +45,11 @@ module NewRelic
45
45
  def perform_action_with_newrelic_trace(*args); yield; end
46
46
  end
47
47
 
48
+ NR_DO_NOT_TRACE_KEY = 'do_not_trace'.freeze unless defined?(NR_DO_NOT_TRACE_KEY)
49
+ NR_IGNORE_APDEX_KEY = 'ignore_apdex'.freeze unless defined?(NR_IGNORE_APDEX_KEY)
50
+ NR_IGNORE_ENDUSER_KEY = 'ignore_enduser'.freeze unless defined?(NR_IGNORE_ENDUSER_KEY)
51
+ NR_DEFAULT_OPTIONS = {}.freeze unless defined?(NR_DEFAULT_OPTIONS)
52
+
48
53
  # @api public
49
54
  module ClassMethods
50
55
  # Have NewRelic ignore actions in this controller. Specify the actions as hash options
@@ -53,7 +58,7 @@ module NewRelic
53
58
  # @api public
54
59
  #
55
60
  def newrelic_ignore(specifiers={})
56
- newrelic_ignore_aspect('do_not_trace', specifiers)
61
+ newrelic_ignore_aspect(NR_DO_NOT_TRACE_KEY, specifiers)
57
62
  end
58
63
  # Have NewRelic omit apdex measurements on the given actions. Typically used for
59
64
  # actions that are not user facing or that skew your overall apdex measurement.
@@ -62,12 +67,12 @@ module NewRelic
62
67
  # @api public
63
68
  #
64
69
  def newrelic_ignore_apdex(specifiers={})
65
- newrelic_ignore_aspect('ignore_apdex', specifiers)
70
+ newrelic_ignore_aspect(NR_IGNORE_APDEX_KEY, specifiers)
66
71
  end
67
72
 
68
73
  # @api public
69
74
  def newrelic_ignore_enduser(specifiers={})
70
- newrelic_ignore_aspect('ignore_enduser', specifiers)
75
+ newrelic_ignore_aspect(NR_IGNORE_ENDUSER_KEY, specifiers)
71
76
  end
72
77
 
73
78
  def newrelic_ignore_aspect(property, specifiers={}) # :nodoc:
@@ -207,52 +212,47 @@ module NewRelic
207
212
  end
208
213
 
209
214
  class TransactionNamer
210
- def self.category_name(type = nil)
211
- type ||= Transaction.current && Transaction.current.type
212
- case type
213
- when :controller, nil then 'Controller'
214
- when :task then 'OtherTransaction/Background'
215
- when :rack then 'Controller/Rack'
216
- when :uri then 'Controller'
217
- when :sinatra then 'Controller/Sinatra'
218
- # for internal use only
219
- else type.to_s
220
- end
215
+ def self.name(txn, traced_obj, category, options={})
216
+ "#{prefix_for_category(txn, category)}#{path_name(traced_obj, options)}"
221
217
  end
222
218
 
223
- def initialize(traced_obj)
224
- @traced_obj = traced_obj
225
- if (@traced_obj.is_a?(Class) || @traced_obj.is_a?(Module))
226
- @traced_class_name = @traced_obj.name
227
- else
228
- @traced_class_name = @traced_obj.class.name
219
+ def self.prefix_for_category(txn, category = nil)
220
+ category ||= (txn && txn.best_category)
221
+ case category
222
+ when :controller then ::NewRelic::Agent::Transaction::CONTROLLER_PREFIX
223
+ when :task then ::NewRelic::Agent::Transaction::TASK_PREFIX
224
+ when :rack then ::NewRelic::Agent::Transaction::RACK_PREFIX
225
+ when :uri then ::NewRelic::Agent::Transaction::CONTROLLER_PREFIX
226
+ when :sinatra then ::NewRelic::Agent::Transaction::SINATRA_PREFIX
227
+ when :middleware then ::NewRelic::Agent::Transaction::MIDDLEWARE_PREFIX
228
+ else "#{category.to_s}/" # for internal use only
229
229
  end
230
230
  end
231
231
 
232
- def name(options={})
233
- name = "#{self.class.category_name(options[:category])}/#{path_name(options)}"
234
- end
232
+ def self.path_name(traced_obj, options={})
233
+ return options[:path] if options[:path]
235
234
 
236
- def path_name(options={})
237
- # if we have the path, use the path
238
- path = options[:path]
239
-
240
- class_name = options[:class_name] || @traced_class_name
241
-
242
- # if we have an explicit action name option, use that
235
+ class_name = class_name(traced_obj, options)
243
236
  if options[:name]
244
- path ||= [ class_name, options[:name] ].compact.join('/')
237
+ if class_name
238
+ "#{class_name}/#{options[:name]}"
239
+ else
240
+ options[:name]
241
+ end
242
+ elsif traced_obj.respond_to?(:newrelic_metric_path)
243
+ traced_obj.newrelic_metric_path
244
+ else
245
+ class_name
245
246
  end
247
+ end
246
248
 
247
- # if newrelic_metric_path() is defined, use that
248
- if @traced_obj.respond_to?(:newrelic_metric_path)
249
- path ||= @traced_obj.newrelic_metric_path
249
+ def self.class_name(traced_obj, options={})
250
+ return options[:class_name] if options[:class_name]
251
+ if (traced_obj.is_a?(Class) || traced_obj.is_a?(Module))
252
+ traced_obj.name
253
+ else
254
+ traced_obj.class.name
250
255
  end
251
-
252
- # fall back on just the traced class name
253
- path ||= class_name
254
-
255
- return path
256
256
  end
257
257
  end
258
258
 
@@ -298,7 +298,7 @@ module NewRelic
298
298
  # * <tt>:category => :task</tt> indicates that this is a
299
299
  # background task and will show up in New Relic with other background
300
300
  # tasks instead of in the controllers list
301
- # * <tt>:category => :rack</tt> if you are instrumenting a rack
301
+ # * <tt>:category => :middleware</tt> if you are instrumenting a rack
302
302
  # middleware call. The <tt>:name</tt> is optional, useful if you
303
303
  # have more than one potential transaction in the #call.
304
304
  # * <tt>:category => :uri</tt> indicates that this is a
@@ -323,34 +323,41 @@ module NewRelic
323
323
  # * <tt>:request => Rack::Request#new(env)</tt> is used to pass in a
324
324
  # request object that may respond to uri and referer.
325
325
  #
326
- # If a single argument is passed in, it is treated as a metric
327
- # path. This form is deprecated.
328
- #
329
326
  # @api public
330
327
  #
331
- def perform_action_with_newrelic_trace(*args, &block)
332
- NewRelic::Agent::TransactionState.request = newrelic_request(args)
328
+ def perform_action_with_newrelic_trace(*args, &block) #THREAD_LOCAL_ACCESS
329
+ state = NewRelic::Agent::TransactionState.tl_get
330
+ state.request = newrelic_request(args)
333
331
 
334
332
  # Skip instrumentation based on the value of 'do_not_trace' and if
335
333
  # we aren't calling directly with a block.
336
334
  if !block_given? && do_not_trace?
337
- # Also ignore all instrumentation in the call sequence
335
+ state.current_transaction.ignore! if state.current_transaction
338
336
  NewRelic::Agent.disable_all_tracing do
339
337
  return perform_action_without_newrelic_trace(*args)
340
338
  end
341
339
  end
342
340
 
341
+ return yield unless state.is_execution_traced?
342
+
343
343
  # If a block was passed in, then the arguments represent options for
344
344
  # the instrumentation, not app method arguments.
345
- txn_options = create_transaction_options(block_given? ? args : [])
346
- return yield unless NewRelic::Agent.is_execution_traced?
345
+ trace_options = NR_DEFAULT_OPTIONS
346
+
347
+ if block_given?
348
+ trace_options = args.last if args.last.is_a?(Hash)
349
+ available_params = trace_options[:params]
350
+ else
351
+ available_params = respond_to?(:params) && params
352
+ end
347
353
 
348
- txn_options[:transaction_name] = TransactionNamer.new(self).name(txn_options)
349
- txn_options[:apdex_start_time] = detect_queue_start_time
354
+ category = trace_options[:category] || :controller
355
+ txn_options = create_transaction_options(trace_options, available_params)
356
+ txn_options[:transaction_name] = TransactionNamer.name(nil, self, category, trace_options)
357
+ txn_options[:apdex_start_time] = detect_queue_start_time(state)
350
358
 
351
359
  begin
352
- txn = Transaction.start(txn_options[:category], txn_options)
353
- _record_queue_length
360
+ txn = Transaction.start(state, category, txn_options)
354
361
 
355
362
  begin
356
363
  if block_given?
@@ -364,9 +371,11 @@ module NewRelic
364
371
  end
365
372
 
366
373
  ensure
367
- Transaction.stop(Time.now,
368
- :ignore_apdex => ignore_apdex?,
369
- :ignore_enduser => ignore_enduser?)
374
+ if txn
375
+ txn.ignore_apdex! if ignore_apdex?
376
+ txn.ignore_enduser! if ignore_enduser?
377
+ end
378
+ Transaction.stop(state)
370
379
  end
371
380
  end
372
381
 
@@ -377,10 +386,6 @@ module NewRelic
377
386
  # passed as a parameter to add_transaction_tracer
378
387
  if opts.respond_to?(:keys) && opts.respond_to?(:[]) && opts[:request]
379
388
  opts[:request]
380
- # in a Rack app
381
- elsif opts.respond_to?(:keys) && opts.respond_to?(:[]) &&
382
- opts['rack.version']
383
- Rack::Request.new(args)
384
389
  # in a Rails app
385
390
  elsif self.respond_to?(:request)
386
391
  self.request
@@ -390,8 +395,8 @@ module NewRelic
390
395
  # Should be implemented in the dispatcher class
391
396
  def newrelic_response_code; end
392
397
 
393
- def newrelic_request_headers
394
- request = NewRelic::Agent::TransactionState.get.request
398
+ def newrelic_request_headers(state)
399
+ request = state.request
395
400
  if request
396
401
  if request.respond_to?(:headers)
397
402
  request.headers
@@ -405,7 +410,7 @@ module NewRelic
405
410
  # or not - you may override this in your controller and supply
406
411
  # your own logic for ignoring transactions.
407
412
  def do_not_trace?
408
- _is_filtered?('do_not_trace')
413
+ _is_filtered?(NR_DO_NOT_TRACE_KEY)
409
414
  end
410
415
 
411
416
  # overrideable method to determine whether to trace an action
@@ -413,29 +418,23 @@ module NewRelic
413
418
  # ignore things like api calls or other fast non-user-facing
414
419
  # actions
415
420
  def ignore_apdex?
416
- _is_filtered?('ignore_apdex')
421
+ _is_filtered?(NR_IGNORE_APDEX_KEY)
417
422
  end
418
423
 
419
424
  def ignore_enduser?
420
- _is_filtered?('ignore_enduser')
425
+ _is_filtered?(NR_IGNORE_ENDUSER_KEY)
421
426
  end
422
427
 
423
428
  private
424
429
 
425
- def create_transaction_options(txn_args)
430
+ def create_transaction_options(trace_options, available_params)
426
431
  txn_options = {}
427
- if txn_args.any?
428
- if txn_args.last.is_a?(Hash)
429
- txn_options = txn_args.pop
430
- end
431
- available_params = txn_options[:params] || {}
432
- txn_options[:name] ||= txn_args.first
433
- else
434
- available_params = self.respond_to?(:params) ? self.params : {}
432
+ txn_options[:request] = trace_options[:request] || (respond_to?(:request) && request)
433
+
434
+ if available_params
435
+ txn_options[:filtered_params] = (respond_to?(:filter_parameters)) ? filter_parameters(available_params) : available_params
435
436
  end
436
437
 
437
- txn_options[:request] ||= self.request if self.respond_to? :request
438
- txn_options[:filtered_params] = (respond_to? :filter_parameters) ? filter_parameters(available_params) : available_params
439
438
  txn_options
440
439
  end
441
440
 
@@ -446,31 +445,18 @@ module NewRelic
446
445
  case ignore_actions
447
446
  when nil; false
448
447
  when Hash
449
- only_actions = Array(ignore_actions[:only])
448
+ only_actions = Array(ignore_actions[:only])
450
449
  except_actions = Array(ignore_actions[:except])
451
450
  only_actions.include?(action_name.to_sym) || (except_actions.any? && !except_actions.include?(action_name.to_sym))
452
451
  else
453
452
  true
454
453
  end
455
454
  end
456
- # Take a guess at a measure representing the number of requests waiting in mongrel
457
- # or heroku.
458
- def _record_queue_length
459
- if newrelic_request_headers
460
- if queue_depth = newrelic_request_headers['HTTP_X_HEROKU_QUEUE_DEPTH']
461
- queue_depth = queue_depth.to_i rescue nil
462
- elsif mongrel = NewRelic::Control.instance.local_env.mongrel
463
- # Always subtrace 1 for the active mongrel
464
- queue_depth = [mongrel.workers.list.length.to_i - 1, 0].max rescue nil
465
- end
466
- NewRelic::Agent.record_metric('Mongrel/Queue Length', queue_depth) if queue_depth
467
- end
468
- end
469
455
 
470
- def detect_queue_start_time
471
- if newrelic_request_headers
472
- QueueTime.parse_frontend_timestamp(newrelic_request_headers)
473
- end
456
+ def detect_queue_start_time(state)
457
+ headers = newrelic_request_headers(state)
458
+
459
+ QueueTime.parse_frontend_timestamp(headers) if headers
474
460
  end
475
461
  end
476
462
  end
@@ -92,9 +92,9 @@ DependencyDetection.defer do
92
92
  include NewRelic::Agent::MethodTracer
93
93
 
94
94
  # Add CAT with callbacks if the request is serial
95
- def add_with_newrelic( curl )
96
- if curl.respond_to?( :_nr_serial ) && curl._nr_serial
97
- hook_pending_request( curl ) if NewRelic::Agent.is_execution_traced?
95
+ def add_with_newrelic(curl) #THREAD_LOCAL_ACCESS
96
+ if curl.respond_to?(:_nr_serial) && curl._nr_serial
97
+ hook_pending_request(curl) if NewRelic::Agent.tl_is_execution_traced?
98
98
  end
99
99
 
100
100
  return add_without_newrelic( curl )
@@ -191,8 +191,9 @@ module NewRelic
191
191
  # We rely on the assumption that all possible entry points have been
192
192
  # hooked with tracers, ensuring that notice_sql attaches this SQL to
193
193
  # the proper call scope.
194
- def log(msg)
195
- return unless NewRelic::Agent.is_execution_traced?
194
+ def log(msg) #THREAD_LOCAL_ACCESS
195
+ state = NewRelic::Agent::TransactionState.tl_get
196
+ return unless state.is_execution_traced?
196
197
  return unless operation = case NewRelic::Helper.correctly_encoded(msg.query)
197
198
  when /^\s*select/i then 'find'
198
199
  when /^\s*(update|insert)/i then 'save'
@@ -204,13 +205,13 @@ module NewRelic
204
205
  duration = msg.duration / 1000000.0
205
206
 
206
207
  # Attach SQL to current segment/scope.
207
- NewRelic::Agent.instance.transaction_sampler.notice_sql(msg.query, nil, duration)
208
+ NewRelic::Agent.instance.transaction_sampler.notice_sql(msg.query, nil, duration, state)
208
209
 
209
210
  # Record query duration associated with each of the desired metrics.
210
211
  metric = "ActiveRecord/#{operation}"
211
212
  rollup_metrics = ActiveRecordHelper.rollup_metrics_for(metric)
212
213
  metrics = [metric] + rollup_metrics
213
- NewRelic::Agent.instance.stats_engine.record_metrics(metrics, duration)
214
+ NewRelic::Agent.instance.stats_engine.tl_record_unscoped_metrics(metrics, duration)
214
215
  ensure
215
216
  super
216
217
  end