newrelic_rpm 3.6.2.96 → 3.6.3.103.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +33 -1
  3. data/README.md +7 -7
  4. data/lib/new_relic/agent/agent.rb +51 -22
  5. data/lib/new_relic/agent/agent_logger.rb +22 -11
  6. data/lib/new_relic/agent/configuration/defaults.rb +6 -1
  7. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -6
  8. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +1 -1
  9. data/lib/new_relic/agent/instrumentation/active_record.rb +8 -48
  10. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +9 -1
  11. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +4 -3
  12. data/lib/new_relic/agent/instrumentation/data_mapper.rb +4 -4
  13. data/lib/new_relic/agent/instrumentation/metric_frame.rb +10 -8
  14. data/lib/new_relic/agent/instrumentation/padrino.rb +32 -0
  15. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +52 -0
  16. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +56 -0
  17. data/lib/new_relic/agent/instrumentation/sinatra.rb +113 -48
  18. data/lib/new_relic/agent/new_relic_service.rb +6 -0
  19. data/lib/new_relic/agent/pipe_channel_manager.rb +13 -8
  20. data/lib/new_relic/agent/request_sampler.rb +205 -0
  21. data/lib/new_relic/agent/sampler.rb +0 -1
  22. data/lib/new_relic/agent/stats_engine/samplers.rb +0 -1
  23. data/lib/new_relic/agent/stats_engine/transactions.rb +12 -16
  24. data/lib/new_relic/agent/transaction.rb +27 -4
  25. data/lib/new_relic/agent/transaction_sample_builder.rb +47 -6
  26. data/lib/new_relic/agent/transaction_sampler.rb +0 -5
  27. data/lib/new_relic/agent.rb +17 -0
  28. data/lib/new_relic/build.rb +2 -2
  29. data/lib/new_relic/coerce.rb +3 -1
  30. data/lib/new_relic/rack/agent_hooks.rb +17 -3
  31. data/lib/new_relic/rack/browser_monitoring.rb +8 -3
  32. data/lib/new_relic/rack/error_collector.rb +2 -0
  33. data/lib/new_relic/transaction_sample/segment.rb +0 -23
  34. data/lib/new_relic/transaction_sample.rb +0 -9
  35. data/lib/new_relic/version.rb +1 -1
  36. data/test/agent_helper.rb +204 -0
  37. data/test/config/newrelic.yml +0 -1
  38. data/test/config/test_control.rb +3 -1
  39. data/test/multiverse/suites/agent_only/key_transactions_test.rb +8 -5
  40. data/test/multiverse/suites/agent_only/logging_test.rb +1 -1
  41. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +7 -8
  42. data/test/multiverse/suites/datamapper/Envfile +7 -0
  43. data/test/multiverse/suites/datamapper/datamapper_test.rb +105 -0
  44. data/test/multiverse/suites/padrino/Envfile +16 -0
  45. data/test/multiverse/suites/padrino/config/newrelic.yml +24 -0
  46. data/test/multiverse/suites/padrino/padrino_test.rb +54 -0
  47. data/test/multiverse/suites/rails/Envfile +5 -5
  48. data/test/multiverse/suites/rails/app.rb +1 -0
  49. data/test/multiverse/suites/rails/request_statistics_test.rb +118 -0
  50. data/test/multiverse/suites/sinatra/Envfile +8 -2
  51. data/test/multiverse/suites/sinatra/ignoring_test.rb +185 -0
  52. data/test/multiverse/suites/sinatra/sinatra_classic_test.rb +92 -0
  53. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +0 -3
  54. data/test/multiverse/suites/sinatra/sinatra_modular_test.rb +89 -0
  55. data/test/multiverse/suites/sinatra/sinatra_test_cases.rb +120 -0
  56. data/test/new_relic/agent/agent_logger_test.rb +149 -56
  57. data/test/new_relic/agent/agent_test.rb +23 -0
  58. data/test/new_relic/agent/agent_test_controller_test.rb +8 -1
  59. data/test/new_relic/agent/autostart_test.rb +10 -6
  60. data/test/new_relic/agent/instrumentation/action_controller_subscriber_test.rb +36 -31
  61. data/test/new_relic/agent/instrumentation/action_view_subscriber_test.rb +7 -0
  62. data/test/new_relic/agent/instrumentation/active_record_helper_test.rb +20 -4
  63. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +20 -9
  64. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +23 -19
  65. data/test/new_relic/agent/instrumentation/sequel_test.rb +118 -79
  66. data/test/new_relic/agent/instrumentation/sinatra/transaction_namer_test.rb +55 -0
  67. data/test/new_relic/agent/instrumentation/sinatra_test.rb +60 -11
  68. data/test/new_relic/agent/method_tracer_test.rb +7 -4
  69. data/test/new_relic/agent/new_relic_service_test.rb +6 -0
  70. data/test/new_relic/agent/pipe_channel_manager_test.rb +6 -2
  71. data/test/new_relic/agent/request_sampler_test.rb +159 -0
  72. data/test/new_relic/agent/stats_engine/samplers_test.rb +1 -5
  73. data/test/new_relic/agent/stats_engine_test.rb +14 -0
  74. data/test/new_relic/agent/transaction_sample_builder_test.rb +43 -6
  75. data/test/new_relic/agent/transaction_sampler_test.rb +31 -1
  76. data/test/new_relic/agent/transaction_test.rb +29 -0
  77. data/test/new_relic/agent_test.rb +7 -0
  78. data/test/new_relic/coerce_test.rb +13 -0
  79. data/test/new_relic/fake_collector.rb +31 -1
  80. data/test/new_relic/metric_spec_test.rb +14 -10
  81. data/test/new_relic/rack/agent_hooks_test.rb +9 -2
  82. data/test/new_relic/rack/browser_monitoring_test.rb +16 -7
  83. data/test/new_relic/rack/developer_mode_test.rb +7 -0
  84. data/test/new_relic/rack/error_collector_test.rb +10 -6
  85. data/test/new_relic/transaction_sample/segment_test.rb +0 -61
  86. data/test/new_relic/transaction_sample_subtest_test.rb +0 -19
  87. data/test/script/ci.sh +14 -0
  88. data/test/test_helper.rb +79 -203
  89. data.tar.gz.sig +0 -0
  90. metadata +50 -18
  91. metadata.gz.sig +0 -0
  92. data/test/multiverse/suites/datamapper/encoding_test.rb +0 -40
  93. data/test/multiverse/suites/sinatra/sinatra_test.rb +0 -143
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'test_helper'))
6
+ require 'new_relic/agent/instrumentation/sinatra/transaction_namer'
7
+
8
+ module NewRelic
9
+ module Agent
10
+ module Instrumentation
11
+ module Sinatra
12
+
13
+ class TransactionNamerTest < Test::Unit::TestCase
14
+
15
+ def test_transaction_name_for_route
16
+ env = { "newrelic.last_route" => /^\/the_route$/}
17
+ request = stub(:request_method => "GET")
18
+ result = TransactionNamer.transaction_name_for_route(env, request)
19
+ assert_equal "GET the_route", result
20
+ end
21
+
22
+ def test_transaction_name_for_route_padrino
23
+ env = {}
24
+ route = stub(:original_path => "/path/:id")
25
+ request = stub(:route_obj => route, :request_method => "GET")
26
+ result = TransactionNamer.transaction_name_for_route(env, request)
27
+
28
+ assert_equal "GET path/:id", result
29
+ end
30
+
31
+ def test_transaction_name_for_route_without_routes
32
+ assert_nil TransactionNamer.transaction_name_for_route({}, nil)
33
+ end
34
+
35
+ def test_basic_sinatra_naming
36
+ assert_transaction_name "(unknown)", "(unknown)"
37
+
38
+ # Sinatra < 1.4 style regexes
39
+ assert_transaction_name "will_boom", "^/will_boom$"
40
+ assert_transaction_name "hello/([^/?#]+)", "^/hello/([^/?#]+)$"
41
+
42
+ # Sinatra 1.4 style regexs
43
+ assert_transaction_name "will_boom", "\A/will_boom\z"
44
+ assert_transaction_name "hello/([^/?#]+)", "\A/hello/([^/?#]+)\z"
45
+ end
46
+
47
+ def assert_transaction_name(expected, original)
48
+ assert_equal expected, TransactionNamer.transaction_name(original, nil)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -6,24 +6,73 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'te
6
6
  require 'new_relic/agent/instrumentation/sinatra'
7
7
 
8
8
  class NewRelic::Agent::Instrumentation::SinatraTest < Test::Unit::TestCase
9
+
10
+ # This fake app is not an actual Sinatra app to avoid having our unit tests
11
+ # take a dependency directly on it. If you need actual Sinatra classes, go
12
+ # write the test in the multiver suite.
9
13
  class SinatraTestApp
10
- def initialize(response)
11
- @response = response
12
- end
14
+ include NewRelic::Agent::Instrumentation::Sinatra
13
15
 
14
- def dispatch!
15
- @response = response
16
+ attr_accessor :env, :request
17
+
18
+ def initialize
19
+ @env = {}
16
20
  end
21
+ end
17
22
 
18
- include NewRelic::Agent::Instrumentation::Sinatra
19
- alias dispatch_without_newrelic dispatch!
20
- alias dispatch! dispatch_with_newrelic
23
+ def setup
24
+ @app = SinatraTestApp.new
21
25
  end
22
26
 
23
27
  def test_newrelic_request_headers
24
- app = SinatraTestApp.new([200, {}, ["OK"]])
25
28
  expected_headers = {:fake => :header}
26
- app.expects(:request).returns(mock('request', :env => expected_headers))
27
- assert_equal app.send(:newrelic_request_headers), expected_headers
29
+ @app.request = mock('request', :env => expected_headers)
30
+
31
+ assert_equal @app.newrelic_request_headers, expected_headers
32
+ end
33
+
34
+ def test_process_route_with_bad_arguments
35
+ @app.stubs(:env).raises("Boo")
36
+ @app.expects(:process_route_without_newrelic).once
37
+ @app.process_route_with_newrelic
38
+ end
39
+
40
+ def test_route_eval_with_bad_params
41
+ @app.stubs(:env).raises("Boo")
42
+ @app.expects(:route_eval_without_newrelic).once
43
+ @app.route_eval_with_newrelic
44
+ end
45
+
46
+ def test_route_eval_without_last_route_doesnt_set_transaction_name
47
+ @app.stubs(:env).returns({})
48
+ @app.expects(:route_eval_without_newrelic).once
49
+ NewRelic::Agent.expects(:set_transaction_name).never
50
+ @app.route_eval_with_newrelic
51
+ end
52
+
53
+ def test_injects_middleware
54
+ SinatraTestApp.stubs(:middleware).returns([])
55
+
56
+ SinatraTestApp.expects(:build_without_newrelic).once
57
+ SinatraTestApp.expects(:use).at_least(3)
58
+
59
+ SinatraTestApp.build_with_newrelic(@app)
60
+ end
61
+
62
+ def test_doesnt_inject_already_existing_middleware
63
+ default_middlewares = SinatraTestApp.newrelic_middlewares
64
+ # mock up the return value of Sinatra's #middleware method, which returns an
65
+ # Array of Arrays.
66
+ middleware_info = default_middlewares.map { |m| [m] }
67
+ SinatraTestApp.stubs(:middleware).returns(middleware_info)
68
+
69
+ SinatraTestApp.expects(:build_without_newrelic).once
70
+ SinatraTestApp.expects(:use).never
71
+
72
+ SinatraTestApp.build_with_newrelic(@app)
73
+ end
74
+
75
+ def assert_transaction_name(expected, original)
76
+ assert_equal expected, NewRelic::Agent::Instrumentation::Sinatra::TransactionNamer.transaction_name(original, nil)
28
77
  end
29
78
  end
@@ -69,12 +69,11 @@ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
69
69
  @stats_engine.clear_stats
70
70
  @scope_listener = NewRelic::Agent::MockScopeListener.new
71
71
  @old_sampler = NewRelic::Agent.instance.transaction_sampler
72
- @stats_engine.transaction_sampler = @scope_listener
72
+ NewRelic::Agent.instance.stubs(:transaction_sampler).returns(@scope_listener)
73
73
  super
74
74
  end
75
75
 
76
76
  def teardown
77
- @stats_engine.transaction_sampler = @old_sampler
78
77
  @stats_engine.clear_stats
79
78
  begin
80
79
  self.class.remove_method_tracer :method_to_be_traced, @metric_name if @metric_name
@@ -196,15 +195,19 @@ class NewRelic::Agent::MethodTracerTest < Test::Unit::TestCase
196
195
  def test_nested_scope_tracer
197
196
  Insider.add_method_tracer :catcher, "catcher", :push_scope => true
198
197
  Insider.add_method_tracer :thrower, "thrower", :push_scope => true
199
- sampler = NewRelic::Agent.instance.transaction_sampler
198
+
199
+ # This expects to use the real transaction sampler, so stub it back
200
+ NewRelic::Agent.instance.stubs(:transaction_sampler).returns(@old_sampler)
201
+
200
202
  mock = Insider.new(@stats_engine)
201
203
  mock.catcher(0)
202
204
  mock.catcher(5)
205
+
203
206
  stats = @stats_engine.get_stats("catcher")
204
207
  assert_equal 2, stats.call_count
205
208
  stats = @stats_engine.get_stats("thrower")
206
209
  assert_equal 6, stats.call_count
207
- sample = sampler.harvest
210
+ sample = @old_sampler.harvest
208
211
  assert_not_nil sample
209
212
  end
210
213
 
@@ -259,6 +259,12 @@ class NewRelicServiceTest < Test::Unit::TestCase
259
259
  assert_equal 'explain this', response
260
260
  end
261
261
 
262
+ def test_analytic_event_data
263
+ @http_handle.respond_to(:analytic_event_data, 'some analytic events')
264
+ response = @service.analytic_event_data([])
265
+ assert_equal 'some analytic events', response
266
+ end
267
+
262
268
 
263
269
  # Thread profiling only available in certain versions
264
270
  if NewRelic::Agent::ThreadProfiler.is_supported?
@@ -103,9 +103,13 @@ class NewRelic::Agent::PipeChannelManagerTest < Test::Unit::TestCase
103
103
  assert_equal(2, NewRelic::Agent.agent.error_collector.errors.size)
104
104
  end
105
105
 
106
+ def pipe_finished?(id)
107
+ (!NewRelic::Agent::PipeChannelManager.channels[id] ||
108
+ NewRelic::Agent::PipeChannelManager.channels[id].closed?)
109
+ end
110
+
106
111
  def assert_pipe_finished(id)
107
- assert(!NewRelic::Agent::PipeChannelManager.channels[id] ||
108
- NewRelic::Agent::PipeChannelManager.channels[id].closed?,
112
+ assert(pipe_finished?(id),
109
113
  "Expected pipe with ID #{id} to be nil or closed")
110
114
  end
111
115
 
@@ -0,0 +1,159 @@
1
+ # -*- ruby -*-
2
+ # encoding: utf-8
3
+ # This file is distributed under New Relic's license terms.
4
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
7
+ require 'new_relic/agent/request_sampler'
8
+
9
+ class NewRelic::Agent::RequestSamplerTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ freeze_time
13
+ @event_listener = NewRelic::Agent::EventListener.new
14
+ @sampler = NewRelic::Agent::RequestSampler.new( @event_listener )
15
+ end
16
+
17
+ def test_samples_on_transaction_finished_event
18
+ with_sampler_config do
19
+ advance_time( 0.60 )
20
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.095 )
21
+
22
+ assert_equal 1, @sampler.samples.length
23
+ end
24
+ end
25
+
26
+ def test_samples_on_transaction_finished_event_include_options
27
+ with_sampler_config do
28
+ advance_time( 0.60 )
29
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.095, :foo => :bar )
30
+
31
+ assert_equal :bar, @sampler.samples.first[:foo]
32
+ end
33
+ end
34
+
35
+ def test_samples_at_the_correct_rate
36
+ with_sampler_config( :'request_sampler.sample_rate_ms' => 50 ) do
37
+ # 240 requests over 6 seconds => 120 samples
38
+ # with_debug_logging do
39
+ 241.times do
40
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
41
+ advance_time( 0.025 )
42
+ end
43
+ # end
44
+
45
+ assert_equal 120, @sampler.samples.length
46
+ @sampler.samples.each do |sample|
47
+ assert_is_valid_transaction_sample( sample )
48
+ end
49
+ @sampler.samples.each_with_index do |sample, i|
50
+ next if i.zero?
51
+ seconds = sample['timestamp'] - @sampler.samples[i-1]['timestamp']
52
+ assert_in_delta( seconds, 0.050, 0.001 )
53
+ end
54
+ end
55
+ end
56
+
57
+ def test_downsamples_and_reduces_sample_rate_when_throttled
58
+ with_sampler_config( :'request_sampler.sample_rate_ms' => 50 ) do
59
+ 240.times do
60
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
61
+ advance_time( 0.025 )
62
+ end
63
+
64
+ @sampler.throttle( 2 )
65
+
66
+ 241.times do
67
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
68
+ advance_time( 0.025 )
69
+ end
70
+
71
+ assert_equal 120, @sampler.samples.length
72
+ @sampler.samples.each do |sample|
73
+ assert_is_valid_transaction_sample( sample )
74
+ end
75
+ @sampler.samples.each_with_index do |sample, i|
76
+ next if i.zero?
77
+ seconds = sample['timestamp'] - @sampler.samples[i-1]['timestamp']
78
+ assert_in_delta( seconds, 0.100, 0.026 )
79
+ end
80
+ end
81
+ end
82
+
83
+ def test_downsamples_and_reduces_sample_rate_when_throttled_multiple_times
84
+ with_sampler_config( :'request_sampler.sample_rate_ms' => 50 ) do
85
+ 240.times do
86
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
87
+ advance_time( 0.025 )
88
+ end
89
+
90
+ @sampler.throttle( 2 )
91
+
92
+ 240.times do
93
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
94
+ advance_time( 0.025 )
95
+ end
96
+
97
+ @sampler.throttle( 3 )
98
+
99
+ 241.times do
100
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
101
+ advance_time( 0.025 )
102
+ end
103
+
104
+ assert_equal 120, @sampler.samples.length
105
+ @sampler.samples.each do |sample|
106
+ assert_is_valid_transaction_sample( sample )
107
+ end
108
+ @sampler.samples.each_with_index do |sample, i|
109
+ next if i.zero?
110
+ seconds = sample['timestamp'] - @sampler.samples[i-1]['timestamp']
111
+ assert_in_delta( seconds, 0.150, 0.051 )
112
+ end
113
+ end
114
+ end
115
+
116
+ def test_can_disable_sampling
117
+ with_sampler_config( :'request_sampler.enabled' => false ) do
118
+ @event_listener.notify( :transaction_finished, 'Controller/foo/bar', 0.200 )
119
+ assert @sampler.samples.empty?
120
+ end
121
+ end
122
+
123
+ def test_allows_sample_rates_as_frequent_as_25ms
124
+ with_config( :'request_sampler.sample_rate_ms' => 25 ) do
125
+ assert_equal 25, @sampler.normal_sample_rate_ms
126
+ end
127
+ end
128
+
129
+ def test_resets_sample_rates_more_frequent_than_25ms_to_25ms
130
+ with_config( :'request_sampler.sample_rate_ms' => 1 ) do
131
+ assert_equal 25, @sampler.normal_sample_rate_ms
132
+ end
133
+ end
134
+
135
+
136
+ #
137
+ # Helpers
138
+ #
139
+
140
+ def with_sampler_config(options = {})
141
+ defaults =
142
+ {
143
+ :'request_sampler.enabled' => true,
144
+ :'request_sampler.sample_rate_ms' => 50
145
+ }
146
+
147
+ defaults.merge!(options)
148
+ with_config(defaults) do
149
+ @event_listener.notify( :finished_configuring )
150
+ yield
151
+ end
152
+ end
153
+
154
+ def assert_is_valid_transaction_sample( sample )
155
+ assert_kind_of Hash, sample
156
+ assert_equal 'Transaction', sample['type']
157
+ end
158
+
159
+ end
@@ -27,7 +27,6 @@ class NewRelic::Agent::StatsEngine::SamplersTest < Test::Unit::TestCase
27
27
  samplers.add_harvest_sampler(sampler)
28
28
 
29
29
  assert_equal [sampler], samplers.harvest_samplers
30
- assert_equal samplers, sampler.stats_engine
31
30
  end
32
31
 
33
32
  def test_cannot_add_harvest_sampler_twice
@@ -70,7 +69,6 @@ class NewRelic::Agent::StatsEngine::SamplersTest < Test::Unit::TestCase
70
69
 
71
70
  def test_memory__default
72
71
  s = NewRelic::Agent::Samplers::MemorySampler.new
73
- s.stats_engine = @stats_engine
74
72
  s.poll
75
73
  s.poll
76
74
  s.poll
@@ -83,7 +81,6 @@ class NewRelic::Agent::StatsEngine::SamplersTest < Test::Unit::TestCase
83
81
  return if RUBY_PLATFORM =~ /darwin/
84
82
  NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'linux'
85
83
  s = NewRelic::Agent::Samplers::MemorySampler.new
86
- s.stats_engine = @stats_engine
87
84
  s.poll
88
85
  s.poll
89
86
  s.poll
@@ -97,9 +94,8 @@ class NewRelic::Agent::StatsEngine::SamplersTest < Test::Unit::TestCase
97
94
  NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'solaris'
98
95
  NewRelic::Agent::Samplers::MemorySampler::ShellPS.any_instance.stubs(:get_memory).returns 999
99
96
  s = NewRelic::Agent::Samplers::MemorySampler.new
100
- s.stats_engine = @stats_engine
101
97
  s.poll
102
- stats = s.stats_engine.get_stats_no_scope("Memory/Physical")
98
+ stats = @stats_engine.get_stats_no_scope("Memory/Physical")
103
99
  assert_equal 1, stats.call_count
104
100
  assert_equal 999, stats.total_call_time
105
101
  end
@@ -174,6 +174,20 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
174
174
  assert_equal 10, scope1.children_time.round
175
175
  end
176
176
 
177
+ def test_sampler_enabling
178
+ assert_sampler_enabled_with(true, :'transaction_tracer.enabled' => true, :developer_mode => false)
179
+ assert_sampler_enabled_with(true, :'transaction_tracer.enabled' => false, :developer_mode => true)
180
+ assert_sampler_enabled_with(true, :'transaction_tracer.enabled' => true, :developer_mode => true)
181
+
182
+ assert_sampler_enabled_with(false, :'transaction_tracer.enabled' => false, :developer_mode => false)
183
+ end
184
+
185
+ def assert_sampler_enabled_with(expected, opts={})
186
+ with_config(opts) do
187
+ assert_equal expected, @engine.sampler_enabled?
188
+ end
189
+ end
190
+
177
191
  private
178
192
  def check_time_approximate(expected, actual)
179
193
  assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
@@ -7,6 +7,7 @@ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper
7
7
  class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
8
8
 
9
9
  def setup
10
+ freeze_time
10
11
  @builder = NewRelic::Agent::TransactionSampleBuilder.new
11
12
  end
12
13
 
@@ -73,27 +74,27 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
73
74
  # this is really a test for transaction sample
74
75
  def test_omit_segments_with
75
76
  build_segment "Controller/my_controller/index" do
76
- sleep 0.010
77
+ advance_time 0.010
77
78
 
78
79
  build_segment "Rails/Application Code Loading" do
79
- sleep 0.020
80
+ advance_time 0.020
80
81
 
81
82
  build_segment "foo/bar" do
82
- sleep 0.010
83
+ advance_time 0.010
83
84
  end
84
85
  end
85
86
 
86
87
  build_segment "a" do
87
88
  build_segment "ab"
88
- sleep 0.010
89
+ advance_time 0.010
89
90
  end
90
91
  build_segment "b" do
91
92
  build_segment "ba"
92
- sleep 0.05
93
+ advance_time 0.05
93
94
  build_segment "bb"
94
95
  build_segment "bc" do
95
96
  build_segment "bca"
96
- sleep 0.05
97
+ advance_time 0.05
97
98
  end
98
99
  end
99
100
  build_segment "c"
@@ -169,6 +170,42 @@ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
169
170
  end
170
171
  end
171
172
 
173
+ def test_trace_has_valid_durations_when_segments_limited
174
+ with_config(:'transaction_tracer.limit_segments' => 3) do
175
+ build_segment "parent" do
176
+ advance_time 1
177
+ build_segment "child-0.0" do
178
+ advance_time 1
179
+ build_segment "child-0.1" do
180
+ advance_time 1
181
+ end
182
+ end
183
+ advance_time 1
184
+ build_segment "child-1.0" do
185
+ advance_time 1
186
+ build_segment "child-1.1" do
187
+ advance_time 1
188
+ end
189
+ end
190
+ end
191
+
192
+ sample = @builder.sample
193
+ assert_equal(3, sample.count_segments)
194
+
195
+ segment_names = []
196
+ segment_durations = []
197
+ sample.each_segment do |s|
198
+ if s != sample.root_segment
199
+ segment_names << s.metric_name
200
+ segment_durations << s.duration
201
+ end
202
+ end
203
+
204
+ assert_equal(["parent", "child-0.0", "child-0.1"], segment_names)
205
+ assert_equal([6.0, 2.0, 1.0], segment_durations)
206
+ end
207
+ end
208
+
172
209
  def test_finish_trace_records_threshold
173
210
  NewRelic::Agent::TransactionInfo.get.stubs(:transaction_trace_threshold) \
174
211
  .returns(2.0)