wd_newrelic_rpm 3.3.4.1 → 3.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (290) hide show
  1. data/.gitignore +20 -0
  2. data/.project +23 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +317 -39
  5. data/GUIDELINES_FOR_CONTRIBUTING.md +76 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE +1 -1
  8. data/{README.rdoc → README.md} +71 -55
  9. data/Rakefile +62 -0
  10. data/bin/mongrel_rpm +1 -1
  11. data/config.dot +290 -0
  12. data/config/database.yml +5 -0
  13. data/init.rb +31 -0
  14. data/lib/new_relic/agent.rb +31 -60
  15. data/lib/new_relic/agent/agent.rb +277 -502
  16. data/lib/new_relic/agent/agent_logger.rb +165 -0
  17. data/lib/new_relic/agent/audit_logger.rb +72 -0
  18. data/lib/new_relic/agent/beacon_configuration.rb +36 -50
  19. data/lib/new_relic/agent/browser_monitoring.rb +114 -61
  20. data/lib/new_relic/agent/busy_calculator.rb +14 -6
  21. data/lib/new_relic/agent/configuration.rb +74 -0
  22. data/lib/new_relic/agent/configuration/defaults.rb +126 -0
  23. data/lib/new_relic/agent/configuration/environment_source.rb +49 -0
  24. data/lib/new_relic/agent/configuration/manager.rb +136 -0
  25. data/lib/new_relic/agent/configuration/mask_defaults.rb +10 -0
  26. data/lib/new_relic/agent/configuration/server_source.rb +27 -0
  27. data/lib/new_relic/agent/configuration/yaml_source.rb +63 -0
  28. data/lib/new_relic/agent/cross_process_monitoring.rb +43 -0
  29. data/lib/new_relic/agent/database.rb +39 -26
  30. data/lib/new_relic/agent/error_collector.rb +48 -49
  31. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  32. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -7
  33. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +1 -1
  34. data/lib/new_relic/agent/instrumentation/authlogic.rb +1 -1
  35. data/lib/new_relic/agent/instrumentation/browser_monitoring_timings.rb +41 -0
  36. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +35 -12
  37. data/lib/new_relic/agent/instrumentation/data_mapper.rb +4 -12
  38. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +2 -2
  39. data/lib/new_relic/agent/instrumentation/memcache.rb +6 -6
  40. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  42. data/lib/new_relic/agent/instrumentation/metric_frame.rb +4 -14
  43. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -1
  44. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  45. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +6 -20
  46. data/lib/new_relic/agent/instrumentation/queue_time.rb +2 -2
  47. data/lib/new_relic/agent/instrumentation/rack.rb +1 -1
  48. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +7 -7
  49. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  50. data/lib/new_relic/agent/instrumentation/rails/errors.rb +1 -1
  51. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -5
  52. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +6 -2
  53. data/lib/new_relic/agent/instrumentation/resque.rb +81 -0
  54. data/lib/new_relic/agent/instrumentation/sinatra.rb +21 -10
  55. data/lib/new_relic/agent/instrumentation/sunspot.rb +1 -1
  56. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +5 -4
  57. data/lib/new_relic/agent/method_tracer.rb +11 -9
  58. data/lib/new_relic/agent/new_relic_service.rb +379 -0
  59. data/lib/new_relic/agent/pipe_channel_manager.rb +175 -0
  60. data/lib/new_relic/agent/pipe_service.rb +58 -0
  61. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +86 -0
  62. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -8
  63. data/lib/new_relic/agent/sql_sampler.rb +31 -74
  64. data/lib/new_relic/agent/stats_engine.rb +0 -5
  65. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +40 -24
  66. data/lib/new_relic/agent/stats_engine/metric_stats.rb +89 -14
  67. data/lib/new_relic/agent/stats_engine/samplers.rb +9 -7
  68. data/lib/new_relic/agent/stats_engine/transactions.rb +20 -12
  69. data/lib/new_relic/agent/thread.rb +32 -0
  70. data/lib/new_relic/agent/thread_profiler.rb +306 -0
  71. data/lib/new_relic/agent/transaction_info.rb +24 -4
  72. data/lib/new_relic/agent/transaction_sample_builder.rb +12 -13
  73. data/lib/new_relic/agent/transaction_sampler.rb +49 -65
  74. data/lib/new_relic/agent/worker_loop.rb +31 -25
  75. data/lib/new_relic/collection_helper.rb +1 -1
  76. data/lib/new_relic/commands/deployments.rb +19 -10
  77. data/lib/new_relic/control.rb +1 -3
  78. data/lib/new_relic/control/class_methods.rb +10 -5
  79. data/lib/new_relic/control/frameworks/merb.rb +0 -6
  80. data/lib/new_relic/control/frameworks/rails.rb +30 -45
  81. data/lib/new_relic/control/frameworks/rails3.rb +23 -18
  82. data/lib/new_relic/control/frameworks/rails4.rb +23 -0
  83. data/lib/new_relic/control/frameworks/ruby.rb +2 -23
  84. data/lib/new_relic/control/instance_methods.rb +35 -73
  85. data/lib/new_relic/control/instrumentation.rb +7 -12
  86. data/lib/new_relic/control/server_methods.rb +17 -19
  87. data/lib/new_relic/delayed_job_injection.rb +2 -2
  88. data/lib/new_relic/helper.rb +34 -0
  89. data/lib/new_relic/language_support.rb +56 -37
  90. data/lib/new_relic/local_environment.rb +32 -67
  91. data/lib/new_relic/metric_data.rb +10 -2
  92. data/lib/new_relic/metric_spec.rb +7 -3
  93. data/lib/new_relic/noticed_error.rb +32 -9
  94. data/lib/new_relic/rack.rb +4 -0
  95. data/lib/new_relic/rack/browser_monitoring.rb +34 -25
  96. data/lib/new_relic/rack/developer_mode.rb +3 -0
  97. data/lib/new_relic/rack/error_collector.rb +56 -0
  98. data/lib/new_relic/stats.rb +9 -7
  99. data/lib/new_relic/transaction_sample.rb +19 -18
  100. data/lib/new_relic/transaction_sample/segment.rb +13 -15
  101. data/lib/new_relic/version.rb +19 -3
  102. data/lib/newrelic_rpm.rb +1 -1
  103. data/lib/tasks/tests.rake +6 -8
  104. data/newrelic.yml +15 -32
  105. data/newrelic_rpm.gemspec +224 -43
  106. data/newrelic_rpm.gemspec.erb +54 -0
  107. data/test/config/newrelic.yml +4 -3
  108. data/test/config/test_control.rb +18 -18
  109. data/test/fixtures/gemspec_no_build.rb +442 -0
  110. data/test/fixtures/gemspec_with_build.rb +442 -0
  111. data/test/fixtures/gemspec_with_build_and_stage.rb +442 -0
  112. data/test/intentional_fail.rb +10 -0
  113. data/test/multiverse/.gitignore +10 -0
  114. data/test/multiverse/README.md +85 -0
  115. data/test/multiverse/lib/multiverse/color.rb +13 -0
  116. data/test/multiverse/lib/multiverse/envfile.rb +66 -0
  117. data/test/multiverse/lib/multiverse/environment.rb +16 -0
  118. data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
  119. data/test/multiverse/lib/multiverse/runner.rb +44 -0
  120. data/test/multiverse/lib/multiverse/suite.rb +162 -0
  121. data/test/multiverse/script/run_one +3 -0
  122. data/test/multiverse/script/runner +9 -0
  123. data/test/multiverse/suites/active_record/Envfile +13 -0
  124. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
  125. data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
  126. data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
  127. data/test/multiverse/suites/agent_only/Envfile +4 -0
  128. data/test/multiverse/suites/agent_only/audit_log_test.rb +99 -0
  129. data/test/multiverse/suites/agent_only/config/newrelic.yml +22 -0
  130. data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
  131. data/test/multiverse/suites/agent_only/marshaling_test.rb +109 -0
  132. data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
  133. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
  134. data/test/multiverse/suites/agent_only/service_timeout_test.rb +29 -0
  135. data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
  136. data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
  137. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +80 -0
  138. data/test/multiverse/suites/config_file_loading/Envfile +7 -0
  139. data/test/multiverse/suites/config_file_loading/config_file_loading_test.rb +106 -0
  140. data/test/multiverse/suites/datamapper/Envfile +8 -0
  141. data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
  142. data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
  143. data/test/multiverse/suites/logging/Envfile +4 -0
  144. data/test/multiverse/suites/logging/config/newrelic.yml +22 -0
  145. data/test/multiverse/suites/logging/logging_test.rb +143 -0
  146. data/test/multiverse/suites/monitor_mode_false/Envfile +2 -0
  147. data/test/multiverse/suites/monitor_mode_false/config/newrelic.yml +25 -0
  148. data/test/multiverse/suites/monitor_mode_false/no_dns_resolv.rb +29 -0
  149. data/test/multiverse/suites/no_load/Envfile +2 -0
  150. data/test/multiverse/suites/no_load/config/newrelic.yml +22 -0
  151. data/test/multiverse/suites/no_load/start_up_test.rb +14 -0
  152. data/test/multiverse/suites/rails_3_error_tracing/Envfile +15 -0
  153. data/test/multiverse/suites/rails_3_error_tracing/config/newrelic.yml +165 -0
  154. data/test/multiverse/suites/rails_3_error_tracing/error_tracing_test.rb +236 -0
  155. data/test/multiverse/suites/rails_3_gc/Envfile +8 -0
  156. data/test/multiverse/suites/rails_3_gc/config/newrelic.yml +167 -0
  157. data/test/multiverse/suites/rails_3_gc/instrumentation_test.rb +92 -0
  158. data/test/multiverse/suites/rails_3_queue_time/Envfile +15 -0
  159. data/test/multiverse/suites/rails_3_queue_time/config/newrelic.yml +165 -0
  160. data/test/multiverse/suites/rails_3_queue_time/queue_time_test.rb +75 -0
  161. data/test/multiverse/suites/rails_3_views/.gitignore +3 -0
  162. data/test/multiverse/suites/rails_3_views/Envfile +16 -0
  163. data/test/multiverse/suites/rails_3_views/app/views/foos/_foo.html.haml +1 -0
  164. data/test/multiverse/suites/rails_3_views/app/views/test/_a_partial.html.erb +1 -0
  165. data/test/multiverse/suites/rails_3_views/app/views/test/_mid_partial.html.erb +1 -0
  166. data/test/multiverse/suites/rails_3_views/app/views/test/_top_partial.html.erb +3 -0
  167. data/test/multiverse/suites/rails_3_views/app/views/test/deep_partial.html.erb +3 -0
  168. data/test/multiverse/suites/rails_3_views/app/views/test/haml_view.html.haml +6 -0
  169. data/test/multiverse/suites/rails_3_views/app/views/test/index.html.erb +4 -0
  170. data/test/multiverse/suites/rails_3_views/config/newrelic.yml +164 -0
  171. data/test/multiverse/suites/rails_3_views/view_instrumentation_test.rb +245 -0
  172. data/test/multiverse/suites/resque/Envfile +21 -0
  173. data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
  174. data/test/multiverse/suites/resque/dump.rdb +0 -0
  175. data/test/multiverse/suites/resque/instrumentation_test.rb +73 -0
  176. data/test/multiverse/suites/rum_auto_instrumentation/Envfile +4 -0
  177. data/test/multiverse/suites/rum_auto_instrumentation/config/newrelic.yml +24 -0
  178. data/test/multiverse/suites/rum_auto_instrumentation/responses/worst_case_small.html +5000 -0
  179. data/test/multiverse/suites/rum_auto_instrumentation/sanity_test.rb +102 -0
  180. data/test/multiverse/suites/sinatra/Envfile +13 -0
  181. data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
  182. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +77 -0
  183. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
  184. data/test/multiverse/suites/sinatra/sinatra_test.rb +55 -0
  185. data/test/multiverse/test/multiverse_test.rb +55 -0
  186. data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
  187. data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
  188. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
  189. data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
  190. data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
  191. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
  192. data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
  193. data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
  194. data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
  195. data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
  196. data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
  197. data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
  198. data/test/new_relic/agent/agent/connect_test.rb +151 -227
  199. data/test/new_relic/agent/agent/start_test.rb +68 -118
  200. data/test/new_relic/agent/agent/start_worker_thread_test.rb +12 -74
  201. data/test/new_relic/agent/agent_logger_test.rb +153 -0
  202. data/test/new_relic/agent/agent_test.rb +116 -30
  203. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  204. data/test/new_relic/agent/agent_test_controller_test.rb +42 -10
  205. data/test/new_relic/agent/audit_logger_test.rb +105 -0
  206. data/test/new_relic/agent/beacon_configuration_test.rb +63 -67
  207. data/test/new_relic/agent/browser_monitoring_test.rb +151 -79
  208. data/test/new_relic/agent/busy_calculator_test.rb +7 -0
  209. data/test/new_relic/agent/configuration/environment_source_test.rb +79 -0
  210. data/test/new_relic/agent/configuration/manager_test.rb +204 -0
  211. data/test/new_relic/agent/configuration/server_source_test.rb +45 -0
  212. data/test/new_relic/agent/configuration/yaml_source_test.rb +75 -0
  213. data/test/new_relic/agent/cross_process_monitoring_test.rb +77 -0
  214. data/test/new_relic/agent/database_test.rb +12 -11
  215. data/test/new_relic/agent/error_collector/notice_error_test.rb +64 -53
  216. data/test/new_relic/agent/error_collector_test.rb +33 -19
  217. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +60 -30
  218. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +39 -0
  219. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +1 -1
  220. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +6 -0
  221. data/test/new_relic/agent/instrumentation/queue_time_test.rb +14 -0
  222. data/test/new_relic/agent/instrumentation/sinatra_test.rb +25 -0
  223. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  224. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +4 -10
  225. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +3 -15
  226. data/test/new_relic/agent/method_tracer_test.rb +7 -6
  227. data/test/new_relic/agent/mock_scope_listener.rb +3 -0
  228. data/test/new_relic/agent/new_relic_service_test.rb +376 -0
  229. data/test/new_relic/agent/pipe_channel_manager_test.rb +131 -0
  230. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  231. data/test/new_relic/agent/rpm_agent_test.rb +27 -50
  232. data/test/new_relic/agent/sql_sampler_test.rb +81 -56
  233. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +3 -20
  234. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +12 -1
  235. data/test/new_relic/agent/stats_engine_test.rb +17 -8
  236. data/test/new_relic/agent/thread_profiler_test.rb +537 -0
  237. data/test/new_relic/agent/thread_test.rb +89 -0
  238. data/test/new_relic/agent/threaded_test.rb +65 -0
  239. data/test/new_relic/agent/transaction_info_test.rb +45 -4
  240. data/test/new_relic/agent/transaction_sample_builder_test.rb +8 -7
  241. data/test/new_relic/agent/transaction_sampler_test.rb +193 -204
  242. data/test/new_relic/agent/worker_loop_test.rb +26 -11
  243. data/test/new_relic/agent_test.rb +113 -33
  244. data/test/new_relic/collection_helper_test.rb +7 -8
  245. data/test/new_relic/command/deployments_test.rb +18 -2
  246. data/test/new_relic/control/class_methods_test.rb +0 -18
  247. data/test/new_relic/control/frameworks/rails_test.rb +26 -0
  248. data/test/new_relic/control_test.rb +96 -137
  249. data/test/new_relic/delayed_job_injection_test.rb +6 -1
  250. data/test/new_relic/dispatcher_test.rb +54 -0
  251. data/test/new_relic/fake_collector.rb +283 -0
  252. data/test/new_relic/fake_service.rb +53 -0
  253. data/test/new_relic/fakes_sending_data.rb +30 -0
  254. data/test/new_relic/framework_test.rb +53 -0
  255. data/test/new_relic/load_test.rb +13 -0
  256. data/test/new_relic/local_environment_test.rb +11 -11
  257. data/test/new_relic/metric_data_test.rb +45 -16
  258. data/test/new_relic/noticed_error_test.rb +24 -0
  259. data/test/new_relic/rack/browser_monitoring_test.rb +20 -10
  260. data/test/new_relic/rack/developer_mode_test.rb +13 -7
  261. data/test/new_relic/rack/error_collector_test.rb +74 -0
  262. data/test/new_relic/stats_test.rb +10 -0
  263. data/test/new_relic/transaction_sample/segment_test.rb +23 -4
  264. data/test/new_relic/transaction_sample_test.rb +47 -2
  265. data/test/new_relic/version_number_test.rb +32 -0
  266. data/test/script/build_test_gem.sh +9 -3
  267. data/test/script/ci.sh +108 -35
  268. data/test/script/ci_agent-tests_runner.sh +82 -0
  269. data/test/script/ci_multiverse_runner.sh +63 -0
  270. data/test/test_contexts.rb +1 -0
  271. data/test/test_helper.rb +68 -18
  272. data/ui/helpers/developer_mode_helper.rb +21 -11
  273. data/ui/views/layouts/newrelic_default.rhtml +1 -0
  274. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  275. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  276. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  277. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  278. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  279. data/ui/views/newrelic/show_sample.rhtml +1 -1
  280. data/ui/views/newrelic/threads.rhtml +2 -10
  281. data/vendor/gems/metric_parser-0.1.0.pre1/.specification +116 -0
  282. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_init.rb +7 -0
  283. metadata +191 -65
  284. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  285. data/lib/new_relic/control/configuration.rb +0 -206
  286. data/lib/new_relic/control/logging_methods.rb +0 -143
  287. data/lib/new_relic/data_serialization.rb +0 -151
  288. data/test/new_relic/control/configuration_test.rb +0 -84
  289. data/test/new_relic/control/logging_methods_test.rb +0 -185
  290. data/test/new_relic/data_serialization_test.rb +0 -208
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'new_relic/agent/thread'
3
+
4
+ class ThreadTest < Test::Unit::TestCase
5
+
6
+ def test_sets_label
7
+ t = NewRelic::Agent::AgentThread.new("labelled") {}
8
+ assert_equal "labelled", t[:newrelic_label]
9
+ end
10
+
11
+ def test_bucket_thread_as_agent_when_profiling
12
+ t = NewRelic::Agent::AgentThread.new("labelled") {}
13
+ assert_equal :agent, NewRelic::Agent::AgentThread.bucket_thread(t, true)
14
+ end
15
+
16
+ def test_bucket_thread_as_agent_when_not_profiling
17
+ t = NewRelic::Agent::AgentThread.new("labelled") {}
18
+ assert_equal :ignore, NewRelic::Agent::AgentThread.bucket_thread(t, false)
19
+ end
20
+
21
+ def test_bucket_thread_as_request
22
+ t = ::Thread.new {}
23
+ frame = NewRelic::Agent::Instrumentation::MetricFrame.new
24
+ frame.request = "has a request"
25
+ t[:newrelic_metric_frame] = frame
26
+
27
+ assert_equal :request, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
28
+ end
29
+
30
+ def test_bucket_thread_as_background
31
+ t = ::Thread.new {}
32
+ frame = NewRelic::Agent::Instrumentation::MetricFrame.new
33
+ t[:newrelic_metric_frame] = frame
34
+
35
+ assert_equal :background, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
36
+ end
37
+
38
+ def test_bucket_thread_as_other_if_nil_frame
39
+ t = ::Thread.new {}
40
+ t[:newrelic_metric_frame] = nil
41
+
42
+ assert_equal :other, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
43
+ end
44
+
45
+ def test_bucket_thread_as_other
46
+ t = ::Thread.new {}
47
+ assert_equal :other, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
48
+ end
49
+
50
+ def test_runs_block
51
+ called = false
52
+
53
+ t = NewRelic::Agent::AgentThread.new("labelled") { called = true }
54
+ t.join
55
+
56
+ assert called
57
+ end
58
+
59
+ TRACE = [
60
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:100:in `catch'",
61
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.5.3.452.dev/lib/new_relic/agent/agent.rb:200:in `start_worker_thread'",
62
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/thin-1.5.0/lib/thin/backends/base.rb:300:in `block (3 levels) in run'",
63
+ ]
64
+
65
+ def test_scrubs_backtrace_when_not_profiling_agent_code
66
+ result = NewRelic::Agent::AgentThread.scrub_backtrace(stub(:backtrace => TRACE), false)
67
+ assert_equal [TRACE[0], TRACE[2]], result
68
+ end
69
+
70
+ def test_doesnt_scrub_backtrace_when_profiling_agent_code
71
+ result = NewRelic::Agent::AgentThread.scrub_backtrace(stub(:backtrace => TRACE), true)
72
+ assert_equal TRACE, result
73
+ end
74
+
75
+ def test_scrub_backtrace_handles_errors_during_backtrace
76
+ dummy_thread = stub
77
+ dummy_thread.stubs(:backtrace).raises(StandardError.new('nah'))
78
+ assert_nothing_raised do
79
+ NewRelic::Agent::AgentThread.scrub_backtrace(dummy_thread, true)
80
+ end
81
+ end
82
+
83
+ def test_scrub_backtrace_handles_nil_backtrace
84
+ bt = NewRelic::Agent::AgentThread.scrub_backtrace(stub(:backtrace => nil), false)
85
+ assert_nil(bt)
86
+ end
87
+
88
+ DONT_CARE = true
89
+ end
@@ -0,0 +1,65 @@
1
+ class ThreadedTest < Test::Unit::TestCase
2
+ def setup
3
+ @original_thread_class = NewRelic::Agent::AgentThread
4
+ swap_thread_class(FakeThread)
5
+ end
6
+
7
+ def teardown
8
+ swap_thread_class(@original_thread_class)
9
+ @original_thread_class = nil
10
+
11
+ FakeThread.list.clear
12
+ end
13
+
14
+ def default_test
15
+ # no-op to keep quiet....
16
+ end
17
+
18
+ private
19
+
20
+ def swap_thread_class(klass)
21
+ NewRelic::Agent.send(:remove_const, "AgentThread") if NewRelic::Agent.const_defined?("AgentThread")
22
+ NewRelic::Agent.const_set("AgentThread", klass)
23
+ end
24
+ end
25
+
26
+ class FakeThread
27
+ @@list = []
28
+
29
+ def initialize(locals={}, &block)
30
+ @locals = locals
31
+ yield if block_given?
32
+ end
33
+
34
+ def self.current
35
+ {}
36
+ end
37
+
38
+ def self.list
39
+ @@list
40
+ end
41
+
42
+ def self.bucket_thread(thread, _)
43
+ thread[:bucket]
44
+ end
45
+
46
+ def self.scrub_backtrace(thread, _)
47
+ thread[:scrubbed_backtrace] || thread.backtrace
48
+ end
49
+
50
+ def key?(key)
51
+ @locals.key?(key)
52
+ end
53
+
54
+ def [](key)
55
+ @locals[key]
56
+ end
57
+
58
+ def backtrace
59
+ @locals[:backtrace] || []
60
+ end
61
+
62
+ def join
63
+ end
64
+ end
65
+
@@ -3,11 +3,52 @@ require 'ostruct'
3
3
 
4
4
  class NewRelic::Agent::TransactionInfoTest < Test::Unit::TestCase
5
5
  def setup
6
- @request = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=1234<tag>evil</tag>5678'})
6
+ @request = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=12345678'})
7
+ @request_with_embedded_tag = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=1234<tag>evil</tag>5678'})
8
+ @request_with_embedded_utf8_encoded_js = OpenStruct.new(:cookies => {'NRAGENT' => "tk=1234&#34&#93&#41&#595678"})
9
+ @request_with_double_quotes = OpenStruct.new(:cookies => {'NRAGENT' => 'tk="""deadbeef"""'})
10
+ @request_with_single_quotes = OpenStruct.new(:cookies => {'NRAGENT' => "tk='''deadbeef'''"})
11
+ @request_with_multi_lt = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=<<<deadbeef'})
12
+ @request_with_multi_gt = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=>>>deadbeef'})
13
+ @request_with_bare_token = OpenStruct.new(:cookies => {'NRAGENT' => 0xdeadbeef})
14
+ @request_with_nil_token = OpenStruct.new(:cookies => {'NRAGENT' => nil})
7
15
  end
8
16
 
9
- def test_get_token_gets_sanitized_token_from_cookie
10
- assert_equal('1234&lt;tag&gt;evil&lt;/tag&gt;5678',
11
- NewRelic::Agent::TransactionInfo.get_token(@request))
17
+ def test_get_token_safe_token_returned_untouched
18
+ assert_equal("12345678", NewRelic::Agent::TransactionInfo.get_token(@request))
12
19
  end
20
+
21
+ def test_get_token_with_embedded_tags_sanitized
22
+ assert_equal("",NewRelic::Agent::TransactionInfo.get_token(@request_with_embedded_tag))
23
+ end
24
+
25
+ def test_get_token_with_embedded_utf8_js_sanitized
26
+ assert_equal("1234&amp;#34&amp;#93&amp;#41&amp;#595678",
27
+ NewRelic::Agent::TransactionInfo.get_token(@request_with_embedded_utf8_encoded_js))
28
+ end
29
+
30
+ def test_get_token_replaces_double_quoted_token_with_empty_string
31
+ assert_equal("", NewRelic::Agent::TransactionInfo.get_token(@request_with_double_quotes))
32
+ end
33
+
34
+ def test_get_token_replaces_single_quoted_toket_with_empty_string
35
+ assert_equal("", NewRelic::Agent::TransactionInfo.get_token(@request_with_single_quotes))
36
+ end
37
+
38
+ def test_get_token_replaces_token_started_with_multiple_Lt_with_empty_string
39
+ assert_equal("", NewRelic::Agent::TransactionInfo.get_token(@request_with_multi_lt))
40
+ end
41
+
42
+ def test_get_token_replaces_token_started_with_multiple_gt_with_empty_string
43
+ assert_equal("", NewRelic::Agent::TransactionInfo.get_token(@request_with_multi_gt))
44
+ end
45
+
46
+ def test_get_token_bare_value_replaced_with_nil
47
+ assert_equal(nil,NewRelic::Agent::TransactionInfo.get_token(@request_with_bare_token))
48
+ end
49
+
50
+ def test_get_token_nil_token_returns_nil_token
51
+ assert_equal(nil,NewRelic::Agent::TransactionInfo.get_token(@request_with_ni_token))
52
+ end
53
+
13
54
  end
@@ -171,19 +171,20 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
171
171
  end
172
172
 
173
173
  def test_trace_should_not_record_more_than_segment_limit
174
- @builder.segment_limit = 3
175
- 8.times {|i| build_segment i.to_s }
176
- assert_equal 3, @builder.sample.count_segments
174
+ with_config(:'transaction_tracer.limit_segments' => 3) do
175
+ 8.times {|i| build_segment i.to_s }
176
+ assert_equal 3, @builder.sample.count_segments
177
+ end
177
178
  end
178
179
 
179
180
  # regression
180
181
  def test_trace_should_log_segment_reached_once
181
- @builder.segment_limit = 3
182
- NewRelic::Control.instance.log.expects(:debug).once
183
- 8.times {|i| build_segment i.to_s }
182
+ with_config(:'transaction_tracer.limit_segments' => 3) do
183
+ expects_logging(:debug, includes("Segment limit"))
184
+ 8.times {|i| build_segment i.to_s }
185
+ end
184
186
  end
185
187
 
186
-
187
188
  def validate_builder(check_names = true)
188
189
  validate_segment @builder.sample.root_segment, check_names
189
190
  end
@@ -27,11 +27,14 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
27
27
  agent.stubs(:stats_engine).returns(stats_engine)
28
28
  @sampler = NewRelic::Agent::TransactionSampler.new
29
29
  stats_engine.transaction_sampler = @sampler
30
+ @test_config = { :'transaction_tracer.enabled' => true }
31
+ NewRelic::Agent.config.apply_config(@test_config)
30
32
  end
31
33
 
32
34
  def teardown
33
35
  super
34
36
  Thread.current[:transaction_sample_builder] = nil
37
+ NewRelic::Agent.config.remove_config(@test_config)
35
38
  end
36
39
 
37
40
  def test_initialize
@@ -45,27 +48,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
45
48
  assert_equal(default_value, @sampler.instance_variable_get('@' + variable.to_s))
46
49
  end
47
50
 
48
- segment_limit = @sampler.instance_variable_get('@segment_limit')
49
- assert(segment_limit.is_a?(Numeric), "Segment limit should be numeric")
50
- assert(segment_limit > 0, "Segment limit should be above zero")
51
-
52
- stack_trace_threshold = @sampler.instance_variable_get('@stack_trace_threshold')
53
- assert(stack_trace_threshold.is_a?((0.1).class), "Stack trace threshold should be a #{(0.1).class.inspect}, but is #{stack_trace_threshold.inspect}")
54
- assert(stack_trace_threshold > 0.0, "Stack trace threshold should be above zero")
55
-
56
51
  lock = @sampler.instance_variable_get('@samples_lock')
57
52
  assert(lock.is_a?(Mutex), "Samples lock should be a mutex, is: #{lock.inspect}")
58
53
  end
59
54
 
60
- def test_configure
61
- control = NewRelic::Control.instance
62
- control.merge_options('transaction_tracer' => {'stack_trace_threshold' => 5.0, 'limit_segments' => 20, 'explain_threshold' => 4.0})
63
- @sampler.configure!
64
- assert_equal 20, @sampler.instance_variable_get('@segment_limit')
65
- assert_equal 5.0, @sampler.instance_variable_get('@stack_trace_threshold')
66
- assert_equal 4.0, @sampler.instance_variable_get('@explain_threshold')
67
- end
68
-
69
55
  def test_current_sample_id_default
70
56
  builder = mock('builder')
71
57
  builder.expects(:sample_id).returns(11111)
@@ -78,20 +64,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
78
64
  assert_equal(nil, @sampler.current_sample_id)
79
65
  end
80
66
 
81
- def test_enable
82
- assert_equal(nil, @sampler.instance_variable_get('@disabled'))
83
- @sampler.enable
84
- assert_equal(false, @sampler.instance_variable_get('@disabled'))
85
- assert_equal(@sampler, NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler'))
86
- end
87
-
88
- def test_disable
89
- assert_nil @sampler.instance_variable_get('@disabled')
90
- @sampler.disable
91
- assert @sampler.instance_variable_get('@disabled')
92
- assert_nil NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler')
93
- end
94
-
95
67
  def test_sampling_rate_equals_default
96
68
  @sampler.sampling_rate = 1
97
69
  assert_equal(1, @sampler.instance_variable_get('@sampling_rate'))
@@ -107,15 +79,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
107
79
  end
108
80
 
109
81
  def test_notice_first_scope_push_default
110
- @sampler.expects(:disabled).returns(false)
111
82
  @sampler.expects(:start_builder).with(100.0)
112
83
  @sampler.notice_first_scope_push(Time.at(100))
113
84
  end
114
85
 
115
86
  def test_notice_first_scope_push_disabled
116
- @sampler.expects(:disabled).returns(true)
117
- @sampler.expects(:start_builder).never
118
- @sampler.notice_first_scope_push(Time.at(100))
87
+ with_config(:'transaction_tracer.enabled' => false,
88
+ :developer_mode => false) do
89
+ @sampler.expects(:start_builder).never
90
+ @sampler.notice_first_scope_push(Time.at(100))
91
+ end
119
92
  end
120
93
 
121
94
  def test_notice_push_scope_no_builder
@@ -124,17 +97,15 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
124
97
  end
125
98
 
126
99
  def test_notice_push_scope_with_builder
127
- NewRelic::Control.instance.expects(:developer_mode?).returns(false)
128
- builder = mock('builder')
129
- builder.expects(:trace_entry).with('a scope', 100.0)
130
- @sampler.expects(:builder).returns(builder).twice
131
-
132
- @sampler.notice_push_scope('a scope', Time.at(100))
100
+ with_config(:developer_mode => false) do
101
+ builder = mock('builder')
102
+ builder.expects(:trace_entry).with('a scope', 100.0)
103
+ @sampler.expects(:builder).returns(builder).twice
104
+ @sampler.notice_push_scope('a scope', Time.at(100))
105
+ end
133
106
  end
134
107
 
135
108
  def test_notice_push_scope_in_dev_mode
136
- NewRelic::Control.instance.expects(:developer_mode?).returns(true)
137
-
138
109
  builder = mock('builder')
139
110
  builder.expects(:trace_entry).with('a scope', 100.0)
140
111
  @sampler.expects(:builder).returns(builder).twice
@@ -224,22 +195,23 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
224
195
  end
225
196
 
226
197
  def test_store_random_sample_no_random_sampling
227
- @sampler.instance_eval { @random_sampling = false }
228
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
229
- @sampler.store_random_sample(mock('sample'))
230
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
198
+ with_config(:'transaction_tracer.random_sample' => false) do
199
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
200
+ @sampler.store_random_sample(mock('sample'))
201
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
202
+ end
231
203
  end
232
204
 
233
205
  def test_store_random_sample_random_sampling
234
- @sampler.instance_eval { @random_sampling = true }
235
- sample = mock('sample')
236
- assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
237
- @sampler.store_random_sample(sample)
238
- assert_equal(sample, @sampler.instance_variable_get('@random_sample'))
206
+ with_config(:'transaction_tracer.random_sample' => true) do
207
+ sample = mock('sample')
208
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
209
+ @sampler.store_random_sample(sample)
210
+ assert_equal(sample, @sampler.instance_variable_get('@random_sample'))
211
+ end
239
212
  end
240
213
 
241
214
  def test_store_sample_for_developer_mode_in_dev_mode
242
- NewRelic::Control.instance.expects(:developer_mode?).returns(true)
243
215
  sample = mock('sample')
244
216
  @sampler.expects(:truncate_samples)
245
217
  @sampler.store_sample_for_developer_mode(sample)
@@ -247,11 +219,11 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
247
219
  end
248
220
 
249
221
  def test_store_sample_for_developer_mode_no_dev
250
- NewRelic::Control.instance.stubs(:developer_mode?).returns(false)
251
- @sampler.configure!
252
- sample = mock('sample')
253
- @sampler.store_sample_for_developer_mode(sample)
254
- assert_equal([], @sampler.instance_variable_get('@samples'))
222
+ with_config(:developer_mode => false) do
223
+ sample = mock('sample')
224
+ @sampler.store_sample_for_developer_mode(sample)
225
+ assert_equal([], @sampler.instance_variable_get('@samples'))
226
+ end
255
227
  end
256
228
 
257
229
  def test_store_slowest_sample_new_is_slowest
@@ -320,13 +292,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
320
292
  end
321
293
 
322
294
  def test_notice_transaction_disabled
323
- @sampler.expects(:disabled).returns(true)
324
- @sampler.expects(:builder).never # since we're disabled
325
- @sampler.notice_transaction('foo')
295
+ builder = mock('builder')
296
+ builder.expects(:set_transaction_info).never # since we're disabled
297
+ @sampler.stubs(:builder).returns(builder)
298
+ with_config(:'transaction_tracer.enabled' => false,
299
+ :developer_mode => false) do
300
+ @sampler.notice_transaction('foo')
301
+ end
326
302
  end
327
303
 
328
304
  def test_notice_transaction_no_builder
329
- @sampler.expects(:disabled).returns(false)
330
305
  @sampler.expects(:builder).returns(nil).once
331
306
  @sampler.notice_transaction('foo')
332
307
  end
@@ -335,7 +310,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
335
310
  builder = mock('builder')
336
311
  builder.expects(:set_transaction_info).with('a path', 'a uri', {:some => :params})
337
312
  @sampler.expects(:builder).returns(builder).twice
338
- @sampler.expects(:disabled).returns(false)
339
313
  @sampler.notice_transaction('a path', 'a uri', {:some => :params})
340
314
  end
341
315
 
@@ -430,19 +404,21 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
430
404
  end
431
405
 
432
406
  def test_append_backtrace_under_duration
433
- @sampler.instance_eval { @stack_trace_threshold = 2.0 }
434
- segment = mock('segment')
435
- segment.expects(:[]=).with(:backtrace, any_parameters).never
436
- @sampler.append_backtrace(mock('segment'), 1.0)
407
+ with_config(:'transaction_tracer.stack_trace_threshold' => 2.0) do
408
+ segment = mock('segment')
409
+ segment.expects(:[]=).with(:backtrace, any_parameters).never
410
+ @sampler.append_backtrace(mock('segment'), 1.0)
411
+ end
437
412
  end
438
413
 
439
414
  def test_append_backtrace_over_duration
440
- @sampler.instance_eval { @stack_trace_threshold = 2.0 }
441
- segment = mock('segment')
442
- # note the mocha expectation matcher - you can't hardcode a
443
- # backtrace so we match on any string, which should be okay.
444
- segment.expects(:[]=).with(:backtrace, instance_of(String))
445
- @sampler.append_backtrace(segment, 2.5)
415
+ with_config(:'transaction_tracer.stack_trace_threshold' => 2.0) do
416
+ segment = mock('segment')
417
+ # note the mocha expectation matcher - you can't hardcode a
418
+ # backtrace so we match on any string, which should be okay.
419
+ segment.expects(:[]=).with(:backtrace, instance_of(String))
420
+ @sampler.append_backtrace(segment, 2.5)
421
+ end
446
422
  end
447
423
 
448
424
  def test_notice_sql_recording_sql
@@ -463,8 +439,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
463
439
  end
464
440
 
465
441
  def test_harvest_when_disabled
466
- @sampler.expects(:disabled).returns(true)
467
- assert_equal([], @sampler.harvest)
442
+ with_config(:'transaction_tracer.enabled' => false,
443
+ :developer_mode => false) do
444
+ assert_equal([], @sampler.harvest)
445
+ end
468
446
  end
469
447
 
470
448
  def test_harvest_defaults
@@ -475,8 +453,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
475
453
  @last_sample = 'a sample'
476
454
  end
477
455
 
478
- @sampler.expects(:disabled).returns(false)
479
- @sampler.expects(:add_samples_to).with([], 2.0).returns([])
456
+ @sampler.expects(:add_samples_to).with([]).returns([])
480
457
 
481
458
  assert_equal([], @sampler.harvest)
482
459
 
@@ -487,12 +464,12 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
487
464
  end
488
465
 
489
466
  def test_harvest_with_previous_samples
490
- sample = mock('sample')
491
- @sampler.expects(:disabled).returns(false)
492
- @sampler.expects(:add_samples_to).with([sample], 2.0).returns([sample])
493
- @sampler.instance_eval { @segment_limit = 2000 }
494
- sample.expects(:truncate).with(2000)
495
- assert_equal([sample], @sampler.harvest([sample]))
467
+ with_config(:'transaction_tracer.limit_segments' => 2000) do
468
+ sample = mock('sample')
469
+ @sampler.expects(:add_samples_to).with([sample]).returns([sample])
470
+ sample.expects(:truncate).with(2000)
471
+ assert_equal([sample], @sampler.harvest([sample]))
472
+ end
496
473
  end
497
474
 
498
475
  def test_add_random_sample_to_not_random_sampling
@@ -528,16 +505,16 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
528
505
  end
529
506
 
530
507
  def test_add_random_sample_to_activated
531
- @sampler.instance_eval { @random_sampling = true }
532
- sample = mock('sample')
533
- @sampler.instance_eval {
534
- @harvest_count = 3
535
- @sampling_rate = 1
536
- @random_sample = sample
537
- }
538
- result = []
539
- @sampler.add_random_sample_to(result)
540
- assert_equal([sample], result, "should add the random sample to the array")
508
+ with_config(:'transaction_tracer.random_sample' => true, :sample_rate => 1) do
509
+ sample = mock('sample')
510
+ @sampler.instance_eval {
511
+ @harvest_count = 3
512
+ @random_sample = sample
513
+ }
514
+ result = []
515
+ @sampler.add_random_sample_to(result)
516
+ assert_equal([sample], result, "should add the random sample to the array")
517
+ end
541
518
  end
542
519
 
543
520
  def test_add_random_sample_to_sampling_rate_zero
@@ -555,10 +532,9 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
555
532
 
556
533
  def test_add_samples_to_no_data
557
534
  result = []
558
- slow_threshold = 2.0
559
535
  @sampler.instance_eval { @slowest_sample = nil }
560
536
  @sampler.expects(:add_random_sample_to).with([])
561
- assert_equal([], @sampler.add_samples_to(result, slow_threshold))
537
+ assert_equal([], @sampler.add_samples_to(result))
562
538
  end
563
539
 
564
540
  def test_add_samples_to_one_result
@@ -566,30 +542,31 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
566
542
  sample.expects(:duration).returns(1).at_least_once
567
543
  sample.stubs(:force_persist).returns(false)
568
544
  result = [sample]
569
- slow_threshold = 2.0
570
545
  @sampler.instance_eval { @slowest_sample = nil }
571
546
  @sampler.expects(:add_random_sample_to).with([sample])
572
- assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
547
+ assert_equal([sample], @sampler.add_samples_to(result))
573
548
  end
574
549
 
575
550
  def test_add_samples_to_adding_slowest
576
551
  sample = mock('sample')
577
552
  sample.expects(:duration).returns(2.5).at_least_once
578
553
  result = []
579
- slow_threshold = 2.0
580
- @sampler.instance_eval { @slowest_sample = sample }
554
+ @sampler.instance_variable_set(:@slowest_sample, sample)
581
555
  @sampler.expects(:add_random_sample_to).with([sample])
582
- assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
556
+ with_config(:'transaction_tracer.transaction_threshold' => 2) do
557
+ assert_equal([sample], @sampler.add_samples_to(result))
558
+ end
583
559
  end
584
560
 
585
561
  def test_add_samples_to_under_threshold
586
562
  result = []
587
- slow_threshold = 2.0
588
563
  sample = mock('sample')
589
564
  sample.expects(:duration).returns(1.0).at_least_once
590
565
  @sampler.instance_eval { @slowest_sample = sample }
591
566
  @sampler.expects(:add_random_sample_to).with([])
592
- assert_equal([], @sampler.add_samples_to(result, slow_threshold))
567
+ with_config(:'transaction_tracer.transaction_threshold' => 2.0) do
568
+ assert_equal([], @sampler.add_samples_to(result))
569
+ end
593
570
  end
594
571
 
595
572
  def test_add_samples_to_two_sample_enter_one_sample_leave
@@ -599,69 +576,72 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
599
576
  faster_sample.expects(:duration).returns(5.0).at_least_once
600
577
  faster_sample.stubs(:force_persist).returns(false)
601
578
  result = [faster_sample]
602
- slow_threshold = 2.0
603
579
  @sampler.instance_eval { @slowest_sample = slower_sample }
604
580
  @sampler.expects(:add_random_sample_to).with([slower_sample])
605
- assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
581
+ assert_equal([slower_sample], @sampler.add_samples_to(result))
606
582
  end
607
583
 
608
584
  def test_add_samples_to_keep_older_slower_sample
609
585
  slower_sample = mock('slower')
610
586
  slower_sample.expects(:duration).returns(10.0).at_least_once
611
587
  slower_sample.stubs(:force_persist).returns(false)
612
-
588
+
613
589
  faster_sample = mock('faster')
614
590
  faster_sample.expects(:duration).returns(5.0).at_least_once
615
591
  result = [slower_sample]
616
- slow_threshold = 2.0
617
592
  @sampler.instance_eval { @slowest_sample = faster_sample }
618
593
  @sampler.expects(:add_random_sample_to).with([slower_sample])
619
- assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
594
+ assert_equal([slower_sample], @sampler.add_samples_to(result))
620
595
  end
621
-
596
+
622
597
  def test_keep_force_persist
623
598
  sample1 = mock('regular')
624
599
  sample1.stubs(:duration).returns(10)
625
600
  sample1.stubs(:force_persist).returns(false)
626
-
601
+
627
602
  sample2 = mock('force_persist')
628
603
  sample2.stubs(:duration).returns(1)
629
604
  sample2.stubs(:force_persist).returns(true)
630
-
631
- result = @sampler.add_samples_to([sample1,sample2], 2.0)
632
-
605
+
606
+ result = @sampler.add_samples_to([sample1,sample2])
607
+
633
608
  assert_equal 2, result.length
634
609
  assert_equal sample1, result[0]
635
610
  assert_equal sample2, result[1]
636
- end
611
+ end
637
612
 
638
613
  def test_start_builder_default
639
614
  Thread.current[:record_tt] = true
640
- @sampler.expects(:disabled).returns(false)
641
615
  NewRelic::Agent.expects(:is_execution_traced?).returns(true)
642
616
  @sampler.send(:start_builder)
643
- assert(Thread.current[:transaction_sample_builder].is_a?(NewRelic::Agent::TransactionSampleBuilder), "should set up a new builder by default")
617
+ assert(Thread.current[:transaction_sample_builder] \
618
+ .is_a?(NewRelic::Agent::TransactionSampleBuilder),
619
+ "should set up a new builder by default")
644
620
  end
645
621
 
646
622
  def test_start_builder_disabled
647
623
  Thread.current[:transaction_sample_builder] = 'not nil.'
648
- @sampler.expects(:disabled).returns(true)
649
- @sampler.send(:start_builder)
650
- assert_equal(nil, Thread.current[:transaction_sample_builder], "should clear the transaction builder when disabled")
624
+ with_config(:'transaction_tracer.enabled' => false,
625
+ :developer_mode => false) do
626
+ @sampler.send(:start_builder)
627
+ assert_equal(nil, Thread.current[:transaction_sample_builder],
628
+ "should clear the transaction builder when disabled")
629
+ end
651
630
  end
652
631
 
653
632
  def test_start_builder_dont_replace_existing_builder
654
633
  fake_builder = mock('transaction sample builder')
655
634
  Thread.current[:transaction_sample_builder] = fake_builder
656
- @sampler.expects(:disabled).returns(false)
657
635
  @sampler.send(:start_builder)
658
- assert_equal(fake_builder, Thread.current[:transaction_sample_builder], "should not overwrite an existing transaction sample builder")
636
+ assert_equal(fake_builder, Thread.current[:transaction_sample_builder],
637
+ "should not overwrite an existing transaction sample builder")
659
638
  Thread.current[:transaction_sample_builder] = nil
660
639
  end
661
640
 
662
641
  def test_builder
663
642
  Thread.current[:transaction_sample_builder] = 'shamalamadingdong, brother.'
664
- assert_equal('shamalamadingdong, brother.', @sampler.send(:builder), 'should return the value from the thread local variable')
643
+ assert_equal('shamalamadingdong, brother.', @sampler.send(:builder),
644
+ 'should return the value from the thread local variable')
665
645
  Thread.current[:transaction_sample_builder] = nil
666
646
  end
667
647
 
@@ -703,9 +683,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
703
683
 
704
684
  @sampler.notice_pop_scope "a"
705
685
  @sampler.notice_scope_empty
706
- sample = @sampler.harvest([],0.0).first
707
- assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
708
-
686
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
687
+ sample = @sampler.harvest([]).first
688
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
689
+ end
709
690
  end
710
691
 
711
692
  def test_sample__gc_stats
@@ -730,8 +711,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
730
711
  @sampler.notice_pop_scope "a"
731
712
  @sampler.notice_scope_empty
732
713
 
733
- sample = @sampler.harvest([],0.0).first
734
- assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
714
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
715
+ sample = @sampler.harvest([]).first
716
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
717
+ end
735
718
  ensure
736
719
  MockGCStats.mock_values = []
737
720
  end
@@ -747,31 +730,38 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
747
730
  # sample traces, for example. It's unfortunate, but we can't
748
731
  # reliably turn off GC on all versions of ruby under test
749
732
  def test_harvest_slowest
750
- run_sample_trace
751
- run_sample_trace
752
- run_sample_trace { sleep 0.1 }
753
- run_sample_trace
754
- run_sample_trace
755
-
756
- slowest = @sampler.harvest(nil, 0)[0]
757
- assert((slowest.duration >= 0.09), "expected sample duration >= 0.09, but was: #{slowest.duration.inspect}")
758
- # this assert is here to make sure the test remains valid
759
- assert((slowest.duration <= 0.15), "expected sample duration <= 0.15, but was: #{slowest.duration.inspect}")
760
-
761
-
762
- run_sample_trace { sleep 0.0001 }
763
- not_as_slow = @sampler.harvest(slowest, 0)[0]
764
- assert((not_as_slow == slowest), "Should re-harvest the same transaction since it should be slower than the new transaction - expected #{slowest.inspect} but got #{not_as_slow.inspect}")
765
-
766
- run_sample_trace { sleep 0.16 }
767
- new_slowest = @sampler.harvest(slowest, 0)[0]
768
- assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
769
- assert((new_slowest.duration >= 0.15), "Slowest duration must be >= 0.15, but was: #{new_slowest.duration.inspect}")
733
+ run_sample_trace(0,0.1)
734
+ run_sample_trace(0,0.1)
735
+ # two second duration
736
+ run_sample_trace(0,2)
737
+ run_sample_trace(0,0.1)
738
+ run_sample_trace(0,0.1)
739
+
740
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
741
+
742
+ slowest = @sampler.harvest(nil)[0]
743
+ first_duration = slowest.duration
744
+ assert((first_duration.round >= 2),
745
+ "expected sample duration = 2, but was: #{slowest.duration.inspect}")
746
+
747
+ # 1 second duration
748
+ run_sample_trace(0,1)
749
+ not_as_slow = @sampler.harvest(slowest)[0]
750
+ assert((not_as_slow == slowest), "Should re-harvest the same transaction since it should be slower than the new transaction - expected #{slowest.inspect} but got #{not_as_slow.inspect}")
751
+
752
+ run_sample_trace(0,10)
753
+
754
+ new_slowest = @sampler.harvest(slowest)[0]
755
+ assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
756
+ assert_equal(new_slowest.duration.round, 10, "Slowest duration must be = 10, but was: #{new_slowest.duration.inspect}")
757
+ end
770
758
  end
771
759
 
772
760
  def test_prepare_to_send
773
761
  run_sample_trace { sleep 0.002 }
774
- sample = @sampler.harvest(nil, 0)[0]
762
+ sample = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
763
+ @sampler.harvest(nil)[0]
764
+ end
775
765
 
776
766
  ready_to_send = sample.prepare_to_send
777
767
  assert sample.duration == ready_to_send.duration
@@ -816,8 +806,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
816
806
  @sampler.notice_scope_empty
817
807
 
818
808
  assert_equal 0, @sampler.scope_depth
819
- sample = @sampler.harvest(nil, 0.0).first
820
- assert_equal "ROOT{a}", sample.to_s_compact
809
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
810
+ sample = @sampler.harvest(nil).first
811
+ assert_equal "ROOT{a}", sample.to_s_compact
812
+ end
821
813
  end
822
814
 
823
815
  def test_double_scope_stack_empty
@@ -830,7 +822,9 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
830
822
  @sampler.notice_scope_empty
831
823
  @sampler.notice_scope_empty
832
824
 
833
- assert_not_nil @sampler.harvest(nil, 0)[0]
825
+ with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
826
+ assert_not_nil @sampler.harvest(nil)[0]
827
+ end
834
828
  end
835
829
 
836
830
 
@@ -847,39 +841,36 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
847
841
  end
848
842
 
849
843
  def test_stack_trace__sql
850
- @sampler.stack_trace_threshold = 0
851
-
852
- @sampler.notice_first_scope_push Time.now.to_f
853
-
854
- @sampler.notice_sql("test", nil, 1)
855
-
856
- segment = @sampler.send(:builder).current_segment
844
+ with_config(:'transaction_tracer.stack_trace_threshold' => 0) do
845
+ @sampler.notice_first_scope_push Time.now.to_f
846
+ @sampler.notice_sql("test", nil, 1)
847
+ segment = @sampler.send(:builder).current_segment
857
848
 
858
- assert segment[:sql]
859
- assert segment[:backtrace]
849
+ assert segment[:sql]
850
+ assert segment[:backtrace]
851
+ end
860
852
  end
861
-
853
+
862
854
  def test_stack_trace__scope
863
- @sampler.stack_trace_threshold = 0
864
- t = Time.now
865
- @sampler.notice_first_scope_push t.to_f
866
- @sampler.notice_push_scope 'Bill', (t+1).to_f
855
+ with_config(:'transaction_tracer.stack_trace_threshold' => 0) do
856
+ t = Time.now
857
+ @sampler.notice_first_scope_push t.to_f
858
+ @sampler.notice_push_scope 'Bill', (t+1).to_f
867
859
 
868
- segment = @sampler.send(:builder).current_segment
869
- assert segment[:backtrace]
860
+ segment = @sampler.send(:builder).current_segment
861
+ assert segment[:backtrace]
862
+ end
870
863
  end
871
864
 
872
865
  def test_nil_stacktrace
873
- @sampler.stack_trace_threshold = 2
874
-
875
- @sampler.notice_first_scope_push Time.now.to_f
876
-
877
- @sampler.notice_sql("test", nil, 1)
878
-
879
- segment = @sampler.send(:builder).current_segment
866
+ with_config(:'transaction_tracer.stack_trace_threshold' => 2) do
867
+ @sampler.notice_first_scope_push Time.now.to_f
868
+ @sampler.notice_sql("test", nil, 1)
869
+ segment = @sampler.send(:builder).current_segment
880
870
 
881
- assert segment[:sql]
882
- assert_nil segment[:backtrace]
871
+ assert segment[:sql]
872
+ assert_nil segment[:backtrace]
873
+ end
883
874
  end
884
875
 
885
876
  def test_big_sql
@@ -917,40 +908,38 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
917
908
 
918
909
  def test_param_capture
919
910
  [true, false].each do |capture|
920
- NewRelic::Control.instance.stubs(:capture_params).returns(capture)
921
- @sampler.notice_first_scope_push Time.now.to_f
922
- @sampler.notice_transaction('/path', nil, {:param => 'hi'})
923
- @sampler.notice_scope_empty
911
+ with_config(:capture_params => capture) do
912
+ @sampler.notice_first_scope_push Time.now.to_f
913
+ @sampler.notice_transaction('/path', nil, {:param => 'hi'})
914
+ @sampler.notice_scope_empty
924
915
 
925
- tt = @sampler.harvest(nil,0)[0]
916
+ tt = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
917
+ @sampler.harvest(nil)[0]
918
+ end
926
919
 
927
- assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
920
+ assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
921
+ end
928
922
  end
929
923
  end
930
-
924
+
931
925
  def test_should_not_collect_segments_beyond_limit
932
- NewRelic::Control.instance.merge_options('transaction_tracer' => {
933
- 'limit_segments' => 3,
934
- })
935
- @sampler.configure!
936
- run_sample_trace do
937
- @sampler.notice_push_scope 'a1'
938
- @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'hallah'", nil, 0)
939
- @sampler.notice_push_scope 'a11'
940
- @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'semolina'", nil, 0)
941
- @sampler.notice_pop_scope "a11"
942
- @sampler.notice_pop_scope "a1"
943
- end
944
- assert_equal 3, @sampler.samples[0].count_segments
945
- NewRelic::Control.instance.merge_options('transaction_tracer' => {
946
- 'limit_segments' => 4000,
947
- })
948
- end
949
-
926
+ with_config(:'transaction_tracer.limit_segments' => 3) do
927
+ run_sample_trace do
928
+ @sampler.notice_push_scope 'a1'
929
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'hallah'", nil, 0)
930
+ @sampler.notice_push_scope 'a11'
931
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'semolina'", nil, 0)
932
+ @sampler.notice_pop_scope "a11"
933
+ @sampler.notice_pop_scope "a1"
934
+ end
935
+ assert_equal 3, @sampler.samples[0].count_segments
936
+ end
937
+ end
938
+
950
939
  private
951
-
952
- def run_sample_trace
953
- @sampler.notice_first_scope_push Time.now.to_f
940
+
941
+ def run_sample_trace(start = Time.now.to_f, stop = nil)
942
+ @sampler.notice_first_scope_push start
954
943
  @sampler.notice_transaction '/path', nil, {}
955
944
  @sampler.notice_push_scope "a"
956
945
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
@@ -962,6 +951,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
962
951
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
963
952
  @sampler.notice_pop_scope "ac"
964
953
  @sampler.notice_pop_scope "a"
965
- @sampler.notice_scope_empty
954
+ @sampler.notice_scope_empty stop || Time.now.to_f
966
955
  end
967
956
  end