wd_newrelic_rpm 3.3.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. data/CHANGELOG +591 -0
  2. data/LICENSE +64 -0
  3. data/README.rdoc +179 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +118 -0
  8. data/cert/oldsite.pem +28 -0
  9. data/cert/site.pem +27 -0
  10. data/install.rb +9 -0
  11. data/lib/conditional_vendored_dependency_detection.rb +3 -0
  12. data/lib/conditional_vendored_metric_parser.rb +5 -0
  13. data/lib/new_relic/agent.rb +467 -0
  14. data/lib/new_relic/agent/agent.rb +1325 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +121 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +142 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +99 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/database.rb +223 -0
  20. data/lib/new_relic/agent/error_collector.rb +251 -0
  21. data/lib/new_relic/agent/instrumentation.rb +9 -0
  22. data/lib/new_relic/agent/instrumentation/active_merchant.rb +29 -0
  23. data/lib/new_relic/agent/instrumentation/active_record.rb +137 -0
  24. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +68 -0
  25. data/lib/new_relic/agent/instrumentation/authlogic.rb +19 -0
  26. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +443 -0
  27. data/lib/new_relic/agent/instrumentation/data_mapper.rb +238 -0
  28. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +52 -0
  29. data/lib/new_relic/agent/instrumentation/memcache.rb +80 -0
  30. data/lib/new_relic/agent/instrumentation/merb/controller.rb +41 -0
  31. data/lib/new_relic/agent/instrumentation/merb/errors.rb +29 -0
  32. data/lib/new_relic/agent/instrumentation/metric_frame.rb +353 -0
  33. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +80 -0
  34. data/lib/new_relic/agent/instrumentation/net.rb +29 -0
  35. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +37 -0
  36. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  37. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  38. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +114 -0
  39. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +42 -0
  40. data/lib/new_relic/agent/instrumentation/rails/errors.rb +42 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +180 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +37 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +78 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +29 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +21 -0
  46. data/lib/new_relic/agent/method_tracer.rb +528 -0
  47. data/lib/new_relic/agent/sampler.rb +50 -0
  48. data/lib/new_relic/agent/samplers/cpu_sampler.rb +58 -0
  49. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +40 -0
  50. data/lib/new_relic/agent/samplers/memory_sampler.rb +143 -0
  51. data/lib/new_relic/agent/samplers/object_sampler.rb +26 -0
  52. data/lib/new_relic/agent/shim_agent.rb +29 -0
  53. data/lib/new_relic/agent/sql_sampler.rb +286 -0
  54. data/lib/new_relic/agent/stats_engine.rb +26 -0
  55. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  56. data/lib/new_relic/agent/stats_engine/metric_stats.rb +187 -0
  57. data/lib/new_relic/agent/stats_engine/samplers.rb +95 -0
  58. data/lib/new_relic/agent/stats_engine/transactions.rb +125 -0
  59. data/lib/new_relic/agent/transaction_info.rb +74 -0
  60. data/lib/new_relic/agent/transaction_sample_builder.rb +116 -0
  61. data/lib/new_relic/agent/transaction_sampler.rb +468 -0
  62. data/lib/new_relic/agent/worker_loop.rb +89 -0
  63. data/lib/new_relic/collection_helper.rb +77 -0
  64. data/lib/new_relic/command.rb +85 -0
  65. data/lib/new_relic/commands/deployments.rb +105 -0
  66. data/lib/new_relic/commands/install.rb +80 -0
  67. data/lib/new_relic/control.rb +46 -0
  68. data/lib/new_relic/control/class_methods.rb +53 -0
  69. data/lib/new_relic/control/configuration.rb +206 -0
  70. data/lib/new_relic/control/frameworks.rb +10 -0
  71. data/lib/new_relic/control/frameworks/external.rb +16 -0
  72. data/lib/new_relic/control/frameworks/merb.rb +31 -0
  73. data/lib/new_relic/control/frameworks/rails.rb +164 -0
  74. data/lib/new_relic/control/frameworks/rails3.rb +75 -0
  75. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  76. data/lib/new_relic/control/frameworks/sinatra.rb +20 -0
  77. data/lib/new_relic/control/instance_methods.rb +179 -0
  78. data/lib/new_relic/control/instrumentation.rb +100 -0
  79. data/lib/new_relic/control/logging_methods.rb +143 -0
  80. data/lib/new_relic/control/profiling.rb +25 -0
  81. data/lib/new_relic/control/server_methods.rb +114 -0
  82. data/lib/new_relic/data_serialization.rb +151 -0
  83. data/lib/new_relic/delayed_job_injection.rb +51 -0
  84. data/lib/new_relic/language_support.rb +73 -0
  85. data/lib/new_relic/local_environment.rb +428 -0
  86. data/lib/new_relic/merbtasks.rb +6 -0
  87. data/lib/new_relic/metric_data.rb +51 -0
  88. data/lib/new_relic/metric_spec.rb +76 -0
  89. data/lib/new_relic/metrics.rb +9 -0
  90. data/lib/new_relic/noticed_error.rb +29 -0
  91. data/lib/new_relic/rack/browser_monitoring.rb +76 -0
  92. data/lib/new_relic/rack/developer_mode.rb +268 -0
  93. data/lib/new_relic/recipes.rb +77 -0
  94. data/lib/new_relic/stats.rb +335 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +77 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +49 -0
  98. data/lib/new_relic/transaction_sample.rb +261 -0
  99. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  100. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  101. data/lib/new_relic/transaction_sample/segment.rb +203 -0
  102. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  103. data/lib/new_relic/url_rule.rb +14 -0
  104. data/lib/new_relic/version.rb +55 -0
  105. data/lib/newrelic_rpm.rb +49 -0
  106. data/lib/tasks/all.rb +4 -0
  107. data/lib/tasks/install.rake +7 -0
  108. data/lib/tasks/tests.rake +19 -0
  109. data/newrelic.yml +265 -0
  110. data/newrelic_rpm.gemspec +312 -0
  111. data/recipes/newrelic.rb +6 -0
  112. data/test/active_record_fixtures.rb +77 -0
  113. data/test/config/newrelic.yml +48 -0
  114. data/test/config/test_control.rb +48 -0
  115. data/test/fixtures/proc_cpuinfo.txt +575 -0
  116. data/test/new_relic/agent/agent/connect_test.rb +403 -0
  117. data/test/new_relic/agent/agent/start_test.rb +255 -0
  118. data/test/new_relic/agent/agent/start_worker_thread_test.rb +153 -0
  119. data/test/new_relic/agent/agent_test.rb +140 -0
  120. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  121. data/test/new_relic/agent/agent_test_controller_test.rb +382 -0
  122. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  123. data/test/new_relic/agent/beacon_configuration_test.rb +111 -0
  124. data/test/new_relic/agent/browser_monitoring_test.rb +323 -0
  125. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  126. data/test/new_relic/agent/database_test.rb +149 -0
  127. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  128. data/test/new_relic/agent/error_collector_test.rb +192 -0
  129. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +576 -0
  130. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +34 -0
  131. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  132. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +171 -0
  133. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  134. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  135. data/test/new_relic/agent/instrumentation/queue_time_test.rb +382 -0
  136. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  137. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  138. data/test/new_relic/agent/memcache_instrumentation_test.rb +143 -0
  139. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  140. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  141. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  142. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  143. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  144. data/test/new_relic/agent/sampler_test.rb +19 -0
  145. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  146. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  147. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  148. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +82 -0
  149. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  150. data/test/new_relic/agent/stats_engine_test.rb +220 -0
  151. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  152. data/test/new_relic/agent/transaction_sample_builder_test.rb +219 -0
  153. data/test/new_relic/agent/transaction_sampler_test.rb +967 -0
  154. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  155. data/test/new_relic/agent_test.rb +187 -0
  156. data/test/new_relic/collection_helper_test.rb +149 -0
  157. data/test/new_relic/command/deployments_test.rb +68 -0
  158. data/test/new_relic/control/class_methods_test.rb +62 -0
  159. data/test/new_relic/control/configuration_test.rb +84 -0
  160. data/test/new_relic/control/logging_methods_test.rb +185 -0
  161. data/test/new_relic/control_test.rb +256 -0
  162. data/test/new_relic/data_serialization_test.rb +208 -0
  163. data/test/new_relic/delayed_job_injection_test.rb +16 -0
  164. data/test/new_relic/local_environment_test.rb +85 -0
  165. data/test/new_relic/metric_data_test.rb +125 -0
  166. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  167. data/test/new_relic/metric_spec_test.rb +95 -0
  168. data/test/new_relic/rack/all_test.rb +11 -0
  169. data/test/new_relic/rack/browser_monitoring_test.rb +142 -0
  170. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  171. data/test/new_relic/rack/developer_mode_test.rb +74 -0
  172. data/test/new_relic/stats_test.rb +411 -0
  173. data/test/new_relic/transaction_analysis/segment_summary_test.rb +91 -0
  174. data/test/new_relic/transaction_analysis_test.rb +121 -0
  175. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  176. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  177. data/test/new_relic/transaction_sample/segment_test.rb +389 -0
  178. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  179. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  180. data/test/new_relic/transaction_sample_test.rb +177 -0
  181. data/test/new_relic/version_number_test.rb +89 -0
  182. data/test/script/build_test_gem.sh +51 -0
  183. data/test/script/ci.sh +94 -0
  184. data/test/script/ci_bench.sh +52 -0
  185. data/test/test_contexts.rb +29 -0
  186. data/test/test_helper.rb +155 -0
  187. data/ui/helpers/developer_mode_helper.rb +357 -0
  188. data/ui/helpers/google_pie_chart.rb +48 -0
  189. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  190. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  191. data/ui/views/newrelic/_sample.rhtml +20 -0
  192. data/ui/views/newrelic/_segment.rhtml +28 -0
  193. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  194. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  195. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  196. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  197. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  198. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  199. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  200. data/ui/views/newrelic/_table.rhtml +12 -0
  201. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  202. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  203. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  204. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  205. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  206. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  207. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  208. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  209. data/ui/views/newrelic/file/images/textmate.png +0 -0
  210. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  211. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  212. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  213. data/ui/views/newrelic/index.rhtml +71 -0
  214. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  215. data/ui/views/newrelic/show_sample.rhtml +80 -0
  216. data/ui/views/newrelic/show_source.rhtml +3 -0
  217. data/ui/views/newrelic/threads.rhtml +53 -0
  218. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +5 -0
  219. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +67 -0
  220. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +135 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  255. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  256. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  257. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  258. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  259. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  260. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +70 -0
  261. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  262. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  263. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  264. metadata +376 -0
@@ -0,0 +1,576 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::Instrumentation::ActiveRecordInstrumentationTest < Test::Unit::TestCase
4
+ require 'active_record_fixtures'
5
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
6
+
7
+ def setup
8
+ super
9
+ NewRelic::Agent.manual_start
10
+ ActiveRecordFixtures.setup
11
+ NewRelic::Agent.instance.transaction_sampler.reset!
12
+ NewRelic::Agent.instance.stats_engine.clear_stats
13
+ rescue => e
14
+ puts e
15
+ puts e.backtrace.join("\n")
16
+ end
17
+
18
+ def teardown
19
+ super
20
+ NewRelic::Agent.shutdown
21
+ end
22
+
23
+ #####################################################################
24
+ # Note: If these tests are failing, most likely the problem is that #
25
+ # the active record instrumentation is not loading for whichever #
26
+ # version of rails you're testing at the moment. #
27
+ #####################################################################
28
+
29
+ def test_agent_setup
30
+ assert NewRelic::Agent.instance.class == NewRelic::Agent::Agent
31
+ end
32
+
33
+ def test_finder
34
+ ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
35
+ find_metric = "ActiveRecord/ActiveRecordFixtures::Order/find"
36
+
37
+ assert_calls_metrics(find_metric) do
38
+ ActiveRecordFixtures::Order.find(:all)
39
+ check_metric_count(find_metric, 1)
40
+ ActiveRecordFixtures::Order.find_all_by_name "jeff"
41
+ check_metric_count(find_metric, 2)
42
+ end
43
+ end
44
+
45
+ def test_exists
46
+ return if NewRelic::Control.instance.rails_version < "2.3.4" ||
47
+ NewRelic::Control.instance.rails_version >= "3.0.7"
48
+
49
+ ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
50
+
51
+ find_metric = "ActiveRecord/ActiveRecordFixtures::Order/find"
52
+
53
+ assert_calls_metrics(find_metric) do
54
+ ActiveRecordFixtures::Order.exists?(["name=?", 'jeff'])
55
+ check_metric_count(find_metric, 1)
56
+ end
57
+ end
58
+
59
+ # multiple duplicate find calls should only cause metric trigger on the first
60
+ # call. the others are ignored.
61
+ def test_query_cache
62
+ # Not sure why we get a transaction error with sqlite
63
+ return if isSqlite?
64
+
65
+ find_metric = "ActiveRecord/ActiveRecordFixtures::Order/find"
66
+ ActiveRecordFixtures::Order.cache do
67
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
68
+ assert_calls_metrics(find_metric) do
69
+ ActiveRecordFixtures::Order.find(:all)
70
+ end
71
+
72
+ check_metric_count(find_metric, 1)
73
+
74
+ assert_calls_metrics(find_metric) do
75
+ 10.times { ActiveRecordFixtures::Order.find m.id }
76
+ end
77
+ check_metric_count(find_metric, 2)
78
+ end
79
+ end
80
+
81
+ def test_metric_names_jruby
82
+ # fails due to a bug in rails 3 - log does not provide the correct
83
+ # transaction type - it returns 'SQL' instead of 'Foo Create', for example.
84
+ return if rails3? || !defined?(JRuby)
85
+ expected = %W[
86
+ ActiveRecord/all
87
+ ActiveRecord/find
88
+ ActiveRecord/ActiveRecordFixtures::Order/find
89
+ Database/SQL/insert
90
+ RemoteService/sql/mysql/localhost
91
+ ]
92
+
93
+ if NewRelic::Control.instance.rails_version < '2.1.0'
94
+ expected += %W[ActiveRecord/save ActiveRecord/ActiveRecordFixtures::Order/save]
95
+ end
96
+
97
+ assert_calls_metrics(*expected) do
98
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
99
+ m = ActiveRecordFixtures::Order.find(m.id)
100
+ m.id = 999
101
+ m.save!
102
+ end
103
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
104
+
105
+ compare_metrics expected, metrics
106
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
107
+ # zero because jruby uses a different mysql adapter
108
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/create", 0)
109
+ end
110
+
111
+ def test_metric_names_sqlite
112
+ # fails due to a bug in rails 3 - log does not provide the correct
113
+ # transaction type - it returns 'SQL' instead of 'Foo Create', for example.
114
+ return if rails3? || !isSqlite? || defined?(JRuby)
115
+
116
+ expected = %W[
117
+ ActiveRecord/all
118
+ ActiveRecord/find
119
+ ActiveRecord/ActiveRecordFixtures::Order/find
120
+ ActiveRecord/create
121
+ ActiveRecord/ActiveRecordFixtures::Order/create]
122
+
123
+ if NewRelic::Control.instance.rails_version < '2.1.0'
124
+ expected += %W[ActiveRecord/save ActiveRecord/ActiveRecordFixtures::Order/save]
125
+ end
126
+
127
+ assert_calls_metrics(*expected) do
128
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'jeff'
129
+ m = ActiveRecordFixtures::Order.find(m.id)
130
+ m.id = 999
131
+ m.save!
132
+ end
133
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
134
+
135
+ compare_metrics expected, metrics
136
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
137
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/create", 1)
138
+ end
139
+
140
+ def test_metric_names_standard
141
+ # fails due to a bug in rails 3 - log does not provide the correct
142
+ # transaction type - it returns 'SQL' instead of 'Foo Create', for example.
143
+ return if defined?(JRuby) || isSqlite?
144
+
145
+ expected = %W[
146
+ ActiveRecord/all
147
+ ActiveRecord/find
148
+ ActiveRecord/create
149
+ ActiveRecord/ActiveRecordFixtures::Order/find
150
+ ActiveRecord/ActiveRecordFixtures::Order/create
151
+ Database/SQL/other
152
+ RemoteService/sql/mysql/localhost]
153
+
154
+ if NewRelic::Control.instance.rails_version < '2.1.0'
155
+ expected += ['ActiveRecord/save',
156
+ 'ActiveRecord/ActiveRecordFixtures::Order/save']
157
+ elsif NewRelic::Control.instance.rails_version >= '3.0.0'
158
+ expected << 'Database/SQL/insert'
159
+ end
160
+
161
+ assert_calls_metrics(*expected) do
162
+ m = ActiveRecordFixtures::Order.create :id => 0, :name => 'donkey'
163
+ m = ActiveRecordFixtures::Order.find(m.id)
164
+ m.id = 999
165
+ m.save!
166
+ end
167
+
168
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
169
+
170
+ compare_metrics expected, metrics
171
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
172
+ if NewRelic::Control.instance.rails_version < '3.0.0'
173
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/create", 1)
174
+ else
175
+ check_metric_count("Database/SQL/insert", 1)
176
+ end
177
+ end
178
+
179
+ def test_join_metrics_jruby
180
+ return unless defined?(JRuby)
181
+ return if rails3?
182
+
183
+ expected_metrics = %W[
184
+ ActiveRecord/all
185
+ ActiveRecord/destroy
186
+ ActiveRecord/ActiveRecordFixtures::Order/destroy
187
+ Database/SQL/insert
188
+ Database/SQL/delete
189
+ Database/SQL/show
190
+ ActiveRecord/find
191
+ ActiveRecord/ActiveRecordFixtures::Order/find
192
+ ActiveRecord/ActiveRecordFixtures::Shipment/find
193
+ RemoteService/sql/mysql/localhost
194
+ ]
195
+
196
+ assert_calls_metrics(*expected_metrics) do
197
+ m = ActiveRecordFixtures::Order.create :name => 'jeff'
198
+ m = ActiveRecordFixtures::Order.find(m.id)
199
+ s = m.shipments.create
200
+ m.shipments.to_a
201
+ m.destroy
202
+ end
203
+
204
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
205
+
206
+ compare_metrics expected_metrics, metrics
207
+
208
+ check_metric_time('ActiveRecord/all', NewRelic::Agent.get_stats("ActiveRecord/all").total_exclusive_time, 0)
209
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
210
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Shipment/find", 1)
211
+ check_metric_count("Database/SQL/insert", 3)
212
+ check_metric_count("Database/SQL/delete", 1)
213
+ end
214
+
215
+ def test_join_metrics_sqlite
216
+ return if (defined?(Rails) && Rails.respond_to?(:version) && Rails.version.to_i == 3)
217
+ return if defined?(JRuby)
218
+ return unless isSqlite?
219
+
220
+ expected_metrics = %W[
221
+ ActiveRecord/all
222
+ ActiveRecord/destroy
223
+ ActiveRecord/ActiveRecordFixtures::Order/destroy
224
+ Database/SQL/insert
225
+ Database/SQL/delete
226
+ ActiveRecord/find
227
+ ActiveRecord/ActiveRecordFixtures::Order/find
228
+ ActiveRecord/ActiveRecordFixtures::Shipment/find
229
+ ActiveRecord/create
230
+ ActiveRecord/ActiveRecordFixtures::Shipment/create
231
+ ActiveRecord/ActiveRecordFixtures::Order/create
232
+ ]
233
+
234
+ assert_calls_metrics(*expected_metrics) do
235
+ m = ActiveRecordFixtures::Order.create :name => 'jeff'
236
+ m = ActiveRecordFixtures::Order.find(m.id)
237
+ s = m.shipments.create
238
+ m.shipments.to_a
239
+ m.destroy
240
+ end
241
+
242
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
243
+ compare_metrics expected_metrics, metrics
244
+ if !(defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /Enterprise Edition/)
245
+ check_metric_time('ActiveRecord/all', NewRelic::Agent.get_stats("ActiveRecord/all").total_exclusive_time, 0)
246
+ end
247
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
248
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Shipment/find", 1)
249
+ check_metric_count("Database/SQL/insert", 3)
250
+ check_metric_count("Database/SQL/delete", 1)
251
+ end
252
+
253
+ def test_join_metrics_standard
254
+ return if (defined?(Rails) && Rails.respond_to?(:version) && Rails.version.to_i == 3)
255
+ return if defined?(JRuby) || isSqlite?
256
+
257
+ expected_metrics = %W[
258
+ ActiveRecord/all
259
+ RemoteService/sql/mysql/localhost
260
+ ActiveRecord/destroy
261
+ ActiveRecord/ActiveRecordFixtures::Order/destroy
262
+ Database/SQL/insert
263
+ Database/SQL/delete
264
+ ActiveRecord/find
265
+ ActiveRecord/ActiveRecordFixtures::Order/find
266
+ ActiveRecord/ActiveRecordFixtures::Shipment/find
267
+ Database/SQL/other
268
+ Database/SQL/show
269
+ ActiveRecord/create
270
+ ActiveRecord/ActiveRecordFixtures::Shipment/create
271
+ ActiveRecord/ActiveRecordFixtures::Order/create
272
+ ]
273
+
274
+ assert_calls_metrics(*expected_metrics) do
275
+ m = ActiveRecordFixtures::Order.create :name => 'jeff'
276
+ m = ActiveRecordFixtures::Order.find(m.id)
277
+ s = m.shipments.create
278
+ m.shipments.to_a
279
+ m.destroy
280
+ end
281
+
282
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
283
+
284
+ compare_metrics expected_metrics, metrics
285
+ if !(defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /Enterprise Edition/)
286
+ check_metric_time('ActiveRecord/all', NewRelic::Agent.get_stats("ActiveRecord/all").total_exclusive_time, 0)
287
+ end
288
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Order/find", 1)
289
+ check_metric_count("ActiveRecord/ActiveRecordFixtures::Shipment/find", 1)
290
+ check_metric_count("Database/SQL/insert", 1)
291
+ check_metric_count("Database/SQL/delete", 1)
292
+ end
293
+
294
+ def test_direct_sql
295
+ assert_nil NewRelic::Agent::Instrumentation::MetricFrame.current
296
+ assert_nil NewRelic::Agent.instance.stats_engine.scope_name
297
+ assert_equal 0, NewRelic::Agent.instance.stats_engine.metrics.size, NewRelic::Agent.instance.stats_engine.metrics.inspect
298
+
299
+ expected_metrics = %W[
300
+ ActiveRecord/all
301
+ Database/SQL/select
302
+ RemoteService/sql/mysql/localhost
303
+ ]
304
+
305
+ assert_calls_unscoped_metrics(*expected_metrics) do
306
+ ActiveRecordFixtures::Order.connection.select_rows "select * from #{ActiveRecordFixtures::Order.table_name}"
307
+ end
308
+
309
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
310
+ compare_metrics(expected_metrics, metrics)
311
+
312
+ check_unscoped_metric_count('Database/SQL/select', 1)
313
+
314
+ end
315
+
316
+ def test_other_sql
317
+ expected_metrics = %W[
318
+ ActiveRecord/all
319
+ Database/SQL/other
320
+ RemoteService/sql/mysql/localhost
321
+ ]
322
+ assert_calls_unscoped_metrics(*expected_metrics) do
323
+ ActiveRecordFixtures::Order.connection.execute "begin"
324
+ end
325
+
326
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
327
+
328
+ compare_metrics expected_metrics, metrics
329
+ check_unscoped_metric_count('Database/SQL/other', 1)
330
+ end
331
+
332
+ def test_show_sql
333
+ return if isSqlite?
334
+ return if isPostgres?
335
+
336
+ expected_metrics = %W[ActiveRecord/all Database/SQL/show RemoteService/sql/mysql/localhost]
337
+
338
+ assert_calls_metrics(*expected_metrics) do
339
+ ActiveRecordFixtures::Order.connection.execute "show tables"
340
+ end
341
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
342
+ compare_metrics expected_metrics, metrics
343
+ check_unscoped_metric_count('Database/SQL/show', 1)
344
+ end
345
+
346
+ def test_blocked_instrumentation
347
+ ActiveRecordFixtures::Order.add_delay
348
+ NewRelic::Agent.disable_all_tracing do
349
+ perform_action_with_newrelic_trace :name => 'bogosity' do
350
+ ActiveRecordFixtures::Order.find(:all)
351
+ end
352
+ end
353
+ assert_nil NewRelic::Agent.instance.transaction_sampler.last_sample
354
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
355
+ compare_metrics [], metrics
356
+ end
357
+
358
+ def test_run_explains
359
+ perform_action_with_newrelic_trace :name => 'bogosity' do
360
+ ActiveRecordFixtures::Order.add_delay
361
+ ActiveRecordFixtures::Order.find(:all)
362
+ end
363
+
364
+ # that's a mouthful. perhaps we should ponder our API.
365
+ segment = NewRelic::Agent.instance.transaction_sampler.last_sample \
366
+ .root_segment.called_segments[0].called_segments[0].called_segments[0]
367
+ regex = /^SELECT (["`]?#{ActiveRecordFixtures::Order.table_name}["`]?.)?\* FROM ["`]?#{ActiveRecordFixtures::Order.table_name}["`]?$/
368
+ assert_match regex, segment.params[:sql].strip
369
+ end
370
+
371
+ def test_prepare_to_send
372
+ perform_action_with_newrelic_trace :name => 'bogosity' do
373
+ ActiveRecordFixtures::Order.add_delay
374
+ ActiveRecordFixtures::Order.find(:all)
375
+ end
376
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
377
+ assert_not_nil sample
378
+
379
+ includes_gc = false
380
+ sample.each_segment {|s| includes_gc ||= s.metric_name =~ /GC/ }
381
+
382
+ assert_equal (includes_gc ? 4 : 3), sample.count_segments, sample.to_s
383
+
384
+ sql_segment = sample.root_segment.called_segments.first.called_segments.first.called_segments.first
385
+ assert_not_nil sql_segment, sample.to_s
386
+ assert_match /^SELECT /, sql_segment.params[:sql]
387
+ assert sql_segment.duration > 0.0, "Segment duration must be greater than zero."
388
+ sample = sample.prepare_to_send(:record_sql => :raw, :explain_sql => 0.0)
389
+ sql_segment = sample.root_segment.called_segments.first.called_segments.first.called_segments.first
390
+ assert_match /^SELECT /, sql_segment.params[:sql]
391
+ explanations = sql_segment.params[:explain_plan]
392
+ if isMysql? || isPostgres?
393
+ assert_not_nil explanations, "No explains in segment: #{sql_segment}"
394
+ assert_equal(2, explanations.size,
395
+ "No explains in segment: #{sql_segment}")
396
+ end
397
+ end
398
+
399
+ def test_transaction_mysql
400
+ return unless isMysql? && !defined?(JRuby)
401
+ ActiveRecordFixtures.setup
402
+ sample = NewRelic::Agent.instance.transaction_sampler.reset!
403
+ perform_action_with_newrelic_trace :name => 'bogosity' do
404
+ ActiveRecordFixtures::Order.add_delay
405
+ ActiveRecordFixtures::Order.find(:all)
406
+ end
407
+
408
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
409
+
410
+ sample = sample.prepare_to_send(:record_sql => :obfuscated, :explain_sql => 0.0)
411
+ segment = sample.root_segment.called_segments.first.called_segments.first.called_segments.first
412
+ explanation = segment.params[:explain_plan]
413
+ assert_not_nil explanation, "No explains in segment: #{segment}"
414
+ assert_equal 2, explanation.size,"No explains in segment: #{segment}"
415
+
416
+ assert_equal 10, explanation[0].size
417
+ ['id', 'select_type', 'table'].each do |c|
418
+ assert explanation[0].include?(c)
419
+ end
420
+ ['1', 'SIMPLE', ActiveRecordFixtures::Order.table_name].each do |c|
421
+ assert explanation[1][0].include?(c)
422
+ end
423
+
424
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
425
+ assert_equal 1, s.call_count
426
+ end
427
+
428
+ def test_transaction_postgres
429
+ return unless isPostgres?
430
+ # note that our current test builds do not use postgres, this is
431
+ # here strictly for troubleshooting, not CI builds
432
+ sample = NewRelic::Agent.instance.transaction_sampler.reset!
433
+ perform_action_with_newrelic_trace :name => 'bogosity' do
434
+ ActiveRecordFixtures::Order.add_delay
435
+ ActiveRecordFixtures::Order.find(:all)
436
+ end
437
+
438
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
439
+
440
+ sample = sample.prepare_to_send(:record_sql => :obfuscated, :explain_sql => 0.0)
441
+ segment = sample.root_segment.called_segments.first.called_segments.first.called_segments.first
442
+ explanations = segment.params[:explain_plan]
443
+
444
+ assert_not_nil explanations, "No explains in segment: #{segment}"
445
+ assert_equal 1, explanations.size,"No explains in segment: #{segment}"
446
+ assert_equal 1, explanations.first.size
447
+
448
+ assert_equal("Explain Plan", explanations[0][0])
449
+ assert_match /Seq Scan on test_data/, explanations[0][1].join(";")
450
+
451
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
452
+ assert_equal 1, s.call_count
453
+ end
454
+
455
+ def test_transaction_other
456
+ return if isMysql? || isPostgres?
457
+ sample = NewRelic::Agent.instance.transaction_sampler.reset!
458
+ perform_action_with_newrelic_trace :name => 'bogosity' do
459
+ ActiveRecordFixtures::Order.add_delay
460
+ ActiveRecordFixtures::Order.find(:all)
461
+ end
462
+
463
+ sample = NewRelic::Agent.instance.transaction_sampler.last_sample
464
+
465
+ sample = sample.prepare_to_send(:record_sql => :obfuscated, :explain_sql => 0.0)
466
+ segment = sample.root_segment.called_segments.first.called_segments.first.called_segments.first
467
+
468
+ s = NewRelic::Agent.get_stats("ActiveRecord/ActiveRecordFixtures::Order/find")
469
+ assert_equal 1, s.call_count
470
+ end
471
+
472
+ # These are only valid for rails 2.1 and later
473
+ if NewRelic::Control.instance.rails_version >= NewRelic::VersionNumber.new("2.1.0")
474
+ ActiveRecordFixtures::Order.class_eval do
475
+ if NewRelic::Control.instance.rails_version < NewRelic::VersionNumber.new("3.1")
476
+ named_scope :jeffs, :conditions => { :name => 'Jeff' }
477
+ else
478
+ scope :jeffs, :conditions => { :name => 'Jeff' }
479
+ end
480
+ end
481
+ def test_named_scope
482
+ ActiveRecordFixtures::Order.create :name => 'Jeff'
483
+
484
+ find_metric = "ActiveRecord/ActiveRecordFixtures::Order/find"
485
+
486
+ check_metric_count(find_metric, 0)
487
+ assert_calls_metrics(find_metric) do
488
+ x = ActiveRecordFixtures::Order.jeffs.find(:all)
489
+ end
490
+ check_metric_count(find_metric, 1)
491
+ end
492
+ end
493
+
494
+ # This is to make sure the all metric is recorded for exceptional cases
495
+ def test_error_handling
496
+ # have the AR select throw an error
497
+ ActiveRecordFixtures::Order.connection.stubs(:log_info).with do | sql, x, y |
498
+ raise "Error" if sql =~ /select/
499
+ true
500
+ end
501
+
502
+ expected_metrics = %W[ActiveRecord/all Database/SQL/select RemoteService/sql/mysql/localhost]
503
+
504
+ assert_calls_metrics(*expected_metrics) do
505
+ begin
506
+ ActiveRecordFixtures::Order.connection.select_rows "select * from #{ActiveRecordFixtures::Order.table_name}"
507
+ rescue RuntimeError => e
508
+ # catch only the error we raise above
509
+ raise unless e.message == 'Error'
510
+ end
511
+ end
512
+ metrics = NewRelic::Agent.instance.stats_engine.metrics
513
+ compare_metrics expected_metrics, metrics
514
+ check_metric_count('Database/SQL/select', 1)
515
+ check_metric_count('ActiveRecord/all', 1)
516
+ check_metric_count('RemoteService/sql/mysql/localhost', 1)
517
+ end
518
+
519
+ def test_rescue_handling
520
+ # Not sure why we get a transaction error with sqlite
521
+ return if isSqlite?
522
+
523
+ begin
524
+ ActiveRecordFixtures::Order.transaction do
525
+ raise ActiveRecord::ActiveRecordError.new('preserve-me!')
526
+ end
527
+ rescue ActiveRecord::ActiveRecordError => e
528
+ assert_equal 'preserve-me!', e.message
529
+ end
530
+ end
531
+
532
+ def test_remote_service_metric_respects_dynamic_connection_config
533
+ return unless isMysql?
534
+
535
+ # puts NewRelic::Agent::Database.config.inspect
536
+
537
+ ActiveRecordFixtures::Shipment.connection.execute('SHOW TABLES');
538
+ assert(NewRelic::Agent.get_stats('RemoteService/sql/mysql/localhost').call_count != 0)
539
+
540
+ config = ActiveRecordFixtures::Shipment.connection.instance_eval { @config }
541
+ config[:host] = '127.0.0.1'
542
+ connection = ActiveRecordFixtures::Shipment.establish_connection(config)
543
+
544
+ # puts ActiveRecord::Base.connection.instance_eval { @config }.inspect
545
+ # puts NewRelic::Agent::Database.config.inspect
546
+
547
+ ActiveRecordFixtures::Shipment.connection.execute('SHOW TABLES');
548
+ assert(NewRelic::Agent.get_stats('RemoteService/sql/mysql/127.0.0.1').call_count != 0)
549
+
550
+ config[:host] = 'localhost'
551
+ ActiveRecordFixtures::Shipment.establish_connection(config)
552
+
553
+ # raise NewRelic::Agent.instance.stats_engine.inspect
554
+ end
555
+
556
+ private
557
+
558
+ def rails3?
559
+ (defined?(Rails) && Rails.respond_to?(:version) && Rails.version.to_i == 3)
560
+ end
561
+
562
+ def rails_env
563
+ rails3? ? ::Rails.env : RAILS_ENV
564
+ end
565
+
566
+ def isPostgres?
567
+ ActiveRecordFixtures::Order.configurations[rails_env]['adapter'] =~ /postgres/i
568
+ end
569
+ def isMysql?
570
+ ActiveRecordFixtures::Order.connection.class.name =~ /mysql/i
571
+ end
572
+
573
+ def isSqlite?
574
+ ActiveRecord::Base.configurations[rails_env]['adapter'] =~ /sqlite/i
575
+ end
576
+ end