newrelic_rpm 3.0.1 → 3.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (74) hide show
  1. data/CHANGELOG +2 -3
  2. data/README.rdoc +3 -3
  3. data/lib/new_relic/agent.rb +19 -7
  4. data/lib/new_relic/agent/agent.rb +83 -19
  5. data/lib/new_relic/agent/beacon_configuration.rb +8 -12
  6. data/lib/new_relic/agent/browser_monitoring.rb +8 -8
  7. data/lib/new_relic/agent/error_collector.rb +13 -13
  8. data/lib/new_relic/agent/instrumentation.rb +9 -0
  9. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +10 -2
  10. data/lib/new_relic/agent/instrumentation/metric_frame.rb +41 -35
  11. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +92 -0
  12. data/lib/new_relic/agent/method_tracer.rb +0 -2
  13. data/lib/new_relic/agent/shim_agent.rb +2 -0
  14. data/lib/new_relic/agent/stats_engine/metric_stats.rb +89 -60
  15. data/lib/new_relic/agent/stats_engine/transactions.rb +1 -1
  16. data/lib/new_relic/agent/worker_loop.rb +1 -1
  17. data/lib/new_relic/collection_helper.rb +0 -2
  18. data/lib/new_relic/control/class_methods.rb +25 -12
  19. data/lib/new_relic/control/logging_methods.rb +30 -17
  20. data/lib/new_relic/data_serialization.rb +81 -0
  21. data/lib/new_relic/local_environment.rb +1 -1
  22. data/lib/new_relic/metric_data.rb +9 -5
  23. data/lib/new_relic/metric_spec.rb +7 -1
  24. data/lib/new_relic/rack/browser_monitoring.rb +1 -7
  25. data/lib/new_relic/stats.rb +4 -0
  26. data/lib/new_relic/transaction_analysis.rb +45 -88
  27. data/lib/new_relic/transaction_analysis/segment_summary.rb +47 -0
  28. data/lib/new_relic/transaction_sample.rb +15 -332
  29. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  30. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  31. data/lib/new_relic/transaction_sample/segment.rb +250 -0
  32. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  33. data/lib/new_relic/version.rb +3 -3
  34. data/newrelic.yml +3 -3
  35. data/newrelic_rpm.gemspec +27 -4
  36. data/test/active_record_fixtures.rb +31 -13
  37. data/test/new_relic/agent/agent/start_worker_thread_test.rb +1 -3
  38. data/test/new_relic/agent/agent_test.rb +73 -28
  39. data/test/new_relic/agent/agent_test_controller_test.rb +11 -10
  40. data/test/new_relic/agent/beacon_configuration_test.rb +37 -20
  41. data/test/new_relic/agent/browser_monitoring_test.rb +17 -28
  42. data/test/new_relic/agent/error_collector/notice_error_test.rb +9 -7
  43. data/test/new_relic/agent/error_collector_test.rb +6 -7
  44. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +12 -5
  45. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +195 -0
  46. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +60 -58
  47. data/test/new_relic/agent/instrumentation/queue_time_test.rb +14 -0
  48. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  49. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +0 -1
  50. data/test/new_relic/agent/method_tracer_test.rb +8 -8
  51. data/test/new_relic/agent/sampler_test.rb +19 -0
  52. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  53. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  54. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +1 -0
  55. data/test/new_relic/agent/stats_engine/samplers_test.rb +4 -3
  56. data/test/new_relic/agent/{stats_engine/stats_engine_test.rb → stats_engine_test.rb} +8 -8
  57. data/test/new_relic/agent/transaction_sampler_test.rb +1 -1
  58. data/test/new_relic/agent/worker_loop_test.rb +2 -2
  59. data/test/new_relic/control/class_methods_test.rb +62 -0
  60. data/test/new_relic/control/logging_methods_test.rb +157 -0
  61. data/test/new_relic/control_test.rb +10 -10
  62. data/test/new_relic/data_serialization_test.rb +50 -0
  63. data/test/new_relic/local_environment_test.rb +13 -13
  64. data/test/new_relic/metric_data_test.rb +125 -0
  65. data/test/new_relic/metric_spec_test.rb +8 -0
  66. data/test/new_relic/transaction_analysis/segment_summary_test.rb +77 -0
  67. data/test/new_relic/transaction_analysis_test.rb +121 -0
  68. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  69. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  70. data/test/new_relic/transaction_sample/segment_test.rb +454 -0
  71. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  72. data/test/new_relic/transaction_sample_test.rb +51 -0
  73. data/test/test_helper.rb +4 -14
  74. metadata +32 -7
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ require 'new_relic/transaction_analysis'
3
+ class NewRelic::TransactionAnalysisTest < Test::Unit::TestCase
4
+ include NewRelic::TransactionAnalysis
5
+
6
+ # these are mostly stub tests just making sure that the API doesn't
7
+ # change if anyone ever needs to modify it.
8
+
9
+ def test_database_time
10
+ self.expects(:time_percentage).with(/^Database\/.*/)
11
+ database_time
12
+ end
13
+
14
+ def test_render_time
15
+ self.expects(:time_percentage).with(/^View\/.*/)
16
+ render_time
17
+ end
18
+
19
+ def test_breakdown_data_default
20
+ root_segment = mock('root_segment')
21
+ # this is for 1.9 compatibility - calling each on something calls
22
+ # #to_a on it - which is fun and exciting
23
+ root_segment.stubs(:to_a).returns([root_segment])
24
+ other_segment = mock('other_segment')
25
+ # this is for 1.9 compatibility - calling each on something calls
26
+ # #to_a on it - which is fun and exciting
27
+ other_segment.stubs(:to_a).returns([other_segment])
28
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
29
+ other_segment.expects(:duration).returns(0.1)
30
+ other_segment.expects(:exclusive_duration).returns(0.1)
31
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
32
+ self.expects(:root_segment).twice.returns(root_segment)
33
+ self.expects(:duration).returns(0.1)
34
+ data = breakdown_data
35
+ assert_equal 'Controller/foo', data[0].metric_name
36
+ end
37
+
38
+ # kind of a hairy test, we're making sure that the data is truncated
39
+ # to one element by the limit
40
+ def test_breakdown_data_limit_one
41
+ root_segment = mock('root_segment')
42
+ # this is for 1.9 compatibility - calling each on something calls
43
+ # #to_a on it - which is fun and exciting
44
+ root_segment.stubs(:to_a).returns([root_segment])
45
+ other_segment = mock('other_segment')
46
+ # this is for 1.9 compatibility - calling each on something calls
47
+ # #to_a on it - which is fun and exciting
48
+ other_segment.stubs(:to_a).returns([other_segment])
49
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
50
+ other_segment.expects(:duration).returns(0.1)
51
+ other_segment.expects(:exclusive_duration).returns(0.1)
52
+ yet_another = mock('another segment')
53
+ # this is for 1.9 compatibility - calling each on something calls
54
+ # #to_a on it - which is fun and exciting
55
+ yet_another.stubs(:to_a).returns([yet_another])
56
+ yet_another.expects(:metric_name).twice.returns('Controller/bar')
57
+ yet_another.expects(:duration).returns(0.2)
58
+ yet_another.expects(:exclusive_duration).returns(0.2)
59
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment, yet_another)
60
+ self.expects(:root_segment).times(3).returns(root_segment)
61
+ self.expects(:duration).returns(0.1)
62
+ data = breakdown_data(1)
63
+ assert_equal 1, data.size
64
+ assert_equal 'Controller/bar', data[0].metric_name
65
+ end
66
+
67
+ def test_breakdown_data_remainder
68
+ root_segment = mock('root_segment')
69
+ # this is for 1.9 compatibility - calling each on something calls
70
+ # #to_a on it - which is fun and exciting
71
+ root_segment.stubs(:to_a).returns([root_segment])
72
+ other_segment = mock('other_segment')
73
+ # this is for 1.9 compatibility - calling each on something calls
74
+ # #to_a on it - which is fun and exciting
75
+ other_segment.stubs(:to_a).returns([other_segment])
76
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
77
+ other_segment.expects(:duration).returns(0.1)
78
+ other_segment.expects(:exclusive_duration).returns(0.1)
79
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
80
+ self.expects(:root_segment).twice.returns(root_segment)
81
+ self.expects(:duration).returns(0.2)
82
+ data = breakdown_data
83
+ assert_equal 2, data.size
84
+ assert_equal 'Controller/foo', data[0].metric_name
85
+ assert_equal 'Remainder', data[1].metric_name
86
+ end
87
+
88
+ def test_sql_segments_default
89
+ root_segment = mock('root_segment') # a segment with no data
90
+ root_segment.expects(:[]).with(:sql).returns(false)
91
+ root_segment.expects(:[]).with(:sql_obfuscated).returns(false)
92
+ root_segment.expects(:[]).with(:key).returns(nil)
93
+ # this is for 1.9 compatibility - calling each on something calls
94
+ # #to_a on it - which is fun and exciting
95
+ root_segment.stubs(:to_a).returns([root_segment])
96
+ other_segment = mock('other_segment') # a sql segment
97
+ other_segment.expects(:[]).with(:sql).returns(true)
98
+ # this is for 1.9 compatibility - calling each on something calls
99
+ # #to_a on it - which is fun and exciting
100
+ other_segment.stubs(:to_a).returns([other_segment])
101
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
102
+ assert_equal [other_segment], sql_segments
103
+ end
104
+
105
+ def test_time_percentage_default
106
+ root_segment = mock('root_segment')
107
+ root_segment.expects(:metric_name).returns('ROOT')
108
+ # this is for 1.9 compatibility - calling each on something calls
109
+ # #to_a on it - which is fun and exciting
110
+ root_segment.stubs(:to_a).returns([root_segment])
111
+ other_segment = mock('other_segment')
112
+ other_segment.expects(:metric_name).returns('Controller/foo')
113
+ other_segment.expects(:duration).returns(0.1)
114
+ # this is for 1.9 compatibility - calling each on something calls
115
+ # #to_a on it - which is fun and exciting
116
+ other_segment.stubs(:to_a).returns([other_segment])
117
+ self.expects(:duration).returns(0.2)
118
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
119
+ assert_equal 50.0, time_percentage(/Controller\/.*/)
120
+ end
121
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/composite_segment'
3
+ class NewRelic::TransactionSample::CompositeSegmentTest < Test::Unit::TestCase
4
+ def test_composite_segment_creation
5
+ fake_segment = mock_segment
6
+ NewRelic::TransactionSample::CompositeSegment.new([fake_segment])
7
+ end
8
+
9
+ def test_detail_segments_equals
10
+ fake_segment = mock_segment
11
+ cs = NewRelic::TransactionSample::CompositeSegment.new([fake_segment])
12
+
13
+ # note that this is a bare mock
14
+ # nothing should be called on it, for now
15
+ other_fake_segment = mock('other segment')
16
+ cs.detail_segments = [other_fake_segment]
17
+
18
+ assert_equal cs.detail_segments, [other_fake_segment]
19
+ end
20
+
21
+ private
22
+
23
+ @@seg_count = 0
24
+ def mock_segment
25
+ @@seg_count += 1
26
+ segment = mock('segment ' + @@seg_count.to_s)
27
+ segment.expects(:entry_timestamp).returns(Time.now)
28
+ # note the following 'twice' - different than SummarySegment
29
+ segment.expects(:exit_timestamp).returns(Time.now).twice
30
+ segment.expects(:metric_name).returns('Custom/test/metric')
31
+ segment.expects(:called_segments).returns([])
32
+ segment
33
+ end
34
+ end
35
+
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/fake_segment'
3
+ class NewRelic::TransactionSample::FakeSegmentTest < Test::Unit::TestCase
4
+ def test_fake_segment_creation
5
+ assert_nothing_raised do
6
+ NewRelic::TransactionSample::FakeSegment.new(0.1, 'Custom/test/metric', nil)
7
+ end
8
+ end
9
+
10
+ def test_parent_segment
11
+ # should be public in this class, but not in the parent class
12
+ s = NewRelic::TransactionSample::FakeSegment.new(0.1, 'Custom/test/metric', nil)
13
+ s.parent_segment = 'foo'
14
+ assert_equal('foo', s.instance_eval { @parent_segment } )
15
+ end
16
+ end
17
+
@@ -0,0 +1,454 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/segment'
3
+ class NewRelic::TransactionSample::SegmentTest < Test::Unit::TestCase
4
+ def test_segment_creation
5
+ # basic smoke test
6
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
7
+ assert_equal NewRelic::TransactionSample::Segment, s.class
8
+ end
9
+
10
+ def test_readers
11
+ t = Time.now
12
+ s = NewRelic::TransactionSample::Segment.new(t, 'Custom/test/metric', nil)
13
+ assert_equal(t, s.entry_timestamp)
14
+ assert_equal(nil, s.exit_timestamp)
15
+ assert_equal(nil, s.parent_segment)
16
+ assert_equal('Custom/test/metric', s.metric_name)
17
+ assert_equal(s.object_id, s.segment_id)
18
+ end
19
+
20
+ def test_end_trace
21
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
22
+ t = Time.now
23
+ s.end_trace(t)
24
+ assert_equal(t, s.exit_timestamp)
25
+ end
26
+
27
+ def test_add_called_segment
28
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
29
+ assert_equal [], s.called_segments
30
+ fake_segment = mock('segment')
31
+ fake_segment.expects(:parent_segment=).with(s)
32
+ s.add_called_segment(fake_segment)
33
+ assert_equal([fake_segment], s.called_segments)
34
+ end
35
+
36
+ def test_to_s
37
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
38
+ s.expects(:to_debug_str).with(0)
39
+ s.to_s
40
+ end
41
+
42
+ def test_to_json
43
+ t = Time.now
44
+ s = NewRelic::TransactionSample::Segment.new(t, 'Custom/test/metric', nil)
45
+ assert_equal({ :entry_timestamp => t, :exit_timestamp => nil, :metric_name => 'Custom/test/metric', :segment_id => s.object_id }.to_json, s.to_json)
46
+ end
47
+
48
+ def test_path_string
49
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
50
+ assert_equal("Custom/test/metric[]", s.path_string)
51
+
52
+ fake_segment = mock('segment')
53
+ fake_segment.expects(:parent_segment=).with(s)
54
+ fake_segment.expects(:path_string).returns('Custom/other/metric[]')
55
+
56
+
57
+ s.add_called_segment(fake_segment)
58
+ assert_equal("Custom/test/metric[Custom/other/metric[]]", s.path_string)
59
+ end
60
+
61
+ def test_to_s_compact
62
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
63
+ assert_equal("Custom/test/metric", s.to_s_compact)
64
+
65
+ fake_segment = mock('segment')
66
+ fake_segment.expects(:parent_segment=).with(s)
67
+ fake_segment.expects(:to_s_compact).returns('Custom/other/metric')
68
+ s.add_called_segment(fake_segment)
69
+
70
+ assert_equal("Custom/test/metric{Custom/other/metric}", s.to_s_compact)
71
+ end
72
+
73
+ def test_to_debug_str_basic
74
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
75
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< n/a Custom/test/metric\n", s.to_debug_str(0))
76
+ end
77
+
78
+ def test_to_debug_str_with_params
79
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
80
+ s.params = {:whee => 'a param'}
81
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n -whee : a param\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
82
+ end
83
+
84
+ def test_to_debug_str_closed
85
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
86
+ s.end_trace(0.1)
87
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< 100 ms Custom/test/metric\n", s.to_debug_str(0))
88
+ end
89
+
90
+ def test_to_debug_str_closed_with_nonnumeric
91
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
92
+ s.end_trace("0.1")
93
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< 0.1 Custom/test/metric\n", s.to_debug_str(0))
94
+ end
95
+
96
+ def test_to_debug_str_one_child
97
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
98
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/other', nil))
99
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << n/a Custom/test/other\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
100
+ # try closing it
101
+ s.called_segments.first.end_trace(0.15)
102
+ s.end_trace(0.2)
103
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << 150 ms Custom/test/other\n<< 200 ms Custom/test/metric\n", s.to_debug_str(0))
104
+ end
105
+
106
+ def test_to_debug_str_multichild
107
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
108
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/other', nil))
109
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.11, 'Custom/test/extra', nil))
110
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << n/a Custom/test/other\n >> 110 ms [Segment] Custom/test/extra \n << n/a Custom/test/extra\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
111
+ ending = 0.12
112
+ s.called_segments.each { |x| x.end_trace(ending += 0.01) }
113
+ s.end_trace(0.2)
114
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << 130 ms Custom/test/other\n >> 110 ms [Segment] Custom/test/extra \n << 140 ms Custom/test/extra\n<< 200 ms Custom/test/metric\n", s.to_debug_str(0))
115
+ end
116
+
117
+ def test_to_debug_str_nested
118
+ inner = NewRelic::TransactionSample::Segment.new(0.2, 'Custom/test/inner', nil)
119
+ middle = NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/middle', nil)
120
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
121
+ middle.add_called_segment(inner)
122
+ s.add_called_segment(middle)
123
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/middle \n >> 200 ms [Segment] Custom/test/inner \n << n/a Custom/test/inner\n << n/a Custom/test/middle\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
124
+
125
+ # close them
126
+ inner.end_trace(0.21)
127
+ middle.end_trace(0.22)
128
+ s.end_trace(0.23)
129
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/middle \n >> 200 ms [Segment] Custom/test/inner \n << 210 ms Custom/test/inner\n << 220 ms Custom/test/middle\n<< 230 ms Custom/test/metric\n", s.to_debug_str(0))
130
+ end
131
+
132
+ def test_called_segments_default
133
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
134
+ assert_equal([], s.called_segments)
135
+ end
136
+
137
+ def test_called_segments_with_segments
138
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
139
+ fake_segment = mock('segment')
140
+ fake_segment.expects(:parent_segment=).with(s)
141
+ s.add_called_segment(fake_segment)
142
+
143
+ assert_equal([fake_segment], s.called_segments)
144
+ end
145
+
146
+ def test_duration
147
+ fake_entry_timestamp = mock('entry timestamp')
148
+ fake_exit_timestamp = mock('exit timestamp')
149
+ fake_result = mock('numeric')
150
+ fake_exit_timestamp.expects(:-).with(fake_entry_timestamp).returns(fake_result)
151
+ fake_result.expects(:to_f).returns(0.5)
152
+
153
+ s = NewRelic::TransactionSample::Segment.new(fake_entry_timestamp, 'Custom/test/metric', nil)
154
+ s.end_trace(fake_exit_timestamp)
155
+ assert_equal(0.5, s.duration)
156
+ end
157
+
158
+ def test_exclusive_duration_no_children
159
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
160
+ s.expects(:duration).returns(0.5)
161
+ assert_equal(0.5, s.exclusive_duration)
162
+ end
163
+
164
+ def test_exclusive_duration_with_children
165
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
166
+
167
+ s.expects(:duration).returns(0.5)
168
+
169
+ fake_segment = mock('segment')
170
+ fake_segment.expects(:parent_segment=).with(s)
171
+ fake_segment.expects(:duration).returns(0.1)
172
+
173
+ s.add_called_segment(fake_segment)
174
+
175
+ assert_equal(0.4, s.exclusive_duration)
176
+ end
177
+
178
+ def test_count_segments_default
179
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
180
+ assert_equal(1, s.count_segments)
181
+ end
182
+
183
+ def test_count_segments_with_children
184
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
185
+
186
+ fake_segment = mock('segment')
187
+ fake_segment.expects(:parent_segment=).with(s)
188
+ fake_segment.expects(:count_segments).returns(1)
189
+
190
+ s.add_called_segment(fake_segment)
191
+
192
+ assert_equal(2, s.count_segments)
193
+ end
194
+
195
+ def test_truncate_returns_number_of_elements
196
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
197
+ assert_equal(1, s.truncate(1))
198
+ dup = s.dup
199
+ s.called_segments = [dup]
200
+ assert_equal(2, s.truncate(2))
201
+
202
+ s.called_segments = [dup, dup]
203
+ assert_equal(3, s.truncate(3))
204
+ end
205
+
206
+
207
+ def test_truncate_default
208
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
209
+
210
+ assert_equal(1, s.truncate(1))
211
+ end
212
+
213
+ def test_truncate_with_a_child
214
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
215
+
216
+ fake_segment = mock('segment')
217
+ fake_segment.expects(:parent_segment=).with(s)
218
+ fake_segment.expects(:truncate).with(1).returns(1)
219
+
220
+ s.add_called_segment(fake_segment)
221
+
222
+ assert_equal(2, s.truncate(2))
223
+ assert_equal([fake_segment], s.called_segments)
224
+
225
+ assert_equal(1, s.truncate(1))
226
+ assert_equal([], s.called_segments)
227
+ end
228
+
229
+ def test_truncate_with_multiple_children
230
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
231
+
232
+ fake_segment = mock('segment')
233
+ fake_segment.expects(:truncate).with(2).returns(1)
234
+
235
+ other_segment = mock('other segment')
236
+ other_segment.expects(:truncate).with(1).returns(1)
237
+
238
+ s.called_segments = [fake_segment, other_segment]
239
+ assert_equal(3, s.truncate(3))
240
+ assert_equal([fake_segment, other_segment], s.called_segments)
241
+ end
242
+
243
+ def test_truncate_removes_elements
244
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
245
+
246
+ fake_segment = mock('segment')
247
+ fake_segment.expects(:truncate).with(1).returns(1)
248
+
249
+ other_segment = mock('other segment')
250
+
251
+ s.called_segments = [fake_segment, other_segment]
252
+ assert_equal(2, s.truncate(2))
253
+ assert_equal([fake_segment], s.called_segments)
254
+ end
255
+
256
+ def test_key_equals
257
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
258
+ # doing this to hold the reference to the hash
259
+ params = {}
260
+ s.params = params
261
+ assert_equal(params, s.params)
262
+
263
+ # should delegate to the same hash we have above
264
+ s[:foo] = 'correct'
265
+
266
+ assert_equal('correct', params[:foo])
267
+ end
268
+
269
+ def test_key
270
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
271
+ s.params = {:foo => 'correct'}
272
+ assert_equal('correct', s[:foo])
273
+ end
274
+
275
+ def test_params
276
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
277
+
278
+ # should have a default value
279
+ assert_equal(nil, s.instance_eval { @params })
280
+ assert_equal({}, s.params)
281
+
282
+ # should otherwise take the value from the @params var
283
+ s.instance_eval { @params = {:foo => 'correct'} }
284
+ assert_equal({:foo => 'correct'}, s.params)
285
+ end
286
+
287
+ def test_each_segment_default
288
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
289
+ # in the base case it just yields the block to itself
290
+ count = 0
291
+ s.each_segment do |x|
292
+ count += 1
293
+ assert_equal(s, x)
294
+ end
295
+ # should only run once
296
+ assert_equal(1, count)
297
+ end
298
+
299
+ def test_each_segment_with_children
300
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
301
+
302
+ fake_segment = mock('segment')
303
+ fake_segment.expects(:parent_segment=).with(s)
304
+ fake_segment.expects(:each_segment).yields(fake_segment)
305
+
306
+ s.add_called_segment(fake_segment)
307
+
308
+ count = 0
309
+ s.each_segment do |x|
310
+ count += 1
311
+ end
312
+
313
+ assert_equal(2, count)
314
+ end
315
+
316
+ def test_find_segment_default
317
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
318
+ id_to_find = s.segment_id
319
+ # should return itself in the base case
320
+ assert_equal(s, s.find_segment(id_to_find))
321
+ end
322
+
323
+ def test_find_segment_not_found
324
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
325
+ assert_equal(nil, s.find_segment(-1))
326
+ end
327
+
328
+ def test_find_segment_with_children
329
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
330
+ id_to_find = s.segment_id
331
+ # should return itself in the base case
332
+ assert_equal(s, s.find_segment(id_to_find))
333
+ end
334
+
335
+ def test_explain_sql_no_sql
336
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
337
+ s.params = {:sql => nil}
338
+ assert_equal(nil, s.explain_sql)
339
+ end
340
+
341
+ def test_explain_sql_no_connection_config
342
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
343
+ s.params = {:sql => 'foo', :connection_config => nil}
344
+ assert_equal(nil, s.explain_sql)
345
+ end
346
+
347
+ def test_explain_sql_non_select
348
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
349
+ s.params = {:sql => 'foo', :connection_config => mock('config')}
350
+ assert_equal([], s.explain_sql)
351
+ end
352
+
353
+ def test_explain_sql_one_select_no_connection
354
+ # NB this test raises an error in the log, much as it might if a
355
+ # user supplied a config that was not valid. This is generally
356
+ # expected behavior - the get_connection method shouldn't allow
357
+ # errors to percolate up.
358
+ config = mock('config')
359
+ config.stubs(:[]).returns(nil)
360
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
361
+ s.params = {:sql => 'SELECT', :connection_config => config}
362
+ assert_equal([], s.explain_sql)
363
+ end
364
+
365
+ def test_explain_sql_one_select_with_connection
366
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
367
+ config = mock('config')
368
+ s.params = {:sql => 'SELECT', :connection_config => config}
369
+ connection = mock('connection')
370
+ # two rows, two columns
371
+ connection.expects(:execute).with('EXPLAIN SELECT').returns([["string", "string"], ["string", "string"]])
372
+ NewRelic::TransactionSample.expects(:get_connection).with(config).returns(connection)
373
+ assert_equal([[['string', 'string'], ['string', 'string']]], s.explain_sql)
374
+ end
375
+
376
+ # this basically casts the resultset to an array of rows, which are
377
+ # arrays of columns
378
+ def test_process_resultset
379
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
380
+ items = mock('bar')
381
+ row = ["column"]
382
+ items.expects(:respond_to?).with(:each).returns(true)
383
+ items.expects(:each).yields(row)
384
+ assert_equal([["column"]], s.process_resultset(items))
385
+ end
386
+
387
+ def test_explain_sql_two_selects_with_connection
388
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
389
+ config = mock('config')
390
+ s.params = {:sql => "SELECT true();\nSELECT false()", :connection_config => config}
391
+ connection = mock('connection')
392
+ # two rows, two columns
393
+ connection.expects(:execute).returns([["string", "string"], ["string", "string"]]).twice
394
+ NewRelic::TransactionSample.expects(:get_connection).with(config).returns(connection).twice
395
+ assert_equal([[['string', 'string'], ['string', 'string']], [['string', 'string'], ['string', 'string']]], s.explain_sql)
396
+ end
397
+
398
+ def test_explain_sql_raising_an_error
399
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
400
+ config = mock('config')
401
+ s.params = {:sql => 'SELECT', :connection_config => config}
402
+ connection = mock('connection')
403
+ NewRelic::TransactionSample.expects(:get_connection).with(config).raises(RuntimeError.new("whee"))
404
+ assert_nothing_raised do
405
+ s.explain_sql
406
+ end
407
+ end
408
+
409
+ def test_params_equal
410
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
411
+ assert_equal(nil, s.instance_eval { @params })
412
+
413
+ params = {:foo => 'correct'}
414
+
415
+ s.params = params
416
+ assert_equal(params, s.instance_eval { @params })
417
+ end
418
+
419
+ def test_handle_exception_in_explain
420
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
421
+ fake_error = Exception.new
422
+ fake_error.expects(:message).returns('a message')
423
+ NewRelic::Control.instance.log.expects(:error).with('Error getting explain plan: a message')
424
+ # backtrace can be basically any string, just should get logged
425
+ NewRelic::Control.instance.log.expects(:debug).with(instance_of(String))
426
+ s.handle_exception_in_explain do
427
+ raise(fake_error)
428
+ end
429
+ end
430
+
431
+ def test_obfuscated_sql
432
+ sql = 'some sql'
433
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
434
+ s[:sql] = sql
435
+ NewRelic::TransactionSample.expects(:obfuscate_sql).with(sql)
436
+ s.obfuscated_sql
437
+ end
438
+
439
+ def test_called_segments_equals
440
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
441
+ assert_equal(nil, s.instance_eval { @called_segments })
442
+ s.called_segments = [1, 2, 3]
443
+ assert_equal([1, 2, 3], s.instance_eval { @called_segments })
444
+ end
445
+
446
+ def test_parent_segment_equals
447
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
448
+ assert_equal(nil, s.instance_eval { @parent_segment })
449
+ fake_segment = mock('segment')
450
+ s.send(:parent_segment=, fake_segment)
451
+ assert_equal(fake_segment, s.parent_segment)
452
+ end
453
+ end
454
+