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,153 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
+ class NewRelic::Agent::Agent::StartWorkerThreadTest < Test::Unit::TestCase
3
+ require 'new_relic/agent/agent'
4
+ include NewRelic::Agent::Agent::StartWorkerThread
5
+
6
+ def test_deferred_work_connects
7
+ self.expects(:catch_errors).yields
8
+ self.expects(:connect).with('connection_options')
9
+ @connected = true
10
+ self.expects(:check_transaction_sampler_status)
11
+ self.expects(:check_sql_sampler_status)
12
+ self.expects(:log_worker_loop_start)
13
+ self.expects(:create_and_run_worker_loop)
14
+ deferred_work!('connection_options')
15
+ end
16
+
17
+ def test_deferred_work_connect_failed
18
+ self.expects(:catch_errors).yields
19
+ self.expects(:connect).with('connection_options')
20
+ @connected = false
21
+ fake_log = mocked_log
22
+ fake_log.expects(:debug).with("No connection. Worker thread ending.")
23
+ deferred_work!('connection_options')
24
+ end
25
+
26
+ def test_check_transaction_sampler_status_enabled
27
+ control = mocked_control
28
+ control.expects(:developer_mode?).returns(false)
29
+ @should_send_samples = true
30
+ @transaction_sampler = mock('transaction_sampler')
31
+ @transaction_sampler.expects(:enable)
32
+ check_transaction_sampler_status
33
+ end
34
+
35
+ def test_check_transaction_sampler_status_devmode
36
+ control = mocked_control
37
+ control.expects(:developer_mode?).returns(true)
38
+ @should_send_samples = false
39
+ @transaction_sampler = mock('transaction_sampler')
40
+ @transaction_sampler.expects(:enable)
41
+ check_transaction_sampler_status
42
+ end
43
+
44
+ def test_check_transaction_sampler_status_disabled
45
+ control = mocked_control
46
+ control.expects(:developer_mode?).returns(false)
47
+ @should_send_samples = false
48
+ @transaction_sampler = mock('transaction_sampler')
49
+ @transaction_sampler.expects(:disable)
50
+ check_transaction_sampler_status
51
+ end
52
+
53
+ def test_log_worker_loop_start
54
+ @report_period = 30
55
+ log = mocked_log
56
+ log.expects(:info).with("Reporting performance data every 30 seconds.")
57
+ log.expects(:debug).with("Running worker loop")
58
+ log_worker_loop_start
59
+ end
60
+
61
+ def test_create_and_run_worker_loop
62
+ @report_period = 30
63
+ @should_send_samples = true
64
+ wl = mock('worker loop')
65
+ NewRelic::Agent::WorkerLoop.expects(:new).returns(wl)
66
+ wl.expects(:run).with(30).yields
67
+ self.expects(:save_or_transmit_data)
68
+ create_and_run_worker_loop
69
+ end
70
+
71
+ def test_handle_force_restart
72
+ # hooray for methods with no branches
73
+ error = mock('exception')
74
+ log = mocked_log
75
+ error.expects(:message).returns('a message')
76
+ log.expects(:info).with('a message')
77
+ self.expects(:reset_stats)
78
+ self.expects(:sleep).with(30)
79
+
80
+ @metric_ids = 'this is not an empty hash'
81
+ @connected = true
82
+
83
+ handle_force_restart(error)
84
+
85
+ assert_equal({}, @metric_ids)
86
+ assert @connected.nil?
87
+ end
88
+
89
+ def test_handle_force_disconnect
90
+ error = mock('exception')
91
+ error.expects(:message).returns('a message')
92
+ log = mocked_log
93
+ log.expects(:error).with("New Relic forced this agent to disconnect (a message)")
94
+ self.expects(:disconnect)
95
+ handle_force_disconnect(error)
96
+ end
97
+
98
+ def test_handle_server_connection_problem
99
+ error_class = mock('class of exception')
100
+ error = mock('exception')
101
+ log = mocked_log
102
+ log.expects(:error).with('Unable to establish connection with the server. Run with log level set to debug for more information.')
103
+ error.expects(:class).returns(error_class)
104
+ error_class.expects(:name).returns('an error class')
105
+ error.expects(:message).returns('a message')
106
+ error.expects(:backtrace).returns(['first line', 'second line'])
107
+ log.expects(:debug).with("an error class: a message\nfirst line")
108
+ self.expects(:disconnect)
109
+ handle_server_connection_problem(error)
110
+ end
111
+
112
+ def test_handle_other_error
113
+ error_class = mock('class of exception')
114
+ error = mock('exception')
115
+ log = mocked_log
116
+ error.expects(:class).returns(error_class)
117
+ error_class.expects(:name).returns('an error class')
118
+ error.expects(:message).returns('a message')
119
+ error.expects(:backtrace).returns(['first line', 'second line'])
120
+ log.expects(:error).with("Terminating worker loop: an error class: a message\n first line\n second line")
121
+ self.expects(:disconnect)
122
+ handle_other_error(error)
123
+ end
124
+
125
+ def test_catch_errors_force_restart
126
+ @runs = 0
127
+ error = NewRelic::Agent::ForceRestartException.new
128
+ # twice, because we expect it to retry the block
129
+ self.expects(:handle_force_restart).with(error).twice
130
+ catch_errors do
131
+ # needed to keep it from looping infinitely in the test
132
+ @runs += 1
133
+ raise error unless @runs > 2
134
+ end
135
+ assert_equal 3, @runs, 'should retry the block when it fails'
136
+ end
137
+
138
+ private
139
+
140
+ def mocked_log
141
+ fake_log = mock('log')
142
+ self.stubs(:log).returns(fake_log)
143
+ fake_log
144
+ end
145
+
146
+
147
+ def mocked_control
148
+ fake_control = mock('control')
149
+ self.stubs(:control).returns(fake_control)
150
+ fake_control
151
+ end
152
+ end
153
+
@@ -0,0 +1,140 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ module NewRelic
3
+ module Agent
4
+ class AgentTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+ @agent = NewRelic::Agent::Agent.new
9
+ end
10
+
11
+ def test_save_or_transmit_data_should_save
12
+ NewRelic::Agent.expects(:save_data).once
13
+ @agent.expects(:harvest_and_send_timeslice_data).never
14
+ NewRelic::DataSerialization.expects(:should_send_data?).returns(false)
15
+ @agent.instance_eval { save_or_transmit_data }
16
+ end
17
+
18
+ def test_save_or_transmit_data_should_transmit
19
+ NewRelic::Control.instance.stubs(:disable_serialization?).returns(false)
20
+ NewRelic::Agent.expects(:load_data)
21
+ @agent.expects(:harvest_and_send_timeslice_data)
22
+ @agent.expects(:harvest_and_send_slowest_sample)
23
+ @agent.expects(:harvest_and_send_errors)
24
+ NewRelic::DataSerialization.expects(:should_send_data?).returns(true)
25
+ @agent.instance_eval { save_or_transmit_data }
26
+ end
27
+
28
+ def test_serialize
29
+ assert_equal([{}, [], []], @agent.send(:serialize), "should return nil when shut down")
30
+ end
31
+
32
+ def test_harvest_transaction_traces
33
+ assert_equal([], @agent.send(:harvest_transaction_traces), 'should return transaction traces')
34
+ end
35
+
36
+ def test_harvest_timeslice_data
37
+ assert_equal({}, @agent.send(:harvest_timeslice_data),
38
+ 'should return timeslice data')
39
+ end
40
+
41
+ def test_harvest_timelice_data_should_be_thread_safe
42
+ 2000.times do |i|
43
+ @agent.stats_engine.stats_hash[i.to_s] = NewRelic::StatsBase.new
44
+ end
45
+
46
+ harvest = Thread.new do
47
+ @agent.send(:harvest_timeslice_data)
48
+ end
49
+
50
+ app = Thread.new do
51
+ @agent.stats_engine.stats_hash["a"] = NewRelic::StatsBase.new
52
+ end
53
+
54
+ assert_nothing_raised do
55
+ [app, harvest].each{|t| t.join}
56
+ end
57
+ end
58
+
59
+ def test_harvest_errors
60
+ assert_equal([], @agent.send(:harvest_errors), 'should return errors')
61
+ end
62
+
63
+ def test_merge_data_from_empty
64
+ unsent_timeslice_data = mock('unsent timeslice data')
65
+ unsent_errors = mock('unsent errors')
66
+ unsent_traces = mock('unsent traces')
67
+ @agent.instance_eval {
68
+ @unsent_errors = unsent_errors
69
+ @unsent_timeslice_data = unsent_timeslice_data
70
+ @traces = unsent_traces
71
+ }
72
+ # nb none of the others should receive merge requests
73
+ @agent.merge_data_from([{}])
74
+ end
75
+
76
+ def test_unsent_errors_size_empty
77
+ @agent.instance_eval {
78
+ @unsent_errors = nil
79
+ }
80
+ assert_equal(nil, @agent.unsent_errors_size)
81
+ end
82
+
83
+ def test_unsent_errors_size_with_errors
84
+ @agent.instance_eval {
85
+ @unsent_errors = ['an error']
86
+ }
87
+ assert_equal(1, @agent.unsent_errors_size)
88
+ end
89
+
90
+ def test_unsent_traces_size_empty
91
+ @agent.instance_eval {
92
+ @traces = nil
93
+ }
94
+ assert_equal(nil, @agent.unsent_traces_size)
95
+ end
96
+
97
+ def test_unsent_traces_size_with_traces
98
+ @agent.instance_eval {
99
+ @traces = ['a trace']
100
+ }
101
+ assert_equal(1, @agent.unsent_traces_size)
102
+ end
103
+
104
+ def test_unsent_timeslice_data_empty
105
+ @agent.instance_eval {
106
+ @unsent_timeslice_data = nil
107
+ }
108
+ assert_equal(0, @agent.unsent_timeslice_data, "should have zero timeslice data to start")
109
+ assert_equal({}, @agent.instance_variable_get('@unsent_timeslice_data'), "should initialize the timeslice data to an empty hash if it is empty")
110
+ end
111
+
112
+ def test_unsent_timeslice_data_with_errors
113
+ @agent.instance_eval {
114
+ @unsent_timeslice_data = {:key => 'value'}
115
+ }
116
+ assert_equal(1, @agent.unsent_timeslice_data, "should have the key from above")
117
+ end
118
+
119
+ def test_merge_data_from_all_three_empty
120
+ unsent_timeslice_data = mock('unsent timeslice data')
121
+ unsent_errors = mock('unsent errors')
122
+ unsent_traces = mock('unsent traces')
123
+ @agent.instance_eval {
124
+ @unsent_errors = unsent_errors
125
+ @unsent_timeslice_data = unsent_timeslice_data
126
+ @traces = unsent_traces
127
+ }
128
+ unsent_errors.expects(:+).with([])
129
+ unsent_traces.expects(:+).with([])
130
+ @agent.merge_data_from([{}, [], []])
131
+ end
132
+
133
+ def test_should_not_log_log_file_location_if_no_log_file
134
+ NewRelic::Control.instance.stubs(:log_file).returns('/vasrkjn4b3b4')
135
+ @agent.expects(:log).never
136
+ @agent.notify_log_file_location
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,77 @@
1
+ # Defining a test controller class with a superclass, used to
2
+ # verify correct attribute inheritence
3
+ class NewRelic::Agent::SuperclassController < ActionController::Base
4
+ def base_action
5
+ render :text => 'none'
6
+ end
7
+ end
8
+ # This is a controller class used in testing controller instrumentation
9
+ class NewRelic::Agent::AgentTestController < NewRelic::Agent::SuperclassController
10
+ # filter_parameter_logging :social_security_number
11
+
12
+ @@headers_to_add = nil
13
+
14
+ def index
15
+ sleep params['wait'].to_i if params['wait']
16
+ render :text => params.inspect
17
+ end
18
+ def _filter_parameters(params)
19
+ filter_parameters params
20
+ end
21
+ def action_inline
22
+ render(:inline => "<%= 'foo' %>fah")
23
+ end
24
+
25
+ def action_to_render
26
+ render :text => params.inspect
27
+ end
28
+ def action_to_ignore
29
+ render :text => 'unmeasured'
30
+ end
31
+ def action_to_ignore_apdex
32
+ render :text => 'unmeasured'
33
+ end
34
+ before_filter :oops, :only => :action_with_before_filter_error
35
+ def action_with_before_filter_error
36
+ render :text => 'nothing'
37
+ end
38
+ def oops
39
+ raise "error in before filter"
40
+ end
41
+ class TestException < RuntimeError
42
+ end
43
+
44
+ def rescue_action_locally(exception)
45
+ if exception.is_a? TestException
46
+ raise "error in the handler"
47
+ end
48
+ end
49
+ def action_with_error
50
+ raise "error in action"
51
+ end
52
+ def entry_action
53
+ perform_action_with_newrelic_trace('internal_action') do
54
+ internal_action
55
+ end
56
+ end
57
+
58
+ def self.set_some_headers(hash_of_headers)
59
+ @@headers_to_add ||= {}
60
+ @@headers_to_add.merge!(hash_of_headers)
61
+ end
62
+
63
+ def self.clear_headers
64
+ @@headers_to_add = nil
65
+ end
66
+
67
+ def newrelic_request_headers
68
+ @@headers_to_add ||= {}
69
+ end
70
+
71
+ private
72
+ def internal_action
73
+ perform_action_with_newrelic_trace(:name => 'internal_traced_action', :force => true) do
74
+ render :text => 'internal action'
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,382 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'action_controller/test_case'
3
+ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
4
+ require 'action_controller/base'
5
+ require 'new_relic/agent/agent_test_controller'
6
+
7
+ self.controller_class = NewRelic::Agent::AgentTestController
8
+
9
+ attr_accessor :agent, :engine
10
+
11
+ def test_initialization
12
+ # Suggested by cee-dub for merb tests. I'm actually amazed if our tests work with merb.
13
+ if defined?(Merb::Router)
14
+ Merb::Router.prepare do |r|
15
+ match('/:controller(/:action)(.:format)').register
16
+ end
17
+ elsif NewRelic::Control.instance.rails_version < NewRelic::VersionNumber.new("3.0")
18
+ ActionController::Routing::Routes.draw do |map|
19
+ map.connect '/:controller/:action.:format'
20
+ map.connect '/:controller/:action'
21
+ end
22
+ else
23
+ Rails.application.routes.draw do
24
+ match '/:controller/:action.:format'
25
+ match '/:controller/:action'
26
+ end
27
+ end
28
+
29
+ if defined?(Rails) && Rails.respond_to?(:application) && Rails.application.respond_to?(:routes)
30
+ @routes = Rails.application.routes
31
+ end
32
+
33
+ Thread.current[:newrelic_ignore_controller] = nil
34
+ NewRelic::Agent.manual_start
35
+ @agent = NewRelic::Agent.instance
36
+ # @agent.instrument_app
37
+ agent.transaction_sampler.harvest
38
+ NewRelic::Agent::AgentTestController.class_eval do
39
+ newrelic_ignore :only => [:action_to_ignore, :entry_action, :base_action]
40
+ newrelic_ignore_apdex :only => :action_to_ignore_apdex
41
+ end
42
+ @engine = @agent.stats_engine
43
+ end
44
+
45
+ # Normally you can do this with #setup but for some reason in rails 2.0.2
46
+ # setup is not called.
47
+ if NewRelic::Control.instance.rails_version <= '2.1.0'
48
+ def initialize name
49
+ super name
50
+ test_initialization
51
+ end
52
+ else
53
+ alias_method :setup, :test_initialization
54
+ end
55
+
56
+ def teardown
57
+ Thread.current[:newrelic_ignore_controller] = nil
58
+ NewRelic::Agent.shutdown
59
+ NewRelic::Agent::AgentTestController.clear_headers
60
+ super
61
+ end
62
+
63
+ def test_mongrel_queue
64
+ NewRelic::Agent::AgentTestController.clear_headers
65
+ engine.clear_stats
66
+ NewRelic::Control.instance.local_env.stubs(:mongrel).returns( stub('mongrel', :workers => stub('workers', :list => stub('list', :length => '10'))))
67
+
68
+ get :index
69
+ assert_equal 1, stats('HttpDispatcher').call_count
70
+ assert_equal 1, engine.get_stats_no_scope('Mongrel/Queue Length').call_count
71
+ assert_equal 9, engine.get_stats_no_scope('Mongrel/Queue Length').total_call_time
72
+ assert_equal 0, engine.get_stats_no_scope('WebFrontend/Mongrel/Average Queue Time').call_count
73
+ end
74
+
75
+ def test_heroku_queue
76
+ engine.clear_stats
77
+ NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_HEROKU_QUEUE_DEPTH'=>'15'
78
+ get :index
79
+ assert_equal 1, stats('HttpDispatcher').call_count
80
+ assert_equal 1, engine.get_stats_no_scope('Mongrel/Queue Length').call_count
81
+ assert_equal 15, engine.get_stats_no_scope('Mongrel/Queue Length').total_call_time
82
+ assert_equal 0, engine.get_stats_no_scope('WebFrontend/Mongrel/Average Queue Time').call_count
83
+ end
84
+
85
+ def test_new_queue_integration
86
+ # make this test deterministic
87
+ Time.stubs(:now => Time.at(2))
88
+
89
+ NewRelic::Agent::AgentTestController.clear_headers
90
+ engine.clear_stats
91
+ start = ((Time.now - 1).to_f * 1_000_000).to_i
92
+ NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_QUEUE_START'=> "t=#{start}"
93
+ get :index
94
+
95
+ check_metric_time('WebFrontend/QueueTime', 1, 0.1)
96
+ end
97
+
98
+
99
+ def test_new_middleware_integration
100
+ # make this test deterministic
101
+ Time.stubs(:now => Time.at(2))
102
+
103
+ engine.clear_stats
104
+ start = ((Time.now - 1).to_f * 1_000_000).to_i
105
+ NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_MIDDLEWARE_START'=> "t=#{start}"
106
+ get :index
107
+
108
+ check_metric_time('Middleware/all', 1, 0.1)
109
+ end
110
+
111
+ def test_new_server_time_integration
112
+ # make this test deterministic
113
+ Time.stubs(:now => Time.at(2))
114
+
115
+ NewRelic::Agent::AgentTestController.clear_headers
116
+ engine.clear_stats
117
+ start = ((Time.now - 1).to_f * 1_000_000).to_i
118
+ NewRelic::Agent::AgentTestController.set_some_headers 'HTTP_X_REQUEST_START'=> "t=#{start}"
119
+ get :index
120
+
121
+ check_metric_time('WebFrontend/WebServer/all', 1, 0.1)
122
+ end
123
+
124
+ def test_new_frontend_work_integration
125
+ # make this test deterministic
126
+ Time.stubs(:now => Time.at(10))
127
+
128
+ engine.clear_stats
129
+ times = [Time.now - 3, Time.now - 2, Time.now - 1]
130
+ times.map! {|t| (t.to_f * 1_000_000).to_i }
131
+ NewRelic::Agent::AgentTestController.set_some_headers({
132
+ 'HTTP_X_REQUEST_START'=> "t=#{times[0]}", 'HTTP_X_QUEUE_START' => "t=#{times[1]}", 'HTTP_X_MIDDLEWARE_START' => "t=#{times[2]}"})
133
+ get :index
134
+
135
+
136
+ check_metric_time('WebFrontend/WebServer/all', 1, 0.1)
137
+ check_metric_time('Middleware/all', 1, 0.1)
138
+ check_metric_time('WebFrontend/QueueTime', 1, 0.1)
139
+ end
140
+
141
+ def test_render_inline
142
+ engine.clear_stats
143
+ get :action_inline
144
+ assert_equal 'foofah', @response.body
145
+ compare_metrics %w[Controller/new_relic/agent/agent_test/action_inline], engine.metrics.grep(/^Controller/)
146
+ end
147
+ def test_metric__ignore
148
+ engine.clear_stats
149
+ compare_metrics [], engine.metrics
150
+ get :action_to_ignore
151
+ compare_metrics [], engine.metrics
152
+ end
153
+
154
+ def test_controller_rescued_error
155
+ engine.clear_stats
156
+ assert_raise RuntimeError do
157
+ get :action_with_error
158
+ end
159
+ metrics = ['Apdex',
160
+ 'Apdex/new_relic/agent/agent_test/action_with_error',
161
+ 'HttpDispatcher',
162
+ 'Controller/new_relic/agent/agent_test/action_with_error',
163
+ 'Errors/all',
164
+ 'Middleware/all',
165
+ 'WebFrontend/WebServer/all',
166
+ 'WebFrontend/QueueTime']
167
+
168
+ compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
169
+ assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
170
+ assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
171
+ apdex = engine.get_stats_no_scope("Apdex")
172
+ score = apdex.get_apdex
173
+ assert_equal 1, score[2], 'failing'
174
+ assert_equal 0, score[1], 'tol'
175
+ assert_equal 0, score[0], 'satisfied'
176
+
177
+ end
178
+ def test_controller_error
179
+ engine.clear_stats
180
+ assert_raise RuntimeError do
181
+ get :action_with_error
182
+ end
183
+ metrics = ['Apdex',
184
+ 'Apdex/new_relic/agent/agent_test/action_with_error',
185
+ 'HttpDispatcher',
186
+ 'Controller/new_relic/agent/agent_test/action_with_error',
187
+ 'Errors/all',
188
+ 'WebFrontend/QueueTime',
189
+ 'Middleware/all',
190
+ 'WebFrontend/WebServer/all']
191
+
192
+ compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
193
+ assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
194
+ assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
195
+ apdex = engine.get_stats_no_scope("Apdex")
196
+ score = apdex.get_apdex
197
+ assert_equal 1, score[2], 'failing'
198
+ assert_equal 0, score[1], 'tol'
199
+ assert_equal 0, score[0], 'satisfied'
200
+
201
+ end
202
+ def test_filter_error
203
+ engine.clear_stats
204
+ assert_raise RuntimeError do
205
+ get :action_with_before_filter_error
206
+ end
207
+ metrics = ['Apdex',
208
+ 'Apdex/new_relic/agent/agent_test/action_with_before_filter_error',
209
+ 'HttpDispatcher',
210
+ 'Controller/new_relic/agent/agent_test/action_with_before_filter_error',
211
+ 'Errors/all',
212
+ 'WebFrontend/QueueTime',
213
+ 'Middleware/all',
214
+ 'WebFrontend/WebServer/all']
215
+
216
+ compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0 || m.index('GC')==0}
217
+ assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_before_filter_error").call_count
218
+ assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
219
+ apdex = engine.get_stats_no_scope("Apdex")
220
+ score = apdex.get_apdex
221
+ assert_equal 1, score[2], 'failing'
222
+ assert_equal 0, score[1], 'tol'
223
+ assert_equal 0, score[0], 'satisfied'
224
+ end
225
+ def test_metric__ignore_base
226
+ engine.clear_stats
227
+ get :base_action
228
+ compare_metrics [], engine.metrics
229
+ end
230
+ def test_metric__no_ignore
231
+ path = 'new_relic/agent/agent_test/index'
232
+ index_stats = stats("Controller/#{path}")
233
+ index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
234
+ assert_difference 'index_stats.call_count' do
235
+ assert_difference 'index_apdex_stats.call_count' do
236
+ get :index
237
+ end
238
+ end
239
+ assert_nil Thread.current[:newrelic_ignore_controller]
240
+ end
241
+ def test_metric__ignore_apdex
242
+ engine = @agent.stats_engine
243
+ path = 'new_relic/agent/agent_test/action_to_ignore_apdex'
244
+ cpu_stats = stats("ControllerCPU/#{path}")
245
+ index_stats = stats("Controller/#{path}")
246
+ index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
247
+ assert_difference 'index_stats.call_count' do
248
+ assert_no_difference 'index_apdex_stats.call_count' do
249
+ get :action_to_ignore_apdex
250
+ end
251
+ end
252
+ assert_nil Thread.current[:newrelic_ignore_controller]
253
+
254
+ end
255
+ def test_metric__dispatched
256
+ engine = @agent.stats_engine
257
+ get :entry_action
258
+ assert_nil Thread.current[:newrelic_ignore_controller]
259
+ assert_nil engine.lookup_stats('Controller/agent_test/entry_action')
260
+ assert_nil engine.lookup_stats('Controller/agent_test_controller/entry_action')
261
+ assert_nil engine.lookup_stats('Controller/AgentTestController/entry_action')
262
+ assert_nil engine.lookup_stats('Controller/NewRelic::Agent::AgentTestController/internal_action')
263
+ assert_nil engine.lookup_stats('Controller/NewRelic::Agent::AgentTestController_controller/internal_action')
264
+ assert_not_nil engine.lookup_stats('Controller/NewRelic::Agent::AgentTestController/internal_traced_action')
265
+ end
266
+ def test_action_instrumentation
267
+ get :index, :foo => 'bar'
268
+ assert_match /bar/, @response.body
269
+ end
270
+
271
+ def test_controller_params
272
+ assert agent.transaction_sampler
273
+ num_samples = NewRelic::Agent.instance.transaction_sampler.samples.length
274
+ assert_equal "[FILTERED]", @controller._filter_parameters({'social_security_number' => 'test'})['social_security_number']
275
+ get :index, 'social_security_number' => "001-555-1212"
276
+ samples = agent.transaction_sampler.samples
277
+ assert_equal num_samples + 1, samples.length
278
+ assert_equal "[FILTERED]", samples.last.params[:request_params]["social_security_number"]
279
+ end
280
+
281
+ def test_controller_params
282
+ agent.transaction_sampler.reset!
283
+ get :index, 'number' => "001-555-1212"
284
+ s = agent.transaction_sampler.harvest(nil, 0.0)
285
+ assert_equal 1, s.size
286
+ assert_equal 6, s.first.params.size
287
+ end
288
+
289
+
290
+ def test_busycalculation
291
+ engine.clear_stats
292
+ assert_equal 0, NewRelic::Agent::BusyCalculator.busy_count
293
+ get :index, 'social_security_number' => "001-555-1212", 'wait' => '0.05'
294
+ NewRelic::Agent::BusyCalculator.harvest_busy
295
+
296
+ assert_equal 1, stats('Instance/Busy').call_count
297
+ assert_equal 1, stats('HttpDispatcher').call_count
298
+ # We are probably busy about 99% of the time, but lets make sure it's at least 50
299
+ assert stats('Instance/Busy').total_call_time > (0.5 * 0.05), stats('Instance/Busy').inspect
300
+ assert_equal 0, stats('WebFrontend/Mongrel/Average Queue Time').call_count
301
+ end
302
+
303
+ def test_queue_headers_no_header
304
+ engine.clear_stats
305
+ queue_length_stat = stats('Mongrel/Queue Length')
306
+ queue_time_stat = stats('WebFrontend/QueueTime')
307
+
308
+ # no request start header
309
+ get 'index'
310
+ assert_equal 0, queue_length_stat.call_count
311
+ end
312
+
313
+ def test_queue_headers_apache
314
+ # make this test deterministic
315
+ Time.stubs(:now => Time.at(10))
316
+
317
+ NewRelic::Agent::AgentTestController.clear_headers
318
+ engine.clear_stats
319
+ queue_length_stat = stats('Mongrel/Queue Length')
320
+ queue_time_stat = stats('WebFrontend/QueueTime')
321
+
322
+ # apache version of header
323
+ request_start = ((Time.now.to_f - 0.5) * 1e6).to_i.to_s
324
+ NewRelic::Agent::AgentTestController.set_some_headers({'HTTP_X_QUEUE_START' => "t=#{request_start}"})
325
+ get :index
326
+ assert_equal(0, queue_length_stat.call_count, 'We should not be seeing a queue length yet')
327
+ assert_equal(1, queue_time_stat.call_count, 'We should have seen the queue header once')
328
+ assert(queue_time_stat.total_call_time > 0.1, "Queue time should be longer than 100ms")
329
+ assert(queue_time_stat.total_call_time < 10, "Queue time should be under 10 seconds (sanity check)")
330
+
331
+ end
332
+ def test_queue_headers_heroku
333
+ # make this test deterministic
334
+ Time.stubs(:now => Time.at(10))
335
+
336
+ engine.clear_stats
337
+ NewRelic::Agent::AgentTestController.clear_headers
338
+
339
+ queue_length_stat = stats('Mongrel/Queue Length')
340
+ queue_time_stat = stats('WebFrontend/QueueTime')
341
+
342
+ # heroku version
343
+ request_start = ((Time.now.to_f - 0.5) * 1e6).to_i.to_s
344
+ NewRelic::Agent::AgentTestController.set_some_headers({'HTTP_X_QUEUE_START' => "t=#{request_start}", 'HTTP_X_HEROKU_QUEUE_DEPTH' => '0'})
345
+ get :index
346
+ assert_equal(0, queue_length_stat.total_call_time, 'queue should be empty')
347
+ assert_equal(1, queue_time_stat.call_count, 'should have seen the queue header once')
348
+ assert(queue_time_stat.total_call_time > 0.1, "Queue time should be longer than 100ms")
349
+ assert(queue_time_stat.total_call_time < 10, "Queue time should be under 10 seconds (sanity check)")
350
+ end
351
+
352
+ def test_queue_headers_heroku_queue_length
353
+ # make this test deterministic
354
+ Time.stubs(:now => Time.at(10))
355
+
356
+ engine.clear_stats
357
+ NewRelic::Agent::AgentTestController.clear_headers
358
+
359
+ queue_length_stat = stats('Mongrel/Queue Length')
360
+ queue_time_stat = stats('WebFrontend/QueueTime')
361
+
362
+ # heroku version with queue length > 0
363
+ request_start = ((Time.now.to_f - 0.5) * 1e6).to_i.to_s
364
+ NewRelic::Agent::AgentTestController.set_some_headers({'HTTP_X_QUEUE_START' => "t=#{request_start}", 'HTTP_X_HEROKU_QUEUE_DEPTH' => '3'})
365
+ get :index
366
+
367
+ assert_equal(1, queue_length_stat.call_count, 'queue should have been seen once')
368
+ assert_equal(1, queue_time_stat.call_count, 'should have seen the queue header once')
369
+ assert(queue_time_stat.total_call_time > 0.1, "Queue time should be longer than 100ms")
370
+ assert(queue_time_stat.total_call_time < 10, "Queue time should be under 10 seconds (sanity check)")
371
+ assert_equal(3, queue_length_stat.total_call_time, 'queue should be 3 long')
372
+
373
+ NewRelic::Agent::AgentTestController.clear_headers
374
+ end
375
+
376
+ private
377
+ def stats(name)
378
+ engine.get_stats_no_scope(name)
379
+ end
380
+
381
+ end if defined? Rails
382
+