ghazel-newrelic_rpm 3.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/CHANGELOG +525 -0
  2. data/LICENSE +37 -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 +423 -0
  14. data/lib/new_relic/agent/agent.rb +1040 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +61 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +83 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/error_collector.rb +203 -0
  20. data/lib/new_relic/agent/instrumentation.rb +9 -0
  21. data/lib/new_relic/agent/instrumentation/active_merchant.rb +21 -0
  22. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +62 -0
  23. data/lib/new_relic/agent/instrumentation/authlogic.rb +13 -0
  24. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +413 -0
  25. data/lib/new_relic/agent/instrumentation/data_mapper.rb +232 -0
  26. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +44 -0
  27. data/lib/new_relic/agent/instrumentation/memcache.rb +56 -0
  28. data/lib/new_relic/agent/instrumentation/merb/controller.rb +36 -0
  29. data/lib/new_relic/agent/instrumentation/merb/errors.rb +23 -0
  30. data/lib/new_relic/agent/instrumentation/metric_frame.rb +326 -0
  31. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +84 -0
  32. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  33. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +34 -0
  34. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  35. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  36. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +83 -0
  37. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +36 -0
  38. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +108 -0
  39. data/lib/new_relic/agent/instrumentation/rails/errors.rb +36 -0
  40. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +60 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +112 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +31 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +54 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +23 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +16 -0
  46. data/lib/new_relic/agent/method_tracer.rb +422 -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 +144 -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/stats_engine.rb +24 -0
  54. data/lib/new_relic/agent/stats_engine/metric_stats.rb +182 -0
  55. data/lib/new_relic/agent/stats_engine/samplers.rb +89 -0
  56. data/lib/new_relic/agent/stats_engine/transactions.rb +184 -0
  57. data/lib/new_relic/agent/transaction_sample_builder.rb +101 -0
  58. data/lib/new_relic/agent/transaction_sampler.rb +378 -0
  59. data/lib/new_relic/agent/worker_loop.rb +81 -0
  60. data/lib/new_relic/collection_helper.rb +69 -0
  61. data/lib/new_relic/command.rb +85 -0
  62. data/lib/new_relic/commands/deployments.rb +105 -0
  63. data/lib/new_relic/commands/install.rb +91 -0
  64. data/lib/new_relic/control.rb +48 -0
  65. data/lib/new_relic/control/class_methods.rb +47 -0
  66. data/lib/new_relic/control/configuration.rb +157 -0
  67. data/lib/new_relic/control/frameworks/external.rb +16 -0
  68. data/lib/new_relic/control/frameworks/merb.rb +30 -0
  69. data/lib/new_relic/control/frameworks/rails.rb +151 -0
  70. data/lib/new_relic/control/frameworks/rails3.rb +70 -0
  71. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  72. data/lib/new_relic/control/frameworks/sinatra.rb +23 -0
  73. data/lib/new_relic/control/instance_methods.rb +154 -0
  74. data/lib/new_relic/control/instrumentation.rb +81 -0
  75. data/lib/new_relic/control/logging_methods.rb +88 -0
  76. data/lib/new_relic/control/profiling.rb +25 -0
  77. data/lib/new_relic/control/server_methods.rb +104 -0
  78. data/lib/new_relic/data_serialization.rb +84 -0
  79. data/lib/new_relic/delayed_job_injection.rb +38 -0
  80. data/lib/new_relic/histogram.rb +91 -0
  81. data/lib/new_relic/local_environment.rb +391 -0
  82. data/lib/new_relic/merbtasks.rb +6 -0
  83. data/lib/new_relic/metric_data.rb +46 -0
  84. data/lib/new_relic/metric_spec.rb +73 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/browser_monitoring.rb +68 -0
  88. data/lib/new_relic/rack/developer_mode.rb +267 -0
  89. data/lib/new_relic/rack/metric_app.rb +65 -0
  90. data/lib/new_relic/rack/mongrel_rpm.ru +28 -0
  91. data/lib/new_relic/rack/newrelic.yml +27 -0
  92. data/lib/new_relic/rack_app.rb +6 -0
  93. data/lib/new_relic/recipes.rb +73 -0
  94. data/lib/new_relic/stats.rb +378 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +76 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +47 -0
  98. data/lib/new_relic/transaction_sample.rb +272 -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 +251 -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 +330 -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/new_relic/agent/agent/connect_test.rb +316 -0
  116. data/test/new_relic/agent/agent/start_test.rb +340 -0
  117. data/test/new_relic/agent/agent/start_worker_thread_test.rb +155 -0
  118. data/test/new_relic/agent/agent_test.rb +173 -0
  119. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  120. data/test/new_relic/agent/agent_test_controller_test.rb +366 -0
  121. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  122. data/test/new_relic/agent/beacon_configuration_test.rb +104 -0
  123. data/test/new_relic/agent/browser_monitoring_test.rb +274 -0
  124. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  125. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  126. data/test/new_relic/agent/error_collector_test.rb +168 -0
  127. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +522 -0
  128. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +36 -0
  129. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  130. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +179 -0
  131. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  132. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  133. data/test/new_relic/agent/instrumentation/queue_time_test.rb +387 -0
  134. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  135. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  136. data/test/new_relic/agent/memcache_instrumentation_test.rb +107 -0
  137. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  138. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  139. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  140. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  141. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  142. data/test/new_relic/agent/sampler_test.rb +19 -0
  143. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  144. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  145. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +81 -0
  146. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  147. data/test/new_relic/agent/stats_engine_test.rb +185 -0
  148. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  149. data/test/new_relic/agent/transaction_sampler_test.rb +945 -0
  150. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  151. data/test/new_relic/collection_helper_test.rb +127 -0
  152. data/test/new_relic/command/deployments_test.rb +68 -0
  153. data/test/new_relic/control/class_methods_test.rb +62 -0
  154. data/test/new_relic/control/logging_methods_test.rb +157 -0
  155. data/test/new_relic/control_test.rb +195 -0
  156. data/test/new_relic/data_serialization_test.rb +70 -0
  157. data/test/new_relic/local_environment_test.rb +72 -0
  158. data/test/new_relic/metric_data_test.rb +125 -0
  159. data/test/new_relic/metric_spec_test.rb +95 -0
  160. data/test/new_relic/rack/all_test.rb +11 -0
  161. data/test/new_relic/rack/browser_monitoring_test.rb +84 -0
  162. data/test/new_relic/rack/developer_mode_test.rb +43 -0
  163. data/test/new_relic/stats_test.rb +426 -0
  164. data/test/new_relic/transaction_analysis/segment_summary_test.rb +77 -0
  165. data/test/new_relic/transaction_analysis_test.rb +121 -0
  166. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  167. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  168. data/test/new_relic/transaction_sample/segment_test.rb +454 -0
  169. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  170. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  171. data/test/new_relic/transaction_sample_test.rb +170 -0
  172. data/test/new_relic/version_number_test.rb +89 -0
  173. data/test/test_contexts.rb +29 -0
  174. data/test/test_helper.rb +144 -0
  175. data/ui/helpers/developer_mode_helper.rb +351 -0
  176. data/ui/helpers/google_pie_chart.rb +49 -0
  177. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  178. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  179. data/ui/views/newrelic/_sample.rhtml +20 -0
  180. data/ui/views/newrelic/_segment.rhtml +28 -0
  181. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  182. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  183. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  184. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  185. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  186. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  187. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  188. data/ui/views/newrelic/_table.rhtml +12 -0
  189. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  190. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  191. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  192. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  193. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  194. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  195. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  196. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  197. data/ui/views/newrelic/file/images/textmate.png +0 -0
  198. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  199. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  200. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  201. data/ui/views/newrelic/index.rhtml +71 -0
  202. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  203. data/ui/views/newrelic/show_sample.rhtml +80 -0
  204. data/ui/views/newrelic/show_source.rhtml +3 -0
  205. data/ui/views/newrelic/threads.rhtml +53 -0
  206. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +19 -0
  207. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  208. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +57 -0
  209. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  210. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  211. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
  212. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  213. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  214. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  215. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  216. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  217. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  218. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  219. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  220. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +122 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +66 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  255. metadata +399 -0
@@ -0,0 +1,945 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
4
+
5
+ module MockGCStats
6
+
7
+ def time
8
+ return 0 if @@values.empty?
9
+ raise "too many calls" if @@index >= @@values.size
10
+ @@curtime ||= 0
11
+ @@curtime += (@@values[@@index] * 1e09).to_i
12
+ @@index += 1
13
+ @@curtime
14
+ end
15
+
16
+ def self.mock_values= array
17
+ @@values = array
18
+ @@index = 0
19
+ end
20
+
21
+ end
22
+
23
+ def setup
24
+ Thread::current[:record_sql] = nil
25
+ agent = NewRelic::Agent.instance
26
+ stats_engine = NewRelic::Agent::StatsEngine.new
27
+ agent.stubs(:stats_engine).returns(stats_engine)
28
+ @sampler = NewRelic::Agent::TransactionSampler.new
29
+ stats_engine.transaction_sampler = @sampler
30
+ end
31
+
32
+ def teardown
33
+ super
34
+ Thread.current[:transaction_sample_builder] = nil
35
+ end
36
+
37
+ def test_initialize
38
+ defaults = {
39
+ :samples => [],
40
+ :harvest_count => 0,
41
+ :max_samples => 100,
42
+ :random_sample => nil,
43
+ }
44
+ defaults.each do |variable, default_value|
45
+ assert_equal(default_value, @sampler.instance_variable_get('@' + variable.to_s))
46
+ end
47
+
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
+ lock = @sampler.instance_variable_get('@samples_lock')
57
+ assert(lock.is_a?(Mutex), "Samples lock should be a mutex, is: #{lock.inspect}")
58
+ end
59
+
60
+ def test_current_sample_id_default
61
+ builder = mock('builder')
62
+ builder.expects(:sample_id).returns(11111)
63
+ @sampler.expects(:builder).returns(builder)
64
+ assert_equal(11111, @sampler.current_sample_id)
65
+ end
66
+
67
+ def test_current_sample_id_no_builder
68
+ @sampler.expects(:builder).returns(nil)
69
+ assert_equal(nil, @sampler.current_sample_id)
70
+ end
71
+
72
+ def test_enable
73
+ assert_equal(nil, @sampler.instance_variable_get('@disabled'))
74
+ @sampler.enable
75
+ assert_equal(false, @sampler.instance_variable_get('@disabled'))
76
+ assert_equal(@sampler, NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler'))
77
+ end
78
+
79
+ def test_disable
80
+ assert_equal(nil, @sampler.instance_variable_get('@disabled'))
81
+ @sampler.disable
82
+ assert_equal(true, @sampler.instance_variable_get('@disabled'))
83
+ assert_equal(nil, NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler'))
84
+ end
85
+
86
+ def test_sampling_rate_equals_default
87
+ @sampler.sampling_rate = 1
88
+ assert_equal(1, @sampler.instance_variable_get('@sampling_rate'))
89
+ # rand(1) is always zero, so we can be sure here
90
+ assert_equal(0, @sampler.instance_variable_get('@harvest_count'))
91
+ end
92
+
93
+ def test_sampling_rate_equals_with_a_float
94
+ @sampler.sampling_rate = 5.5
95
+ assert_equal(5, @sampler.instance_variable_get('@sampling_rate'))
96
+ harvest_count = @sampler.instance_variable_get('@harvest_count')
97
+ assert((0..4).include?(harvest_count), "should be in the range 0..4")
98
+ end
99
+
100
+ def test_notice_first_scope_push_default
101
+ @sampler.expects(:disabled).returns(false)
102
+ @sampler.expects(:start_builder).with(100.0)
103
+ @sampler.notice_first_scope_push(Time.at(100))
104
+ end
105
+
106
+ def test_notice_first_scope_push_disabled
107
+ @sampler.expects(:disabled).returns(true)
108
+ @sampler.expects(:start_builder).never
109
+ @sampler.notice_first_scope_push(Time.at(100))
110
+ end
111
+
112
+ def test_notice_push_scope_no_builder
113
+ @sampler.expects(:builder)
114
+ assert_equal(nil, @sampler.notice_push_scope('a scope'))
115
+ end
116
+
117
+ def test_notice_push_scope_with_builder
118
+ NewRelic::Control.instance.expects(:developer_mode?).returns(false)
119
+ builder = mock('builder')
120
+ builder.expects(:trace_entry).with('a scope', 100.0)
121
+ @sampler.expects(:builder).returns(builder).twice
122
+
123
+ @sampler.notice_push_scope('a scope', Time.at(100))
124
+ end
125
+
126
+ def test_notice_push_scope_in_dev_mode
127
+ NewRelic::Control.instance.expects(:developer_mode?).returns(true)
128
+
129
+ builder = mock('builder')
130
+ builder.expects(:trace_entry).with('a scope', 100.0)
131
+ @sampler.expects(:builder).returns(builder).twice
132
+ @sampler.expects(:capture_segment_trace)
133
+
134
+ @sampler.notice_push_scope('a scope', Time.at(100))
135
+ end
136
+
137
+ def test_scope_depth_no_builder
138
+ @sampler.expects(:builder).returns(nil)
139
+ assert_equal(0, @sampler.scope_depth, "should default to zero with no builder")
140
+ end
141
+
142
+ def test_scope_depth_with_builder
143
+ builder = mock('builder')
144
+ builder.expects(:scope_depth).returns('scope_depth')
145
+ @sampler.expects(:builder).returns(builder).twice
146
+
147
+ assert_equal('scope_depth', @sampler.scope_depth, "should delegate scope depth to the builder")
148
+ end
149
+
150
+ def test_notice_pop_scope_no_builder
151
+ @sampler.expects(:builder).returns(nil)
152
+ assert_equal(nil, @sampler.notice_pop_scope('a scope', Time.at(100)))
153
+ end
154
+
155
+ def test_notice_pop_scope_with_frozen_sample
156
+ builder = mock('builder')
157
+ sample = mock('sample')
158
+ builder.expects(:sample).returns(sample)
159
+ sample.expects(:frozen?).returns(true)
160
+ @sampler.expects(:builder).returns(builder).twice
161
+
162
+ assert_raise(RuntimeError) do
163
+ @sampler.notice_pop_scope('a scope', Time.at(100))
164
+ end
165
+ end
166
+
167
+ def test_notice_pop_scope_builder_delegation
168
+ builder = mock('builder')
169
+ builder.expects(:trace_exit).with('a scope', 100.0)
170
+ sample = mock('sample')
171
+ builder.expects(:sample).returns(sample)
172
+ sample.expects(:frozen?).returns(false)
173
+ @sampler.expects(:builder).returns(builder).times(3)
174
+
175
+ @sampler.notice_pop_scope('a scope', Time.at(100))
176
+ end
177
+
178
+ def test_notice_scope_empty_no_builder
179
+ @sampler.expects(:builder).returns(nil)
180
+ assert_equal(nil, @sampler.notice_scope_empty)
181
+ end
182
+
183
+ def test_notice_scope_empty_ignored_transaction
184
+ builder = mock('builder')
185
+ # the builder should be cached, so only called once
186
+ @sampler.expects(:builder).returns(builder).once
187
+
188
+ builder.expects(:finish_trace).with(100.0)
189
+
190
+ @sampler.expects(:clear_builder)
191
+
192
+ builder.expects(:ignored?).returns(true)
193
+
194
+ assert_equal(nil, @sampler.notice_scope_empty(Time.at(100)))
195
+ end
196
+
197
+ def test_notice_scope_empty_with_builder
198
+ builder = mock('builder')
199
+ # the builder should be cached, so only called once
200
+ @sampler.expects(:builder).returns(builder).once
201
+
202
+
203
+ builder.expects(:finish_trace).with(100.0)
204
+ @sampler.expects(:clear_builder)
205
+
206
+ builder.expects(:ignored?).returns(false)
207
+
208
+ sample = mock('sample')
209
+ builder.expects(:sample).returns(sample)
210
+ @sampler.expects(:store_sample).with(sample)
211
+
212
+ @sampler.notice_scope_empty(Time.at(100))
213
+
214
+ assert_equal(sample, @sampler.instance_variable_get('@last_sample'))
215
+ end
216
+
217
+ def test_store_random_sample_no_random_sampling
218
+ @sampler.instance_eval { @random_sampling = false }
219
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
220
+ @sampler.store_random_sample(mock('sample'))
221
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
222
+ end
223
+
224
+ def test_store_random_sample_random_sampling
225
+ @sampler.instance_eval { @random_sampling = true }
226
+ sample = mock('sample')
227
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
228
+ @sampler.store_random_sample(sample)
229
+ assert_equal(sample, @sampler.instance_variable_get('@random_sample'))
230
+ end
231
+
232
+ def test_store_sample_for_developer_mode_in_dev_mode
233
+ NewRelic::Control.instance.expects(:developer_mode?).returns(true)
234
+ sample = mock('sample')
235
+ @sampler.expects(:truncate_samples)
236
+ @sampler.store_sample_for_developer_mode(sample)
237
+ assert_equal([sample], @sampler.instance_variable_get('@samples'))
238
+ end
239
+
240
+ def test_store_sample_for_developer_mode_no_dev
241
+ NewRelic::Control.instance.expects(:developer_mode?).returns(false)
242
+ sample = mock('sample')
243
+ @sampler.store_sample_for_developer_mode(sample)
244
+ assert_equal([], @sampler.instance_variable_get('@samples'))
245
+ end
246
+
247
+ def test_store_slowest_sample_new_is_slowest
248
+ old_sample = mock('old_sample')
249
+ new_sample = mock('new_sample')
250
+ @sampler.instance_eval { @slowest_sample = old_sample }
251
+ @sampler.expects(:slowest_sample?).with(old_sample, new_sample).returns(true)
252
+
253
+ @sampler.store_slowest_sample(new_sample)
254
+
255
+ assert_equal(new_sample, @sampler.instance_variable_get('@slowest_sample'))
256
+ end
257
+
258
+
259
+ def test_store_slowest_sample_not_slowest
260
+ old_sample = mock('old_sample')
261
+ new_sample = mock('new_sample')
262
+ @sampler.instance_eval { @slowest_sample = old_sample }
263
+ @sampler.expects(:slowest_sample?).with(old_sample, new_sample).returns(false)
264
+
265
+ @sampler.store_slowest_sample(new_sample)
266
+
267
+ assert_equal(old_sample, @sampler.instance_variable_get('@slowest_sample'))
268
+ end
269
+
270
+ def test_slowest_sample_no_sample
271
+ old_sample = nil
272
+ new_sample = mock('new_sample')
273
+ assert_equal(true, @sampler.slowest_sample?(old_sample, new_sample))
274
+ end
275
+
276
+ def test_slowest_sample_faster_sample
277
+ old_sample = mock('old_sample')
278
+ new_sample = mock('new_sample')
279
+ old_sample.expects(:duration).returns(1.0)
280
+ new_sample.expects(:duration).returns(0.5)
281
+ assert_equal(false, @sampler.slowest_sample?(old_sample, new_sample))
282
+ end
283
+
284
+ def test_slowest_sample_slower_sample
285
+ old_sample = mock('old_sample')
286
+ new_sample = mock('new_sample')
287
+ old_sample.expects(:duration).returns(0.5)
288
+ new_sample.expects(:duration).returns(1.0)
289
+ assert_equal(true, @sampler.slowest_sample?(old_sample, new_sample))
290
+ end
291
+
292
+ def test_truncate_samples_no_samples
293
+ @sampler.instance_eval { @max_samples = 10 }
294
+ @sampler.instance_eval { @samples = [] }
295
+ @sampler.truncate_samples
296
+ assert_equal([], @sampler.instance_variable_get('@samples'))
297
+ end
298
+
299
+ def test_truncate_samples_equal_samples
300
+ @sampler.instance_eval { @max_samples = 2 }
301
+ @sampler.instance_eval { @samples = [1, 2] }
302
+ @sampler.truncate_samples
303
+ assert_equal([1, 2], @sampler.instance_variable_get('@samples'))
304
+ end
305
+
306
+ def test_truncate_samples_extra_samples
307
+ @sampler.instance_eval { @max_samples = 2 }
308
+ @sampler.instance_eval { @samples = [1, 2, 3] }
309
+ @sampler.truncate_samples
310
+ assert_equal([2, 3], @sampler.instance_variable_get('@samples'))
311
+ end
312
+
313
+ def test_notice_transaction_disabled
314
+ @sampler.expects(:disabled).returns(true)
315
+ @sampler.expects(:builder).never # since we're disabled
316
+ @sampler.notice_transaction('foo')
317
+ end
318
+
319
+ def test_notice_transaction_no_builder
320
+ @sampler.expects(:disabled).returns(false)
321
+ @sampler.expects(:builder).returns(nil).once
322
+ @sampler.notice_transaction('foo')
323
+ end
324
+
325
+ def test_notice_transaction_with_builder
326
+ builder = mock('builder')
327
+ builder.expects(:set_transaction_info).with('a path', 'a uri', {:some => :params})
328
+ @sampler.expects(:builder).returns(builder).twice
329
+ @sampler.expects(:disabled).returns(false)
330
+ @sampler.notice_transaction('a path', 'a uri', {:some => :params})
331
+ end
332
+
333
+ def test_ignore_transaction_no_builder
334
+ @sampler.expects(:builder).returns(nil).once
335
+ @sampler.ignore_transaction
336
+ end
337
+
338
+ def test_ignore_transaction_with_builder
339
+ builder = mock('builder')
340
+ builder.expects(:ignore_transaction)
341
+ @sampler.expects(:builder).returns(builder).twice
342
+ @sampler.ignore_transaction
343
+ end
344
+
345
+ def test_notice_profile_no_builder
346
+ @sampler.expects(:builder).returns(nil).once
347
+ @sampler.notice_profile(nil)
348
+ end
349
+
350
+ def test_notice_profile_with_builder
351
+ profile = mock('profile')
352
+ builder = mock('builder')
353
+ @sampler.expects(:builder).returns(builder).twice
354
+ builder.expects(:set_profile).with(profile)
355
+
356
+ @sampler.notice_profile(profile)
357
+ end
358
+
359
+ def test_notice_transaction_cpu_time_no_builder
360
+ @sampler.expects(:builder).returns(nil).once
361
+ @sampler.notice_transaction_cpu_time(0.0)
362
+ end
363
+
364
+ def test_notice_transaction_cpu_time_with_builder
365
+ cpu_time = mock('cpu_time')
366
+ builder = mock('builder')
367
+ @sampler.expects(:builder).returns(builder).twice
368
+ builder.expects(:set_transaction_cpu_time).with(cpu_time)
369
+
370
+ @sampler.notice_transaction_cpu_time(cpu_time)
371
+ end
372
+
373
+ def test_notice_extra_data_no_builder
374
+ @sampler.expects(:builder).returns(nil).once
375
+ @sampler.send(:notice_extra_data, nil, nil, nil)
376
+ end
377
+
378
+ def test_notice_extra_data_no_segment
379
+ builder = mock('builder')
380
+ @sampler.expects(:builder).returns(builder).twice
381
+ builder.expects(:current_segment).returns(nil)
382
+ @sampler.send(:notice_extra_data, nil, nil, nil)
383
+ end
384
+
385
+ def test_notice_extra_data_with_segment_no_old_message_no_config_key
386
+ key = :a_key
387
+ builder = mock('builder')
388
+ segment = mock('segment')
389
+ @sampler.expects(:builder).returns(builder).twice
390
+ builder.expects(:current_segment).returns(segment)
391
+ segment.expects(:[]).with(key).returns(nil)
392
+ @sampler.expects(:append_new_message).with(nil, 'a message').returns('a message')
393
+ @sampler.expects(:truncate_message).with('a message').returns('truncated_message')
394
+ segment.expects(:[]=).with(key, 'truncated_message')
395
+ @sampler.expects(:append_backtrace).with(segment, 1.0)
396
+ @sampler.send(:notice_extra_data, 'a message', 1.0, key)
397
+ end
398
+
399
+ def test_truncate_message_short_message
400
+ message = 'a message'
401
+ assert_equal(message, @sampler.truncate_message(message))
402
+ end
403
+
404
+ def test_truncate_message_long_message
405
+ message = 'a' * 16384
406
+ truncated_message = @sampler.truncate_message(message)
407
+ assert_equal(16384, truncated_message.length)
408
+ assert_equal('a' * 16381 + '...', truncated_message)
409
+ end
410
+
411
+ def test_append_new_message_no_old_message
412
+ old_message = nil
413
+ new_message = 'a message'
414
+ assert_equal(new_message, @sampler.append_new_message(old_message, new_message))
415
+ end
416
+
417
+ def test_append_new_message_with_old_message
418
+ old_message = 'old message'
419
+ new_message = ' a message'
420
+ assert_equal("old message;\n a message", @sampler.append_new_message(old_message, new_message))
421
+ end
422
+
423
+ def test_append_backtrace_under_duration
424
+ @sampler.instance_eval { @stack_trace_threshold = 2.0 }
425
+ segment = mock('segment')
426
+ segment.expects(:[]=).with(:backtrace, any_parameters).never
427
+ @sampler.append_backtrace(mock('segment'), 1.0)
428
+ end
429
+
430
+ def test_append_backtrace_over_duration
431
+ @sampler.instance_eval { @stack_trace_threshold = 2.0 }
432
+ segment = mock('segment')
433
+ # note the mocha expectation matcher - you can't hardcode a
434
+ # backtrace so we match on any string, which should be okay.
435
+ segment.expects(:[]=).with(:backtrace, instance_of(String))
436
+ @sampler.append_backtrace(segment, 2.5)
437
+ end
438
+
439
+ def test_notice_sql_recording_sql
440
+ Thread.current[:record_sql] = true
441
+ @sampler.expects(:notice_extra_data).with('some sql', 1.0, :sql, 'a config', :connection_config)
442
+ @sampler.notice_sql('some sql', 'a config', 1.0)
443
+ end
444
+
445
+ def test_notice_sql_not_recording
446
+ Thread.current[:record_sql] = false
447
+ @sampler.expects(:notice_extra_data).with('some sql', 1.0, :sql, 'a config', :connection_config).never # <--- important
448
+ @sampler.notice_sql('some sql', 'a config', 1.0)
449
+ end
450
+
451
+ def test_notice_nosql
452
+ @sampler.expects(:notice_extra_data).with('a key', 1.0, :key)
453
+ @sampler.notice_nosql('a key', 1.0)
454
+ end
455
+
456
+ def test_harvest_when_disabled
457
+ @sampler.expects(:disabled).returns(true)
458
+ assert_equal([], @sampler.harvest)
459
+ end
460
+
461
+ def test_harvest_defaults
462
+ # making sure the sampler clears out the old samples
463
+ @sampler.instance_eval do
464
+ @slowest_sample = 'a sample'
465
+ @random_sample = 'a sample'
466
+ @last_sample = 'a sample'
467
+ end
468
+
469
+ @sampler.expects(:disabled).returns(false)
470
+ @sampler.expects(:add_samples_to).with([], 2.0).returns([])
471
+
472
+ assert_equal([], @sampler.harvest)
473
+
474
+ # make sure the samples have been cleared
475
+ assert_equal(nil, @sampler.instance_variable_get('@slowest_sample'))
476
+ assert_equal(nil, @sampler.instance_variable_get('@random_sample'))
477
+ assert_equal(nil, @sampler.instance_variable_get('@last_sample'))
478
+ end
479
+
480
+ def test_harvest_with_previous_samples
481
+ sample = mock('sample')
482
+ @sampler.expects(:disabled).returns(false)
483
+ @sampler.expects(:add_samples_to).with([sample], 2.0).returns([sample])
484
+ @sampler.instance_eval { @segment_limit = 2000 }
485
+ sample.expects(:truncate).with(2000)
486
+ assert_equal([sample], @sampler.harvest([sample]))
487
+ end
488
+
489
+ def test_add_random_sample_to_not_random_sampling
490
+ @sampler.instance_eval { @random_sampling = false }
491
+ result = []
492
+ @sampler.add_random_sample_to(result)
493
+ assert_equal([], result, "should not add anything to the array if we are not random sampling")
494
+ end
495
+
496
+ def test_add_random_sample_to_no_random_sample
497
+ @sampler.instance_eval { @random_sampling = true }
498
+ @sampler.instance_eval {
499
+ @harvest_count = 1
500
+ @sampling_rate = 2
501
+ @random_sample = nil
502
+ }
503
+ result = []
504
+ @sampler.add_random_sample_to(result)
505
+ assert_equal([], result, "should not add sample to the array when it is nil")
506
+ end
507
+
508
+ def test_add_random_sample_to_not_active
509
+ @sampler.instance_eval { @random_sampling = true }
510
+ sample = mock('sample')
511
+ @sampler.instance_eval {
512
+ @harvest_count = 4
513
+ @sampling_rate = 40 # 4 % 40 = 4, so the sample should not be added
514
+ @random_sample = sample
515
+ }
516
+ result = []
517
+ @sampler.add_random_sample_to(result)
518
+ assert_equal([], result, "should not add samples to the array when harvest count is not moduli sampling rate")
519
+ end
520
+
521
+ def test_add_random_sample_to_duplicate
522
+ @sampler.instance_eval { @random_sampling = true }
523
+ sample = mock('sample')
524
+ @sampler.instance_eval {
525
+ @harvest_count = 1
526
+ @sampling_rate = 2
527
+ @random_sample = sample
528
+ }
529
+ result = [sample]
530
+ @sampler.add_random_sample_to(result)
531
+ assert_equal([sample], result, "should not add duplicate samples to the array")
532
+ end
533
+
534
+ def test_add_random_sample_to_activated
535
+ @sampler.instance_eval { @random_sampling = true }
536
+ sample = mock('sample')
537
+ @sampler.instance_eval {
538
+ @harvest_count = 3
539
+ @sampling_rate = 1
540
+ @random_sample = sample
541
+ }
542
+ result = []
543
+ @sampler.add_random_sample_to(result)
544
+ assert_equal([sample], result, "should add the random sample to the array")
545
+ end
546
+
547
+ def test_add_random_sample_to_sampling_rate_zero
548
+ @sampler.instance_eval { @random_sampling = true }
549
+ sample = mock('sample')
550
+ @sampler.instance_eval {
551
+ @harvest_count = 3
552
+ @sampling_rate = 0
553
+ @random_sample = sample
554
+ }
555
+ result = []
556
+ @sampler.add_random_sample_to(result)
557
+ assert_equal([], result, "should not add the sample to the array")
558
+ end
559
+
560
+
561
+ def test_add_samples_to_no_data
562
+ result = []
563
+ slow_threshold = 2.0
564
+ @sampler.instance_eval { @slowest_sample = nil }
565
+ @sampler.expects(:add_random_sample_to).with([])
566
+ assert_equal([], @sampler.add_samples_to(result, slow_threshold))
567
+ end
568
+
569
+ def test_add_samples_to_one_result
570
+ sample = mock('sample')
571
+ sample.expects(:duration).returns(1).at_least_once
572
+ result = [sample]
573
+ slow_threshold = 2.0
574
+ @sampler.instance_eval { @slowest_sample = nil }
575
+ @sampler.expects(:add_random_sample_to).with([sample])
576
+ assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
577
+ end
578
+
579
+ def test_add_samples_to_adding_slowest
580
+ sample = mock('sample')
581
+ sample.expects(:duration).returns(2.5).at_least_once
582
+ result = []
583
+ slow_threshold = 2.0
584
+ @sampler.instance_eval { @slowest_sample = sample }
585
+ @sampler.expects(:add_random_sample_to).with([sample])
586
+ assert_equal([sample], @sampler.add_samples_to(result, slow_threshold))
587
+ end
588
+
589
+ def test_add_samples_to_under_threshold
590
+ result = []
591
+ slow_threshold = 2.0
592
+ sample = mock('sample')
593
+ sample.expects(:duration).returns(1.0).at_least_once
594
+ @sampler.instance_eval { @slowest_sample = sample }
595
+ @sampler.expects(:add_random_sample_to).with([])
596
+ assert_equal([], @sampler.add_samples_to(result, slow_threshold))
597
+ end
598
+
599
+ def test_add_samples_to_two_sample_enter_one_sample_leave
600
+ slower_sample = mock('slower')
601
+ slower_sample.expects(:duration).returns(10.0).at_least_once
602
+ faster_sample = mock('faster')
603
+ faster_sample.expects(:duration).returns(5.0).at_least_once
604
+ result = [faster_sample]
605
+ slow_threshold = 2.0
606
+ @sampler.instance_eval { @slowest_sample = slower_sample }
607
+ @sampler.expects(:add_random_sample_to).with([slower_sample])
608
+ assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
609
+ end
610
+
611
+ def test_add_samples_to_keep_older_slower_sample
612
+ slower_sample = mock('slower')
613
+ slower_sample.expects(:duration).returns(10.0).at_least_once
614
+ faster_sample = mock('faster')
615
+ faster_sample.expects(:duration).returns(5.0).at_least_once
616
+ result = [slower_sample]
617
+ slow_threshold = 2.0
618
+ @sampler.instance_eval { @slowest_sample = faster_sample }
619
+ @sampler.expects(:add_random_sample_to).with([slower_sample])
620
+ assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
621
+ end
622
+
623
+ def test_start_builder_default
624
+ Thread.current[:record_tt] = true
625
+ @sampler.expects(:disabled).returns(false)
626
+ NewRelic::Agent.expects(:is_execution_traced?).returns(true)
627
+ @sampler.send(:start_builder)
628
+ assert(Thread.current[:transaction_sample_builder].is_a?(NewRelic::Agent::TransactionSampleBuilder), "should set up a new builder by default")
629
+ end
630
+
631
+ def test_start_builder_disabled
632
+ Thread.current[:transaction_sample_builder] = 'not nil.'
633
+ @sampler.expects(:disabled).returns(true)
634
+ @sampler.send(:start_builder)
635
+ assert_equal(nil, Thread.current[:transaction_sample_builder], "should clear the transaction builder when disabled")
636
+ end
637
+
638
+ def test_start_builder_dont_replace_existing_builder
639
+ fake_builder = mock('transaction sample builder')
640
+ Thread.current[:transaction_sample_builder] = fake_builder
641
+ @sampler.expects(:disabled).returns(false)
642
+ @sampler.send(:start_builder)
643
+ assert_equal(fake_builder, Thread.current[:transaction_sample_builder], "should not overwrite an existing transaction sample builder")
644
+ end
645
+
646
+ def test_builder
647
+ Thread.current[:transaction_sample_builder] = 'shamalamadingdong, brother.'
648
+ assert_equal('shamalamadingdong, brother.', @sampler.send(:builder), 'should return the value from the thread local variable')
649
+ Thread.current[:transaction_sample_builder] = nil
650
+ end
651
+
652
+ def test_clear_builder
653
+ Thread.current[:transaction_sample_builder] = 'shamalamadingdong, brother.'
654
+ assert_equal(nil, @sampler.send(:clear_builder), 'should clear the thread local variable')
655
+ end
656
+
657
+ # Tests below this line are functional tests for the sampler, not
658
+ # unit tests per se - some overlap with the tests above, but
659
+ # generally usefully so
660
+
661
+ def test_multiple_samples
662
+
663
+ run_sample_trace
664
+ run_sample_trace
665
+ run_sample_trace
666
+ run_sample_trace
667
+
668
+ samples = @sampler.samples
669
+ assert_equal 4, samples.length
670
+ assert_equal "a", samples.first.root_segment.called_segments[0].metric_name
671
+ assert_equal "a", samples.last.root_segment.called_segments[0].metric_name
672
+ end
673
+
674
+ def test_sample_tree
675
+ assert_equal 0, @sampler.scope_depth
676
+
677
+ @sampler.notice_first_scope_push Time.now.to_f
678
+ @sampler.notice_transaction "/path", nil, {}
679
+ @sampler.notice_push_scope "a"
680
+
681
+ @sampler.notice_push_scope "b"
682
+ @sampler.notice_pop_scope "b"
683
+
684
+ @sampler.notice_push_scope "c"
685
+ @sampler.notice_push_scope "d"
686
+ @sampler.notice_pop_scope "d"
687
+ @sampler.notice_pop_scope "c"
688
+
689
+ @sampler.notice_pop_scope "a"
690
+ @sampler.notice_scope_empty
691
+ sample = @sampler.harvest([],0.0).first
692
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
693
+
694
+ end
695
+
696
+ def test_sample__gc_stats
697
+ GC.extend MockGCStats
698
+ # These are effectively Garbage Collects, detected each time GC.time is
699
+ # called by the transaction sampler. One time value in seconds for each call.
700
+ MockGCStats.mock_values = [0,0,0,1,0,0,1,0,0,0,0,0,0,0,0]
701
+ assert_equal 0, @sampler.scope_depth
702
+
703
+ @sampler.notice_first_scope_push Time.now.to_f
704
+ @sampler.notice_transaction "/path", nil, {}
705
+ @sampler.notice_push_scope "a"
706
+
707
+ @sampler.notice_push_scope "b"
708
+ @sampler.notice_pop_scope "b"
709
+
710
+ @sampler.notice_push_scope "c"
711
+ @sampler.notice_push_scope "d"
712
+ @sampler.notice_pop_scope "d"
713
+ @sampler.notice_pop_scope "c"
714
+
715
+ @sampler.notice_pop_scope "a"
716
+ @sampler.notice_scope_empty
717
+
718
+ sample = @sampler.harvest([],0.0).first
719
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
720
+ ensure
721
+ MockGCStats.mock_values = []
722
+ end
723
+
724
+ def test_sample_id
725
+ run_sample_trace do
726
+ assert((@sampler.current_sample_id && @sampler.current_sample_id != 0), @sampler.current_sample_id.to_s + ' should not be zero')
727
+ end
728
+ end
729
+
730
+
731
+ # NB this test occasionally fails due to a GC during one of the
732
+ # sample traces, for example. It's unfortunate, but we can't
733
+ # reliably turn off GC on all versions of ruby under test
734
+ def test_harvest_slowest
735
+
736
+ run_sample_trace
737
+ run_sample_trace
738
+ run_sample_trace { sleep 0.1 }
739
+ run_sample_trace
740
+ run_sample_trace
741
+
742
+ slowest = @sampler.harvest(nil, 0)[0]
743
+ assert((slowest.duration >= 0.09), "expected sample duration >= 0.09, but was: #{slowest.duration.inspect}")
744
+ # this assert is here to make sure the test remains valid
745
+ assert((slowest.duration <= 0.15), "expected sample duration <= 0.15, but was: #{slowest.duration.inspect}")
746
+
747
+
748
+ run_sample_trace { sleep 0.0001 }
749
+ not_as_slow = @sampler.harvest(slowest, 0)[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 { sleep 0.16 }
753
+ new_slowest = @sampler.harvest(slowest, 0)[0]
754
+ assert((new_slowest != slowest), "Should not harvest the same trace since the new one should be slower")
755
+ assert((new_slowest.duration >= 0.15), "Slowest duration must be >= 0.15, but was: #{new_slowest.duration.inspect}")
756
+ end
757
+
758
+
759
+ def test_prepare_to_send
760
+
761
+ run_sample_trace { sleep 0.002 }
762
+ sample = @sampler.harvest(nil, 0)[0]
763
+
764
+ ready_to_send = sample.prepare_to_send
765
+ assert sample.duration == ready_to_send.duration
766
+
767
+ assert ready_to_send.start_time.is_a?(Time)
768
+ end
769
+
770
+ def test_multithread
771
+ threads = []
772
+
773
+ 5.times do
774
+ t = Thread.new(@sampler) do |the_sampler|
775
+ @sampler = the_sampler
776
+ 10.times do
777
+ run_sample_trace { sleep 0.0001 }
778
+ end
779
+ end
780
+
781
+ threads << t
782
+ end
783
+ threads.each {|t| t.join }
784
+ end
785
+
786
+ def test_sample_with_parallel_paths
787
+
788
+ assert_equal 0, @sampler.scope_depth
789
+
790
+ @sampler.notice_first_scope_push Time.now.to_f
791
+ @sampler.notice_transaction "/path", nil, {}
792
+ @sampler.notice_push_scope "a"
793
+
794
+ assert_equal 1, @sampler.scope_depth
795
+
796
+ @sampler.notice_pop_scope "a"
797
+ @sampler.notice_scope_empty
798
+
799
+ assert_equal 0, @sampler.scope_depth
800
+
801
+ @sampler.notice_first_scope_push Time.now.to_f
802
+ @sampler.notice_transaction "/path", nil, {}
803
+ @sampler.notice_push_scope "a"
804
+ @sampler.notice_pop_scope "a"
805
+ @sampler.notice_scope_empty
806
+
807
+ assert_equal 0, @sampler.scope_depth
808
+ sample = @sampler.harvest(nil, 0.0).first
809
+ assert_equal "ROOT{a}", sample.to_s_compact
810
+ end
811
+
812
+ def test_double_scope_stack_empty
813
+
814
+ @sampler.notice_first_scope_push Time.now.to_f
815
+ @sampler.notice_transaction "/path", nil, {}
816
+ @sampler.notice_push_scope "a"
817
+ @sampler.notice_pop_scope "a"
818
+ @sampler.notice_scope_empty
819
+ @sampler.notice_scope_empty
820
+ @sampler.notice_scope_empty
821
+ @sampler.notice_scope_empty
822
+
823
+ assert_not_nil @sampler.harvest(nil, 0)[0]
824
+ end
825
+
826
+
827
+ def test_record_sql_off
828
+
829
+ @sampler.notice_first_scope_push Time.now.to_f
830
+
831
+ Thread::current[:record_sql] = false
832
+
833
+ @sampler.notice_sql("test", nil, 0)
834
+
835
+ segment = @sampler.send(:builder).current_segment
836
+
837
+ assert_nil segment[:sql]
838
+ end
839
+
840
+ def test_stack_trace__sql
841
+ @sampler.stack_trace_threshold = 0
842
+
843
+ @sampler.notice_first_scope_push Time.now.to_f
844
+
845
+ @sampler.notice_sql("test", nil, 1)
846
+
847
+ segment = @sampler.send(:builder).current_segment
848
+
849
+ assert segment[:sql]
850
+ assert segment[:backtrace]
851
+ end
852
+ def test_stack_trace__scope
853
+
854
+ @sampler.stack_trace_threshold = 0
855
+ t = Time.now
856
+ @sampler.notice_first_scope_push t.to_f
857
+ @sampler.notice_push_scope 'Bill', (t+1).to_f
858
+
859
+ segment = @sampler.send(:builder).current_segment
860
+ assert segment[:backtrace]
861
+ end
862
+
863
+ def test_nil_stacktrace
864
+
865
+ @sampler.stack_trace_threshold = 2
866
+
867
+ @sampler.notice_first_scope_push Time.now.to_f
868
+
869
+ @sampler.notice_sql("test", nil, 1)
870
+
871
+ segment = @sampler.send(:builder).current_segment
872
+
873
+ assert segment[:sql]
874
+ assert_nil segment[:backtrace]
875
+ end
876
+
877
+ def test_big_sql
878
+
879
+ @sampler.notice_first_scope_push Time.now.to_f
880
+
881
+ sql = "SADJKHASDHASD KAJSDH ASKDH ASKDHASDK JASHD KASJDH ASKDJHSAKDJHAS DKJHSADKJSAH DKJASHD SAKJDH SAKDJHS"
882
+
883
+ len = 0
884
+ while len <= 16384
885
+ @sampler.notice_sql(sql, nil, 0)
886
+ len += sql.length
887
+ end
888
+
889
+ segment = @sampler.send(:builder).current_segment
890
+
891
+ sql = segment[:sql]
892
+
893
+ assert sql.length <= 16384
894
+ end
895
+
896
+
897
+ def test_segment_obfuscated
898
+
899
+ @sampler.notice_first_scope_push Time.now.to_f
900
+ @sampler.notice_push_scope "foo"
901
+
902
+ orig_sql = "SELECT * from Jim where id=66"
903
+
904
+ @sampler.notice_sql(orig_sql, nil, 0)
905
+
906
+ segment = @sampler.send(:builder).current_segment
907
+
908
+ assert_equal orig_sql, segment[:sql]
909
+ assert_equal "SELECT * from Jim where id=?", segment.obfuscated_sql
910
+ @sampler.notice_pop_scope "foo"
911
+ end
912
+
913
+
914
+ def test_param_capture
915
+ [true, false].each do |capture|
916
+ NewRelic::Control.instance.stubs(:capture_params).returns(capture)
917
+ @sampler.notice_first_scope_push Time.now.to_f
918
+ @sampler.notice_transaction('/path', nil, {:param => 'hi'})
919
+ @sampler.notice_scope_empty
920
+
921
+ tt = @sampler.harvest(nil,0)[0]
922
+
923
+ assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
924
+ end
925
+ end
926
+
927
+
928
+ private
929
+ def run_sample_trace(&proc)
930
+ @sampler.notice_first_scope_push Time.now.to_f
931
+ @sampler.notice_transaction '/path', nil, {}
932
+ @sampler.notice_push_scope "a"
933
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
934
+ @sampler.notice_push_scope "ab"
935
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
936
+ proc.call if proc
937
+ @sampler.notice_pop_scope "ab"
938
+ @sampler.notice_push_scope "lew"
939
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
940
+ @sampler.notice_pop_scope "lew"
941
+ @sampler.notice_pop_scope "a"
942
+ @sampler.notice_scope_empty
943
+ end
944
+
945
+ end