ghazel-newrelic_rpm 3.1.0.1 → 3.4.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/CHANGELOG +120 -35
  2. data/LICENSE +29 -2
  3. data/README.rdoc +2 -2
  4. data/bin/mongrel_rpm +0 -0
  5. data/bin/newrelic +0 -0
  6. data/bin/newrelic_cmd +0 -0
  7. data/lib/new_relic/agent.rb +50 -38
  8. data/lib/new_relic/agent/agent.rb +459 -337
  9. data/lib/new_relic/agent/beacon_configuration.rb +71 -11
  10. data/lib/new_relic/agent/browser_monitoring.rb +73 -14
  11. data/lib/new_relic/agent/busy_calculator.rb +11 -3
  12. data/lib/new_relic/agent/chained_call.rb +2 -2
  13. data/lib/new_relic/agent/database.rb +223 -0
  14. data/lib/new_relic/agent/error_collector.rb +231 -183
  15. data/lib/new_relic/agent/instrumentation.rb +2 -2
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +10 -2
  17. data/lib/new_relic/agent/instrumentation/active_record.rb +138 -0
  18. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +7 -1
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +6 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +46 -14
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +8 -2
  22. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +11 -3
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +49 -25
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +7 -2
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +7 -1
  26. data/lib/new_relic/agent/instrumentation/metric_frame.rb +31 -4
  27. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -5
  28. data/lib/new_relic/agent/instrumentation/net.rb +8 -2
  29. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +5 -2
  30. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +66 -35
  32. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +7 -1
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -1
  34. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +121 -1
  35. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +7 -1
  36. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +21 -0
  37. data/lib/new_relic/agent/instrumentation/resque.rb +80 -0
  38. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -20
  39. data/lib/new_relic/agent/instrumentation/sunspot.rb +6 -0
  40. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +7 -2
  41. data/lib/new_relic/agent/method_tracer.rb +205 -99
  42. data/lib/new_relic/agent/new_relic_service.rb +221 -0
  43. data/lib/new_relic/agent/pipe_channel_manager.rb +161 -0
  44. data/lib/new_relic/agent/pipe_service.rb +54 -0
  45. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +89 -0
  46. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -7
  47. data/lib/new_relic/agent/shim_agent.rb +5 -5
  48. data/lib/new_relic/agent/sql_sampler.rb +282 -0
  49. data/lib/new_relic/agent/stats_engine.rb +2 -0
  50. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  51. data/lib/new_relic/agent/stats_engine/metric_stats.rb +35 -30
  52. data/lib/new_relic/agent/stats_engine/samplers.rb +10 -4
  53. data/lib/new_relic/agent/stats_engine/transactions.rb +28 -87
  54. data/lib/new_relic/agent/transaction_info.rb +74 -0
  55. data/lib/new_relic/agent/transaction_sample_builder.rb +18 -3
  56. data/lib/new_relic/agent/transaction_sampler.rb +108 -20
  57. data/lib/new_relic/agent/worker_loop.rb +14 -6
  58. data/lib/new_relic/collection_helper.rb +19 -11
  59. data/lib/new_relic/command.rb +1 -1
  60. data/lib/new_relic/commands/deployments.rb +2 -2
  61. data/lib/new_relic/commands/install.rb +2 -13
  62. data/lib/new_relic/control.rb +2 -3
  63. data/lib/new_relic/control/class_methods.rb +12 -6
  64. data/lib/new_relic/control/configuration.rb +57 -8
  65. data/lib/new_relic/control/frameworks.rb +10 -0
  66. data/lib/new_relic/control/frameworks/external.rb +4 -4
  67. data/lib/new_relic/control/frameworks/merb.rb +2 -1
  68. data/lib/new_relic/control/frameworks/rails.rb +35 -22
  69. data/lib/new_relic/control/frameworks/rails3.rb +12 -7
  70. data/lib/new_relic/control/frameworks/ruby.rb +5 -5
  71. data/lib/new_relic/control/frameworks/sinatra.rb +1 -4
  72. data/lib/new_relic/control/instance_methods.rb +38 -12
  73. data/lib/new_relic/control/instrumentation.rb +23 -4
  74. data/lib/new_relic/control/logging_methods.rb +70 -15
  75. data/lib/new_relic/control/server_methods.rb +22 -9
  76. data/lib/new_relic/delayed_job_injection.rb +16 -3
  77. data/lib/new_relic/helper.rb +21 -0
  78. data/lib/new_relic/language_support.rb +95 -0
  79. data/lib/new_relic/local_environment.rb +92 -48
  80. data/lib/new_relic/metric_data.rb +7 -2
  81. data/lib/new_relic/metric_spec.rb +12 -9
  82. data/lib/new_relic/noticed_error.rb +6 -1
  83. data/lib/new_relic/rack/browser_monitoring.rb +18 -19
  84. data/lib/new_relic/rack/developer_mode.rb +3 -2
  85. data/lib/new_relic/recipes.rb +8 -4
  86. data/lib/new_relic/stats.rb +17 -60
  87. data/lib/new_relic/transaction_analysis.rb +2 -1
  88. data/lib/new_relic/transaction_analysis/segment_summary.rb +4 -2
  89. data/lib/new_relic/transaction_sample.rb +60 -75
  90. data/lib/new_relic/transaction_sample/segment.rb +31 -79
  91. data/lib/new_relic/version.rb +2 -2
  92. data/lib/newrelic_rpm.rb +1 -1
  93. data/newrelic.yml +2 -2
  94. data/newrelic_rpm.gemspec +46 -54
  95. data/test/active_record_fixtures.rb +3 -3
  96. data/test/config/newrelic.yml +1 -1
  97. data/test/fixtures/proc_cpuinfo.txt +575 -0
  98. data/test/new_relic/agent/agent/connect_test.rb +128 -25
  99. data/test/new_relic/agent/agent/start_test.rb +9 -94
  100. data/test/new_relic/agent/agent/start_worker_thread_test.rb +2 -4
  101. data/test/new_relic/agent/agent_test.rb +51 -78
  102. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  103. data/test/new_relic/agent/agent_test_controller_test.rb +49 -33
  104. data/test/new_relic/agent/beacon_configuration_test.rb +12 -5
  105. data/test/new_relic/agent/browser_monitoring_test.rb +99 -50
  106. data/test/new_relic/agent/database_test.rb +161 -0
  107. data/test/new_relic/agent/error_collector_test.rb +47 -23
  108. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +96 -42
  109. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +0 -2
  110. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +1 -1
  111. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +3 -11
  112. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +9 -9
  113. data/test/new_relic/agent/instrumentation/queue_time_test.rb +6 -11
  114. data/test/new_relic/agent/memcache_instrumentation_test.rb +54 -18
  115. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -1
  116. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +1 -1
  117. data/test/new_relic/agent/method_tracer_test.rb +3 -2
  118. data/test/new_relic/agent/new_relic_service_test.rb +151 -0
  119. data/test/new_relic/agent/pipe_channel_manager_test.rb +114 -0
  120. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  121. data/test/new_relic/agent/rpm_agent_test.rb +4 -31
  122. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +19 -18
  124. data/test/new_relic/agent/stats_engine_test.rb +41 -6
  125. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  126. data/test/new_relic/agent/transaction_sample_builder_test.rb +27 -4
  127. data/test/new_relic/agent/transaction_sampler_test.rb +68 -46
  128. data/test/new_relic/agent/worker_loop_test.rb +3 -3
  129. data/test/new_relic/agent_test.rb +242 -0
  130. data/test/new_relic/collection_helper_test.rb +50 -28
  131. data/test/new_relic/control/configuration_test.rb +77 -0
  132. data/test/new_relic/control/logging_methods_test.rb +49 -21
  133. data/test/new_relic/control_test.rb +115 -54
  134. data/test/new_relic/delayed_job_injection_test.rb +21 -0
  135. data/test/new_relic/fake_collector.rb +210 -0
  136. data/test/new_relic/fake_service.rb +44 -0
  137. data/test/new_relic/local_environment_test.rb +14 -1
  138. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  139. data/test/new_relic/rack/browser_monitoring_test.rb +84 -23
  140. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  141. data/test/new_relic/rack/developer_mode_test.rb +31 -0
  142. data/test/new_relic/stats_test.rb +3 -18
  143. data/test/new_relic/transaction_analysis/segment_summary_test.rb +14 -0
  144. data/test/new_relic/transaction_analysis_test.rb +3 -3
  145. data/test/new_relic/transaction_sample/segment_test.rb +15 -80
  146. data/test/new_relic/transaction_sample_test.rb +25 -18
  147. data/test/script/build_test_gem.sh +51 -0
  148. data/test/script/ci.sh +140 -0
  149. data/test/script/ci_agent-tests_runner.sh +82 -0
  150. data/test/script/ci_bench.sh +52 -0
  151. data/test/script/ci_multiverse_runner.sh +63 -0
  152. data/test/test_contexts.rb +1 -0
  153. data/test/test_helper.rb +18 -5
  154. data/ui/helpers/developer_mode_helper.rb +14 -8
  155. data/ui/helpers/google_pie_chart.rb +0 -1
  156. data/ui/views/newrelic/index.rhtml +2 -2
  157. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +4 -18
  158. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +10 -0
  159. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +11 -11
  160. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +17 -4
  161. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +4 -0
  162. metadata +50 -36
  163. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +0 -108
  164. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +0 -112
  165. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  166. data/lib/new_relic/data_serialization.rb +0 -84
  167. data/lib/new_relic/histogram.rb +0 -91
  168. data/lib/new_relic/rack/metric_app.rb +0 -65
  169. data/lib/new_relic/rack/mongrel_rpm.ru +0 -28
  170. data/lib/new_relic/rack/newrelic.yml +0 -27
  171. data/lib/new_relic/rack_app.rb +0 -6
  172. data/test/new_relic/data_serialization_test.rb +0 -70
  173. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  174. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  175. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
@@ -9,6 +9,7 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
9
9
  puts e
10
10
  puts e.backtrace.join("\n")
11
11
  end
12
+
12
13
  def teardown
13
14
  @engine.harvest_timeslice_data({},{})
14
15
  super
@@ -28,6 +29,7 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
28
29
  end
29
30
 
30
31
  def test_harvest
32
+ @engine.clear_stats
31
33
  s1 = @engine.get_stats "a"
32
34
  s2 = @engine.get_stats "c"
33
35
 
@@ -35,47 +37,46 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
35
37
  s2.trace_call 1
36
38
  s2.trace_call 3
37
39
 
38
- assert @engine.get_stats("a").call_count == 1
39
- assert @engine.get_stats("a").total_call_time == 10
40
+ assert_equal 1, @engine.get_stats("a").call_count
41
+ assert_equal 10, @engine.get_stats("a").total_call_time
40
42
 
41
- assert @engine.get_stats("c").call_count == 2
42
- assert @engine.get_stats("c").total_call_time == 4
43
+ assert_equal 2, @engine.get_stats("c").call_count
44
+ assert_equal 4, @engine.get_stats("c").total_call_time
43
45
 
44
46
  metric_data = @engine.harvest_timeslice_data({}, {}).values
45
-
47
+
46
48
  # after harvest, all the metrics should be reset
47
- assert @engine.get_stats("a").call_count == 0
48
- assert @engine.get_stats("a").total_call_time == 0
49
+ assert_equal 0, @engine.get_stats("a").call_count
50
+ assert_equal 0, @engine.get_stats("a").total_call_time
49
51
 
50
- assert @engine.get_stats("c").call_count == 0
51
- assert @engine.get_stats("c").total_call_time == 0
52
+ assert_equal 0, @engine.get_stats("c").call_count
53
+ assert_equal 0, @engine.get_stats("c").total_call_time
52
54
 
53
55
  metric_data = metric_data.reverse if metric_data[0].metric_spec.name != "a"
54
56
 
55
- assert metric_data[0].metric_spec.name == "a"
57
+ assert_equal 'a', metric_data[0].metric_spec.name
56
58
 
57
- assert metric_data[0].stats.call_count == 1
58
- assert metric_data[0].stats.total_call_time == 10
59
+ assert_equal 1, metric_data[0].stats.call_count
60
+ assert_equal 10, metric_data[0].stats.total_call_time
59
61
  end
60
62
 
61
63
  def test_harvest_with_merge
62
64
  s = @engine.get_stats "a"
63
65
  s.trace_call 1
64
66
 
65
- assert @engine.get_stats("a").call_count == 1
67
+ assert_equal 1, @engine.get_stats("a").call_count
66
68
 
67
69
  harvest = @engine.harvest_timeslice_data({}, {})
68
- assert s.call_count == 0
70
+ assert_equal 0, s.call_count
69
71
  s.trace_call 2
70
- assert s.call_count == 1
72
+ assert_equal 1, s.call_count
71
73
 
72
74
  # this calk should merge the contents of the previous harvest,
73
75
  # so the stats for metric "a" should have 2 data points
74
76
  harvest = @engine.harvest_timeslice_data(harvest, {})
75
77
  stats = harvest.fetch(NewRelic::MetricSpec.new("a")).stats
76
- assert stats.call_count == 2
77
- assert stats.total_call_time == 3
78
+ assert_equal 2, stats.call_count
79
+ assert_equal 3, stats.total_call_time
78
80
  end
79
-
80
81
  end
81
82
 
@@ -9,8 +9,10 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
9
9
  puts e
10
10
  puts e.backtrace.join("\n")
11
11
  end
12
+
12
13
  def teardown
13
14
  @engine.harvest_timeslice_data({},{})
15
+ mocha_teardown
14
16
  super
15
17
  end
16
18
 
@@ -169,17 +171,50 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
169
171
  @engine.pop_scope scope2, 10
170
172
  @engine.pop_scope scope1, 10
171
173
 
172
- assert_equal 0, scope4.children_time
173
- assert_equal 10, scope3.children_time
174
- assert_equal 10, scope2.children_time
175
- assert_equal 10, scope1.children_time
174
+ assert_equal 0, scope4.children_time.round
175
+ assert_equal 10, scope3.children_time.round
176
+ assert_equal 10, scope2.children_time.round
177
+ assert_equal 10, scope1.children_time.round
176
178
  end
177
179
 
178
180
 
181
+ def test_collect_gc_data
182
+ GC.disable unless NewRelic::LanguageSupport.using_engine?('jruby')
183
+ if NewRelic::LanguageSupport.using_engine?('rbx')
184
+ agent = ::Rubinius::Agent.loopback
185
+ agent.stubs(:get).with('system.gc.young.total_wallclock') \
186
+ .returns([:value, 1000], [:value, 2500])
187
+ agent.stubs(:get).with('system.gc.full.total_wallclock') \
188
+ .returns([:value, 2000], [:value, 3500])
189
+ agent.stubs(:get).with('system.gc.young.count') \
190
+ .returns([:value, 1], [:value, 2])
191
+ agent.stubs(:get).with('system.gc.full.count') \
192
+ .returns([:value, 1], [:value, 2])
193
+ ::Rubinius::Agent.stubs(:loopback).returns(agent)
194
+ elsif NewRelic::LanguageSupport.using_version?('1.9')
195
+ ::GC::Profiler.stubs(:enabled?).returns(true)
196
+ ::GC::Profiler.stubs(:total_time).returns(1.0, 4.0)
197
+ ::GC.stubs(:count).returns(1, 3)
198
+ elsif NewRelic::LanguageSupport.using_version?('1.8')
199
+ ::GC.stubs(:time).returns(1000000, 4000000)
200
+ ::GC.stubs(:collections).returns(1, 3)
201
+ end
202
+
203
+ engine = NewRelic::Agent.instance.stats_engine
204
+ scope = engine.push_scope "scope"
205
+ engine.start_transaction
206
+ engine.pop_scope scope, 0.01
207
+ engine.end_transaction
208
+
209
+ gc_stats = engine.get_stats('GC/cumulative')
210
+ assert_equal 2, gc_stats.call_count
211
+ assert_equal 3.0, gc_stats.total_call_time
212
+ ensure
213
+ GC.enable unless NewRelic::LanguageSupport.using_engine?('jruby')
214
+ end
215
+
179
216
  private
180
217
  def check_time_approximate(expected, actual)
181
218
  assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
182
219
  end
183
-
184
220
  end
185
-
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'ostruct'
3
+
4
+ class NewRelic::Agent::TransactionInfoTest < Test::Unit::TestCase
5
+ def setup
6
+ @request = OpenStruct.new(:cookies => {'NRAGENT' => 'tk=1234<tag>evil</tag>5678'})
7
+ end
8
+
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))
12
+ end
13
+ end
@@ -6,6 +6,14 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
6
6
  @builder = NewRelic::Agent::TransactionSampleBuilder.new
7
7
  end
8
8
 
9
+ # if it doesn't the core app tests will break. Not strictly necessary but
10
+ # we'll enforce it with this test for now.
11
+ def test_trace_entry_returns_segment
12
+ segment = @builder.trace_entry("/Foo/Bar", Time.now)
13
+ assert segment, "Segment should not be nil"
14
+ assert segment.is_a?(NewRelic::TransactionSample::Segment), "Segment should not be a #{segment.class.name}"
15
+ end
16
+
9
17
  def test_build_sample
10
18
  build_segment("a") do
11
19
  build_segment("aa") do
@@ -42,7 +50,7 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
42
50
  begin
43
51
  builder.sample
44
52
  assert false
45
- rescue Exception => e
53
+ rescue => e
46
54
  # expected
47
55
  end
48
56
 
@@ -52,7 +60,7 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
52
60
 
53
61
  begin
54
62
  build_segment "b"
55
- assert_false
63
+ assert false
56
64
  rescue TypeError => e
57
65
  # expected
58
66
  end
@@ -94,8 +102,10 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
94
102
 
95
103
  should_be_a_copy = sample.omit_segments_with('OMIT NOTHING')
96
104
  validate_segment should_be_a_copy.root_segment, false
97
-
98
- assert sample.to_s == should_be_a_copy.to_s
105
+
106
+ assert_equal sample.params, should_be_a_copy.params
107
+ assert_equal(sample.root_segment.to_debug_str(0),
108
+ should_be_a_copy.root_segment.to_debug_str(0))
99
109
 
100
110
  without_code_loading = sample.omit_segments_with('Rails/Application Code Loading')
101
111
  validate_segment without_code_loading.root_segment, false
@@ -159,7 +169,20 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
159
169
  @builder.finish_trace(Time.now.to_f)
160
170
  validate_builder
161
171
  end
172
+
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
177
+ end
162
178
 
179
+ # regression
180
+ 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 }
184
+ end
185
+
163
186
  def validate_builder(check_names = true)
164
187
  validate_segment @builder.sample.root_segment, check_names
165
188
  end
@@ -57,6 +57,15 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
57
57
  assert(lock.is_a?(Mutex), "Samples lock should be a mutex, is: #{lock.inspect}")
58
58
  end
59
59
 
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
+
60
69
  def test_current_sample_id_default
61
70
  builder = mock('builder')
62
71
  builder.expects(:sample_id).returns(11111)
@@ -77,10 +86,10 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
77
86
  end
78
87
 
79
88
  def test_disable
80
- assert_equal(nil, @sampler.instance_variable_get('@disabled'))
89
+ assert_nil @sampler.instance_variable_get('@disabled')
81
90
  @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'))
91
+ assert @sampler.instance_variable_get('@disabled')
92
+ assert_nil NewRelic::Agent.instance.stats_engine.instance_variable_get('@transaction_sampler')
84
93
  end
85
94
 
86
95
  def test_sampling_rate_equals_default
@@ -238,7 +247,8 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
238
247
  end
239
248
 
240
249
  def test_store_sample_for_developer_mode_no_dev
241
- NewRelic::Control.instance.expects(:developer_mode?).returns(false)
250
+ NewRelic::Control.instance.stubs(:developer_mode?).returns(false)
251
+ @sampler.configure!
242
252
  sample = mock('sample')
243
253
  @sampler.store_sample_for_developer_mode(sample)
244
254
  assert_equal([], @sampler.instance_variable_get('@samples'))
@@ -255,7 +265,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
255
265
  assert_equal(new_sample, @sampler.instance_variable_get('@slowest_sample'))
256
266
  end
257
267
 
258
-
259
268
  def test_store_slowest_sample_not_slowest
260
269
  old_sample = mock('old_sample')
261
270
  new_sample = mock('new_sample')
@@ -288,7 +297,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
288
297
  new_sample.expects(:duration).returns(1.0)
289
298
  assert_equal(true, @sampler.slowest_sample?(old_sample, new_sample))
290
299
  end
291
-
300
+
292
301
  def test_truncate_samples_no_samples
293
302
  @sampler.instance_eval { @max_samples = 10 }
294
303
  @sampler.instance_eval { @samples = [] }
@@ -518,19 +527,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
518
527
  assert_equal([], result, "should not add samples to the array when harvest count is not moduli sampling rate")
519
528
  end
520
529
 
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
530
  def test_add_random_sample_to_activated
535
531
  @sampler.instance_eval { @random_sampling = true }
536
532
  sample = mock('sample')
@@ -557,7 +553,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
557
553
  assert_equal([], result, "should not add the sample to the array")
558
554
  end
559
555
 
560
-
561
556
  def test_add_samples_to_no_data
562
557
  result = []
563
558
  slow_threshold = 2.0
@@ -569,6 +564,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
569
564
  def test_add_samples_to_one_result
570
565
  sample = mock('sample')
571
566
  sample.expects(:duration).returns(1).at_least_once
567
+ sample.stubs(:force_persist).returns(false)
572
568
  result = [sample]
573
569
  slow_threshold = 2.0
574
570
  @sampler.instance_eval { @slowest_sample = nil }
@@ -601,6 +597,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
601
597
  slower_sample.expects(:duration).returns(10.0).at_least_once
602
598
  faster_sample = mock('faster')
603
599
  faster_sample.expects(:duration).returns(5.0).at_least_once
600
+ faster_sample.stubs(:force_persist).returns(false)
604
601
  result = [faster_sample]
605
602
  slow_threshold = 2.0
606
603
  @sampler.instance_eval { @slowest_sample = slower_sample }
@@ -611,6 +608,8 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
611
608
  def test_add_samples_to_keep_older_slower_sample
612
609
  slower_sample = mock('slower')
613
610
  slower_sample.expects(:duration).returns(10.0).at_least_once
611
+ slower_sample.stubs(:force_persist).returns(false)
612
+
614
613
  faster_sample = mock('faster')
615
614
  faster_sample.expects(:duration).returns(5.0).at_least_once
616
615
  result = [slower_sample]
@@ -619,6 +618,22 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
619
618
  @sampler.expects(:add_random_sample_to).with([slower_sample])
620
619
  assert_equal([slower_sample], @sampler.add_samples_to(result, slow_threshold))
621
620
  end
621
+
622
+ def test_keep_force_persist
623
+ sample1 = mock('regular')
624
+ sample1.stubs(:duration).returns(10)
625
+ sample1.stubs(:force_persist).returns(false)
626
+
627
+ sample2 = mock('force_persist')
628
+ sample2.stubs(:duration).returns(1)
629
+ sample2.stubs(:force_persist).returns(true)
630
+
631
+ result = @sampler.add_samples_to([sample1,sample2], 2.0)
632
+
633
+ assert_equal 2, result.length
634
+ assert_equal sample1, result[0]
635
+ assert_equal sample2, result[1]
636
+ end
622
637
 
623
638
  def test_start_builder_default
624
639
  Thread.current[:record_tt] = true
@@ -641,6 +656,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
641
656
  @sampler.expects(:disabled).returns(false)
642
657
  @sampler.send(:start_builder)
643
658
  assert_equal(fake_builder, Thread.current[:transaction_sample_builder], "should not overwrite an existing transaction sample builder")
659
+ Thread.current[:transaction_sample_builder] = nil
644
660
  end
645
661
 
646
662
  def test_builder
@@ -659,7 +675,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
659
675
  # generally usefully so
660
676
 
661
677
  def test_multiple_samples
662
-
663
678
  run_sample_trace
664
679
  run_sample_trace
665
680
  run_sample_trace
@@ -732,7 +747,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
732
747
  # sample traces, for example. It's unfortunate, but we can't
733
748
  # reliably turn off GC on all versions of ruby under test
734
749
  def test_harvest_slowest
735
-
736
750
  run_sample_trace
737
751
  run_sample_trace
738
752
  run_sample_trace { sleep 0.1 }
@@ -740,24 +754,22 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
740
754
  run_sample_trace
741
755
 
742
756
  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
-
757
+ first_duration = slowest.duration
758
+ assert((first_duration >= 0.1),
759
+ "expected sample duration >= 0.1, but was: #{slowest.duration.inspect}")
747
760
 
748
761
  run_sample_trace { sleep 0.0001 }
749
762
  not_as_slow = @sampler.harvest(slowest, 0)[0]
750
763
  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
764
 
752
- run_sample_trace { sleep 0.16 }
765
+ run_sample_trace { sleep(first_duration + 0.1) }
766
+
753
767
  new_slowest = @sampler.harvest(slowest, 0)[0]
754
768
  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}")
769
+ assert((new_slowest.duration >= first_duration + 0.1), "Slowest duration must be >= #{first_duration + 0.1}, but was: #{new_slowest.duration.inspect}")
756
770
  end
757
771
 
758
-
759
772
  def test_prepare_to_send
760
-
761
773
  run_sample_trace { sleep 0.002 }
762
774
  sample = @sampler.harvest(nil, 0)[0]
763
775
 
@@ -784,7 +796,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
784
796
  end
785
797
 
786
798
  def test_sample_with_parallel_paths
787
-
788
799
  assert_equal 0, @sampler.scope_depth
789
800
 
790
801
  @sampler.notice_first_scope_push Time.now.to_f
@@ -810,7 +821,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
810
821
  end
811
822
 
812
823
  def test_double_scope_stack_empty
813
-
814
824
  @sampler.notice_first_scope_push Time.now.to_f
815
825
  @sampler.notice_transaction "/path", nil, {}
816
826
  @sampler.notice_push_scope "a"
@@ -825,7 +835,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
825
835
 
826
836
 
827
837
  def test_record_sql_off
828
-
829
838
  @sampler.notice_first_scope_push Time.now.to_f
830
839
 
831
840
  Thread::current[:record_sql] = false
@@ -849,8 +858,8 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
849
858
  assert segment[:sql]
850
859
  assert segment[:backtrace]
851
860
  end
861
+
852
862
  def test_stack_trace__scope
853
-
854
863
  @sampler.stack_trace_threshold = 0
855
864
  t = Time.now
856
865
  @sampler.notice_first_scope_push t.to_f
@@ -861,7 +870,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
861
870
  end
862
871
 
863
872
  def test_nil_stacktrace
864
-
865
873
  @sampler.stack_trace_threshold = 2
866
874
 
867
875
  @sampler.notice_first_scope_push Time.now.to_f
@@ -875,7 +883,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
875
883
  end
876
884
 
877
885
  def test_big_sql
878
-
879
886
  @sampler.notice_first_scope_push Time.now.to_f
880
887
 
881
888
  sql = "SADJKHASDHASD KAJSDH ASKDH ASKDHASDK JASHD KASJDH ASKDJHSAKDJHAS DKJHSADKJSAH DKJASHD SAKJDH SAKDJHS"
@@ -893,9 +900,7 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
893
900
  assert sql.length <= 16384
894
901
  end
895
902
 
896
-
897
903
  def test_segment_obfuscated
898
-
899
904
  @sampler.notice_first_scope_push Time.now.to_f
900
905
  @sampler.notice_push_scope "foo"
901
906
 
@@ -910,7 +915,6 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
910
915
  @sampler.notice_pop_scope "foo"
911
916
  end
912
917
 
913
-
914
918
  def test_param_capture
915
919
  [true, false].each do |capture|
916
920
  NewRelic::Control.instance.stubs(:capture_params).returns(capture)
@@ -923,23 +927,41 @@ class NewRelic::Agent::TransactionSamplerTest < Test::Unit::TestCase
923
927
  assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
924
928
  end
925
929
  end
926
-
927
-
930
+
931
+ 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
+
928
950
  private
929
- def run_sample_trace(&proc)
951
+
952
+ def run_sample_trace
930
953
  @sampler.notice_first_scope_push Time.now.to_f
931
954
  @sampler.notice_transaction '/path', nil, {}
932
955
  @sampler.notice_push_scope "a"
933
956
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
934
957
  @sampler.notice_push_scope "ab"
935
958
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
936
- proc.call if proc
959
+ yield if block_given?
937
960
  @sampler.notice_pop_scope "ab"
938
- @sampler.notice_push_scope "lew"
961
+ @sampler.notice_push_scope "ac"
939
962
  @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
940
- @sampler.notice_pop_scope "lew"
963
+ @sampler.notice_pop_scope "ac"
941
964
  @sampler.notice_pop_scope "a"
942
965
  @sampler.notice_scope_empty
943
966
  end
944
-
945
967
  end