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,149 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..',
2
+ 'test_helper'))
3
+ require 'new_relic/agent/database'
4
+ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
5
+ def teardown
6
+ NewRelic::Agent::Database::Obfuscator.instance.reset
7
+ end
8
+
9
+ def test_process_resultset
10
+ resultset = [["column"]]
11
+ assert_equal([nil, [["column"]]],
12
+ NewRelic::Agent::Database.process_resultset(resultset))
13
+ end
14
+
15
+ def test_explain_sql_select_with_mysql_connection
16
+ config = {:adapter => 'mysql'}
17
+ config.default('val')
18
+ sql = 'SELECT foo'
19
+ connection = mock('connection')
20
+ plan = {
21
+ "select_type"=>"SIMPLE", "key_len"=>nil, "table"=>"blogs", "id"=>"1",
22
+ "possible_keys"=>nil, "type"=>"ALL", "Extra"=>"", "rows"=>"2",
23
+ "ref"=>nil, "key"=>nil
24
+ }
25
+ result = mock('explain plan')
26
+ result.expects(:each_hash).yields(plan)
27
+ # two rows, two columns
28
+ connection.expects(:execute).with('EXPLAIN SELECT foo').returns(result)
29
+ NewRelic::Agent::Database.expects(:get_connection).with(config).returns(connection)
30
+
31
+ result = NewRelic::Agent::Database.explain_sql(sql, config)
32
+ assert_equal(plan.keys.sort, result[0].sort)
33
+ assert_equal(plan.values.compact.sort, result[1][0].compact.sort)
34
+ end
35
+
36
+ def test_explain_sql_one_select_with_pg_connection
37
+ config = {:adapter => 'postgresql'}
38
+ config.default('val')
39
+ sql = 'select count(id) from blogs limit 1'
40
+ connection = mock('connection')
41
+ plan = [{"QUERY PLAN"=>"Limit (cost=11.75..11.76 rows=1 width=4)"},
42
+ {"QUERY PLAN"=>" -> Aggregate (cost=11.75..11.76 rows=1 width=4)"},
43
+ {"QUERY PLAN"=>" -> Seq Scan on blogs (cost=0.00..11.40 rows=140 width=4)"}]
44
+ connection.expects(:execute).returns(plan)
45
+ NewRelic::Agent::Database.expects(:get_connection).with(config).returns(connection)
46
+ assert_equal([['QUERY PLAN'],
47
+ [["Limit (cost=11.75..11.76 rows=1 width=4)"],
48
+ [" -> Aggregate (cost=11.75..11.76 rows=1 width=4)"],
49
+ [" -> Seq Scan on blogs (cost=0.00..11.40 rows=140 width=4)"]]],
50
+ NewRelic::Agent::Database.explain_sql(sql, config))
51
+ end
52
+
53
+ def test_explain_sql_no_sql
54
+ assert_equal(nil, NewRelic::Agent::Database.explain_sql(nil, nil))
55
+ end
56
+
57
+ def test_explain_sql_no_connection_config
58
+ assert_equal(nil, NewRelic::Agent::Database.explain_sql('select foo', nil))
59
+ end
60
+
61
+ def test_explain_sql_non_select
62
+ assert_equal([], NewRelic::Agent::Database.explain_sql('foo',
63
+ mock('config')))
64
+ end
65
+
66
+ def test_explain_sql_one_select_no_connection
67
+ # NB this test raises an error in the log, much as it might if a
68
+ # user supplied a config that was not valid. This is generally
69
+ # expected behavior - the get_connection method shouldn't allow
70
+ # errors to percolate up.
71
+ config = mock('config')
72
+ config.stubs(:[]).returns(nil)
73
+ assert_equal([], NewRelic::Agent::Database.explain_sql('SELECT', config))
74
+ end
75
+
76
+ def test_handle_exception_in_explain
77
+ fake_error = StandardError.new('a message')
78
+ NewRelic::Control.instance.log.expects(:error).with('Error getting query plan: a message')
79
+ # backtrace can be basically any string, just should get logged
80
+ NewRelic::Control.instance.log.expects(:debug).with(instance_of(String))
81
+
82
+ NewRelic::Agent::Database.handle_exception_in_explain do
83
+ raise(fake_error)
84
+ end
85
+ end
86
+
87
+ def test_obfuscation_mysql_basic
88
+ insert = %q[INSERT INTO `X` values("test",0, 1 , 2, 'test')]
89
+ assert_equal("INSERT INTO `X` values(?,?, ? , ?, ?)",
90
+ NewRelic::Agent::Database.obfuscate_sql(insert))
91
+ select = %q[SELECT `table`.`column` FROM `table` WHERE `table`.`column` = 'value' AND `table`.`other_column` = "other value" LIMIT 1]
92
+ assert_equal(%q[SELECT `table`.`column` FROM `table` WHERE `table`.`column` = ? AND `table`.`other_column` = ? LIMIT ?],
93
+ NewRelic::Agent::Database.obfuscate_sql(select))
94
+ end
95
+
96
+ def test_obfuscation_postgresql_basic
97
+ insert = NewRelic::Agent::Database::Statement.new(%q[INSERT INTO "X" values('test',0, 1 , 2, 'test')])
98
+ insert.adapter = :postgresql
99
+ assert_equal('INSERT INTO "X" values(?,?, ? , ?, ?)',
100
+ NewRelic::Agent::Database.obfuscate_sql(insert))
101
+ select = NewRelic::Agent::Database::Statement.new(%q[SELECT "table"."column" FROM "table" WHERE "table"."column" = 'value' AND "table"."other_column" = 'other value' LIMIT 1])
102
+ select.adapter = :postgresql
103
+ assert_equal(%q[SELECT "table"."column" FROM "table" WHERE "table"."column" = ? AND "table"."other_column" = ? LIMIT ?],
104
+ NewRelic::Agent::Database.obfuscate_sql(select))
105
+ end
106
+
107
+ def test_obfuscation_escaped_literals
108
+ insert = %q[INSERT INTO X values('', 'jim''s ssn',0, 1 , 'jim''s son''s son', """jim''s"" hat", "\"jim''s secret\"")]
109
+ assert_equal("INSERT INTO X values(?, ?,?, ? , ?, ?, ?)",
110
+ NewRelic::Agent::Database.obfuscate_sql(insert))
111
+ end
112
+
113
+ def test_obfuscation_mysql_integers_in_identifiers
114
+ select = %q[SELECT * FROM `table_007` LIMIT 1]
115
+ assert_equal(%q[SELECT * FROM `table_007` LIMIT ?],
116
+ NewRelic::Agent::Database.obfuscate_sql(select))
117
+ end
118
+
119
+ def test_obfuscation_postgresql_integers_in_identifiers
120
+ select = NewRelic::Agent::Database::Statement.new(%q[SELECT * FROM "table_007" LIMIT 1])
121
+ select.adapter = :postgresql
122
+ assert_equal(%q[SELECT * FROM "table_007" LIMIT ?],
123
+ NewRelic::Agent::Database.obfuscate_sql(select))
124
+ end
125
+
126
+ def test_sql_obfuscation_filters
127
+ NewRelic::Agent::Database.set_sql_obfuscator(:replace) do |string|
128
+ "1" + string
129
+ end
130
+
131
+ sql = "SELECT * FROM TABLE 123 'jim'"
132
+
133
+ assert_equal "1" + sql, NewRelic::Agent::Database.obfuscate_sql(sql)
134
+
135
+ NewRelic::Agent::Database.set_sql_obfuscator(:before) do |string|
136
+ "2" + string
137
+ end
138
+
139
+ assert_equal "12" + sql, NewRelic::Agent::Database.obfuscate_sql(sql)
140
+
141
+ NewRelic::Agent::Database.set_sql_obfuscator(:after) do |string|
142
+ string + "3"
143
+ end
144
+
145
+ assert_equal "12" + sql + "3", NewRelic::Agent::Database.obfuscate_sql(sql)
146
+
147
+ NewRelic::Agent::Database::Obfuscator.instance.reset
148
+ end
149
+ end
@@ -0,0 +1,257 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
+ class NewRelic::Agent::ErrorCollector::NoticeErrorTest < Test::Unit::TestCase
3
+ require 'new_relic/agent/error_collector'
4
+ include NewRelic::Agent::ErrorCollector::NoticeError
5
+
6
+ def test_error_params_from_options_mocked
7
+ options = {:initial => 'options'}
8
+ self.expects(:uri_ref_and_root).returns({:hi => 'there', :hello => 'bad'})
9
+ self.expects(:normalized_request_and_custom_params).with({:initial => 'options'}).returns({:hello => 'world'})
10
+ assert_equal({:hi => 'there', :hello => 'world'}, error_params_from_options(options))
11
+ end
12
+
13
+ module Winner
14
+ def winner
15
+ 'yay'
16
+ end
17
+ end
18
+
19
+ def test_sense_method
20
+ object = Object.new
21
+ object.extend(Winner)
22
+ assert !sense_method(object, 'blab')
23
+ assert_equal 'yay', sense_method(object, 'winner')
24
+ end
25
+
26
+ def test_fetch_from_options
27
+ options = {:hello => 'world'}
28
+ assert_equal 'world', fetch_from_options(options, :hello, '')
29
+ assert_equal '', fetch_from_options(options, :none, '')
30
+ assert_equal({}, options)
31
+ end
32
+
33
+ def test_uri_ref_and_root_default
34
+ fake_control = mocked_control
35
+ fake_control.expects(:root).returns('rootbeer')
36
+ options = {}
37
+ assert_equal({:request_referer => '', :rails_root => 'rootbeer', :request_uri => ''}, uri_ref_and_root(options))
38
+ end
39
+
40
+ def test_uri_ref_and_root_values
41
+ fake_control = mocked_control
42
+ fake_control.expects(:root).returns('rootbeer')
43
+ options = {:uri => 'whee', :referer => 'bang'}
44
+ assert_equal({:request_referer => 'bang', :rails_root => 'rootbeer', :request_uri => 'whee'}, uri_ref_and_root(options))
45
+ end
46
+
47
+ def test_custom_params_from_opts_base
48
+ assert_equal({}, custom_params_from_opts({}))
49
+ end
50
+
51
+ def test_custom_params_from_opts_custom_params
52
+ assert_equal({:foo => 'bar'}, custom_params_from_opts({:custom_params => {:foo => 'bar'}}))
53
+ end
54
+
55
+ def test_custom_params_from_opts_merged_params
56
+ assert_equal({:foo => 'baz'}, custom_params_from_opts({:custom_params => {:foo => 'bar'}, :foo => 'baz'}))
57
+ end
58
+
59
+ def test_request_params_from_opts_positive
60
+ fake_control = mock('control')
61
+ self.expects(:control).returns(fake_control)
62
+ fake_control.expects(:capture_params).returns(true)
63
+ val = {:request_params => 'foo'}
64
+ assert_equal('foo', request_params_from_opts(val))
65
+ assert_equal({}, val, "should delete request_params key from hash")
66
+ end
67
+
68
+ def test_request_params_from_opts_negative
69
+ fake_control = mock('control')
70
+ self.expects(:control).returns(fake_control)
71
+ fake_control.expects(:capture_params).returns(false)
72
+ val = {:request_params => 'foo'}
73
+ assert_equal(nil, request_params_from_opts(val))
74
+ assert_equal({}, val, "should delete request_params key from hash")
75
+ end
76
+
77
+ def test_normalized_request_and_custom_params_base
78
+ self.expects(:normalize_params).with(nil).returns(nil)
79
+ self.expects(:normalize_params).with({}).returns({})
80
+ fake_control = mock('control')
81
+ self.expects(:control).returns(fake_control)
82
+ fake_control.expects(:capture_params).returns(true)
83
+ assert_equal({:request_params => nil, :custom_params => {}}, normalized_request_and_custom_params({}))
84
+ end
85
+
86
+ def test_extract_source_base
87
+ @capture_source = true
88
+ self.expects(:sense_method).with(nil, 'source_extract')
89
+ assert_equal(nil, extract_source(nil))
90
+ end
91
+
92
+ def test_extract_source_disabled
93
+ @capture_source = false
94
+ assert_equal(nil, extract_source(mock('exception')))
95
+ end
96
+
97
+ def test_extract_source_with_source
98
+ self.expects(:sense_method).with('happy', 'source_extract').returns('THE SOURCE')
99
+ @capture_source = true
100
+ assert_equal('THE SOURCE', extract_source('happy'))
101
+ end
102
+
103
+ def test_extract_stack_trace
104
+ exception = mock('exception')
105
+ self.expects(:sense_method).with(exception, 'original_exception')
106
+ self.expects(:sense_method).with(exception, 'backtrace')
107
+ assert_equal('<no stack trace>', extract_stack_trace(exception))
108
+ end
109
+
110
+ def test_extract_stack_trace_positive
111
+ orig = mock('original')
112
+ exception = mock('exception')
113
+ self.expects(:sense_method).with(exception, 'original_exception').returns(orig)
114
+ self.expects(:sense_method).with(orig, 'backtrace').returns('STACK STACK STACK')
115
+ assert_equal('STACK STACK STACK', extract_stack_trace(exception))
116
+ end
117
+
118
+ def test_over_queue_limit_negative
119
+ @errors = []
120
+ assert !over_queue_limit?(nil)
121
+ end
122
+
123
+ def test_over_queue_limit_positive
124
+ @errors = %w(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
125
+ fake_log = mock('log')
126
+ self.expects(:log).returns(fake_log)
127
+ fake_log.expects(:warn).with('The error reporting queue has reached 20. The error detail for this and subsequent errors will not be transmitted to New Relic until the queued errors have been sent: hooray')
128
+ assert over_queue_limit?('hooray')
129
+ end
130
+
131
+ def test_exception_info
132
+ exception = mock('exception')
133
+ self.expects(:sense_method).with(exception, 'file_name').returns('file_name')
134
+ self.expects(:sense_method).with(exception, 'line_number').returns('line_number')
135
+ self.expects(:extract_source).with(exception).returns('source')
136
+ self.expects(:extract_stack_trace).with(exception).returns('stack_trace')
137
+ assert_equal({:file_name => 'file_name', :line_number => 'line_number', :source => 'source', :stack_trace => 'stack_trace'},
138
+ exception_info(exception))
139
+ end
140
+
141
+ def test_add_to_error_queue_positive
142
+ noticed_error = mock('noticed_error')
143
+ noticed_error.expects(:message).returns('a message')
144
+ @lock = Mutex.new
145
+ @errors = []
146
+ self.expects(:over_queue_limit?).with('a message').returns(false)
147
+ add_to_error_queue(noticed_error)
148
+ assert_equal([noticed_error], @errors)
149
+ end
150
+
151
+ def test_add_to_error_queue_negative
152
+ noticed_error = mock('noticed_error')
153
+ noticed_error.expects(:message).returns('a message')
154
+ @lock = Mutex.new
155
+ @errors = []
156
+ self.expects(:over_queue_limit?).with('a message').returns(true)
157
+ add_to_error_queue(noticed_error)
158
+ assert_equal([], @errors)
159
+ end
160
+
161
+ def test_should_exit_notice_error_disabled
162
+ error = mocked_error
163
+ @enabled = false
164
+ assert should_exit_notice_error?(error)
165
+ end
166
+
167
+ def test_should_exit_notice_error_nil
168
+ error = nil
169
+ @enabled = true
170
+ self.expects(:error_is_ignored?).with(error).returns(false)
171
+ # we increment it for the case that someone calls
172
+ # NewRelic::Agent.notice_error(foo) # foo is nil
173
+ # (which is probably not a good idea but is the existing api)
174
+ self.expects(:increment_error_count!)
175
+ assert should_exit_notice_error?(error)
176
+ end
177
+
178
+ def test_should_exit_notice_error_positive
179
+ error = mocked_error
180
+ @enabled = true
181
+ self.expects(:error_is_ignored?).with(error).returns(true)
182
+ assert should_exit_notice_error?(error)
183
+ end
184
+
185
+ def test_should_exit_notice_error_negative
186
+ error = mocked_error
187
+ @enabled = true
188
+ self.expects(:error_is_ignored?).with(error).returns(false)
189
+ self.expects(:increment_error_count!)
190
+ assert !should_exit_notice_error?(error)
191
+ end
192
+
193
+ def test_filtered_error_positive
194
+ @ignore = {'an_error' => true}
195
+ error = mocked_error
196
+ error_class = mock('error class')
197
+ error.expects(:class).returns(error_class)
198
+ error_class.expects(:name).returns('an_error')
199
+ assert filtered_error?(error)
200
+ end
201
+
202
+ def test_filtered_error_negative
203
+ @ignore = {}
204
+ error = mocked_error
205
+ error_class = mock('error class')
206
+ error.expects(:class).returns(error_class)
207
+ error_class.expects(:name).returns('an_error')
208
+ self.expects(:filtered_by_error_filter?).with(error).returns(false)
209
+ assert !filtered_error?(error)
210
+ end
211
+
212
+ def test_filtered_by_error_filter_empty
213
+ # should return right away when there's no filter
214
+ @ignore_filter = nil
215
+ assert !filtered_by_error_filter?(nil)
216
+ end
217
+
218
+ def test_filtered_by_error_filter_positive
219
+ error = mocked_error
220
+ @ignore_filter = lambda { |x| assert_equal error, x; false }
221
+ assert filtered_by_error_filter?(error)
222
+ end
223
+
224
+ def test_filtered_by_error_filter_negative
225
+ error = mocked_error
226
+ @ignore_filter = lambda { |x| assert_equal error, x; true }
227
+ assert !filtered_by_error_filter?(error)
228
+ end
229
+
230
+ def test_error_is_ignored_positive
231
+ error = mocked_error
232
+ self.expects(:filtered_error?).with(error).returns(true)
233
+ assert error_is_ignored?(error)
234
+ end
235
+
236
+ def test_error_is_ignored_negative
237
+ error = mocked_error
238
+ self.expects(:filtered_error?).with(error).returns(false)
239
+ assert !error_is_ignored?(error)
240
+ end
241
+
242
+ def test_error_is_ignored_no_error
243
+ assert !error_is_ignored?(nil), 'should not ignore nil'
244
+ end
245
+
246
+ private
247
+
248
+ def mocked_error
249
+ mock('error')
250
+ end
251
+
252
+ def mocked_control
253
+ fake_control = mock('control')
254
+ self.stubs(:control).returns(fake_control)
255
+ fake_control
256
+ end
257
+ end
@@ -0,0 +1,192 @@
1
+ # Run faster standalone
2
+ ENV['SKIP_RAILS'] = 'true'
3
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
4
+
5
+ class NewRelic::Agent::ErrorCollectorTest < Test::Unit::TestCase
6
+ def setup
7
+ super
8
+ @error_collector = NewRelic::Agent::ErrorCollector.new
9
+ @error_collector.stubs(:enabled).returns(true)
10
+ end
11
+
12
+ def test_empty
13
+ @error_collector.harvest_errors([])
14
+ @error_collector.notice_error(nil, :metric=> 'path', :request_params => {:x => 'y'})
15
+ errors = @error_collector.harvest_errors([])
16
+
17
+ assert_equal 0, errors.length
18
+
19
+ @error_collector.notice_error('Some error message', :metric=> 'path', :request_params => {:x => 'y'})
20
+ errors = @error_collector.harvest_errors([])
21
+
22
+ err = errors.first
23
+ assert_equal 'Some error message', err.message
24
+ assert_equal 'y', err.params[:request_params][:x]
25
+ assert_equal '', err.params[:request_uri]
26
+ assert_equal '', err.params[:request_referer]
27
+ assert_equal 'path', err.path
28
+ assert_equal 'Error', err.exception_class
29
+ end
30
+
31
+ def test_simple
32
+ @error_collector.notice_error(StandardError.new("message"), :uri => '/myurl/', :metric => 'path', :referer => 'test_referer', :request_params => {:x => 'y'})
33
+
34
+ old_errors = []
35
+ errors = @error_collector.harvest_errors(old_errors)
36
+
37
+ assert_equal errors.length, 1
38
+
39
+ err = errors.first
40
+ assert_equal 'message', err.message
41
+ assert_equal 'y', err.params[:request_params][:x]
42
+ assert err.params[:request_uri] == '/myurl/'
43
+ assert err.params[:request_referer] == "test_referer"
44
+ assert err.path == 'path'
45
+ assert err.exception_class == 'StandardError'
46
+
47
+ # the collector should now return an empty array since nothing
48
+ # has been added since its last harvest
49
+ errors = @error_collector.harvest_errors(nil)
50
+ assert errors.length == 0
51
+ end
52
+
53
+ def test_long_message
54
+ #yes, times 500. it's a 5000 byte string. Assuming strings are
55
+ #still 1 byte / char.
56
+ @error_collector.notice_error(StandardError.new("1234567890" * 500), :uri => '/myurl/', :metric => 'path', :request_params => {:x => 'y'})
57
+
58
+ old_errors = []
59
+ errors = @error_collector.harvest_errors(old_errors)
60
+
61
+ assert_equal errors.length, 1
62
+
63
+ err = errors.first
64
+ assert_equal 4096, err.message.length
65
+ assert_equal ('1234567890' * 500)[0..4095], err.message
66
+ end
67
+
68
+ def test_collect_failover
69
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'first', :request_params => {:x => 'y'})
70
+
71
+ errors = @error_collector.harvest_errors([])
72
+
73
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'second', :request_params => {:x => 'y'})
74
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
75
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'last', :request_params => {:x => 'y'})
76
+
77
+ errors = @error_collector.harvest_errors(errors)
78
+
79
+ assert_equal 4, errors.length
80
+ assert_equal 'first', errors.first.path
81
+ assert_equal 'last', errors.last.path
82
+
83
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'first', :request_params => {:x => 'y'})
84
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'last', :request_params => {:x => 'y'})
85
+
86
+ errors = @error_collector.harvest_errors(nil)
87
+ assert_equal 2, errors.length
88
+ assert_equal 'first', errors.first.path
89
+ assert_equal 'last', errors.last.path
90
+ end
91
+
92
+ def test_queue_overflow
93
+
94
+ max_q_length = 20 # for some reason I can't read the constant in ErrorCollector
95
+
96
+ silence_stream(::STDOUT) do
97
+ (max_q_length + 5).times do |n|
98
+ @error_collector.notice_error(StandardError.new("exception #{n}"), :metric => "path", :request_params => {:x => n})
99
+ end
100
+ end
101
+
102
+ errors = @error_collector.harvest_errors([])
103
+ assert errors.length == max_q_length
104
+ errors.each_index do |i|
105
+ err = errors.shift
106
+ assert_equal i.to_s, err.params[:request_params][:x], err.params.inspect
107
+ end
108
+ end
109
+
110
+ # Why would anyone undef these methods?
111
+ class TestClass
112
+ undef to_s
113
+ undef inspect
114
+ end
115
+
116
+
117
+ def test_supported_param_types
118
+
119
+ types = [[1, '1'],
120
+ [1.1, '1.1'],
121
+ ['hi', 'hi'],
122
+ [:hi, :hi],
123
+ [StandardError.new("test"), "#<StandardError>"],
124
+ [TestClass.new, "#<NewRelic::Agent::ErrorCollectorTest::TestClass>"]
125
+ ]
126
+
127
+
128
+ types.each do |test|
129
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'path', :request_params => {:x => test[0]})
130
+
131
+ assert_equal test[1], @error_collector.harvest_errors([])[0].params[:request_params][:x]
132
+ end
133
+ end
134
+
135
+
136
+ def test_exclude
137
+ @error_collector.ignore(["IOError"])
138
+
139
+ @error_collector.notice_error(IOError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
140
+
141
+ errors = @error_collector.harvest_errors([])
142
+
143
+ assert_equal 0, errors.length
144
+ end
145
+
146
+ def test_exclude_block
147
+ NewRelic::Agent.logger.expects(:error).never
148
+ @error_collector.ignore_error_filter &wrapped_filter_proc
149
+
150
+ @error_collector.notice_error(IOError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
151
+ @error_collector.notice_error(StandardError.new("message"), :metric => 'path', :request_params => {:x => 'y'})
152
+
153
+ errors = @error_collector.harvest_errors([])
154
+
155
+ assert_equal 1, errors.length
156
+ end
157
+
158
+ def test_obfuscates_error_messages_when_high_security_is_set
159
+ NewRelic::Control.instance['high_security'] = true
160
+
161
+ @error_collector.notice_error(StandardError.new("YO SQL BAD: serect * flom test where foo = 'bar'"))
162
+ @error_collector.notice_error(StandardError.new("YO SQL BAD: serect * flom test where foo in (1,2,3,4,5)"))
163
+
164
+ old_errors = []
165
+ errors = @error_collector.harvest_errors([])
166
+
167
+ assert_equal('YO SQL BAD: serect * flom test where foo = ?',
168
+ errors[0].message)
169
+ assert_equal('YO SQL BAD: serect * flom test where foo in (?,?,?,?,?)',
170
+ errors[1].message)
171
+
172
+ NewRelic::Control.instance['high_security'] = nil
173
+ end
174
+
175
+ private
176
+
177
+ def wrapped_filter_proc
178
+ Proc.new do |e|
179
+ if e.is_a? IOError
180
+ return nil
181
+ else
182
+ return e
183
+ end
184
+ end
185
+ end
186
+
187
+ def silence_stream(*args)
188
+ super
189
+ rescue NoMethodError
190
+ yield
191
+ end
192
+ end