honkster-newrelic_rpm 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/CHANGELOG +462 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +172 -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 +34 -0
  8. data/install.rb +9 -0
  9. data/lib/new_relic/agent.rb +382 -0
  10. data/lib/new_relic/agent/agent.rb +741 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/error_collector.rb +131 -0
  14. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  16. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +409 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +58 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +319 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +45 -0
  32. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  34. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  36. data/lib/new_relic/agent/method_tracer.rb +350 -0
  37. data/lib/new_relic/agent/sampler.rb +50 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  39. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  40. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  41. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  42. data/lib/new_relic/agent/shim_agent.rb +25 -0
  43. data/lib/new_relic/agent/stats_engine.rb +24 -0
  44. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  45. data/lib/new_relic/agent/stats_engine/samplers.rb +83 -0
  46. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  47. data/lib/new_relic/agent/transaction_sampler.rb +330 -0
  48. data/lib/new_relic/agent/worker_loop.rb +81 -0
  49. data/lib/new_relic/collection_helper.rb +71 -0
  50. data/lib/new_relic/command.rb +85 -0
  51. data/lib/new_relic/commands/deployments.rb +105 -0
  52. data/lib/new_relic/commands/install.rb +81 -0
  53. data/lib/new_relic/control.rb +203 -0
  54. data/lib/new_relic/control/configuration.rb +149 -0
  55. data/lib/new_relic/control/frameworks/external.rb +13 -0
  56. data/lib/new_relic/control/frameworks/merb.rb +24 -0
  57. data/lib/new_relic/control/frameworks/rails.rb +126 -0
  58. data/lib/new_relic/control/frameworks/rails3.rb +60 -0
  59. data/lib/new_relic/control/frameworks/ruby.rb +36 -0
  60. data/lib/new_relic/control/frameworks/sinatra.rb +18 -0
  61. data/lib/new_relic/control/instrumentation.rb +95 -0
  62. data/lib/new_relic/control/logging_methods.rb +74 -0
  63. data/lib/new_relic/control/profiling.rb +24 -0
  64. data/lib/new_relic/control/server_methods.rb +88 -0
  65. data/lib/new_relic/delayed_job_injection.rb +27 -0
  66. data/lib/new_relic/histogram.rb +91 -0
  67. data/lib/new_relic/local_environment.rb +333 -0
  68. data/lib/new_relic/merbtasks.rb +6 -0
  69. data/lib/new_relic/metric_data.rb +42 -0
  70. data/lib/new_relic/metric_parser.rb +136 -0
  71. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  72. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  73. data/lib/new_relic/metric_parser/active_record.rb +28 -0
  74. data/lib/new_relic/metric_parser/apdex.rb +88 -0
  75. data/lib/new_relic/metric_parser/controller.rb +62 -0
  76. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  77. data/lib/new_relic/metric_parser/errors.rb +6 -0
  78. data/lib/new_relic/metric_parser/external.rb +50 -0
  79. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  80. data/lib/new_relic/metric_parser/other_transaction.rb +36 -0
  81. data/lib/new_relic/metric_parser/view.rb +61 -0
  82. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  83. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  84. data/lib/new_relic/metric_spec.rb +67 -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/developer_mode.rb +257 -0
  88. data/lib/new_relic/rack/metric_app.rb +64 -0
  89. data/lib/new_relic/rack/mongrel_rpm.ru +26 -0
  90. data/lib/new_relic/rack/newrelic.yml +27 -0
  91. data/lib/new_relic/rack_app.rb +6 -0
  92. data/lib/new_relic/recipes.rb +82 -0
  93. data/lib/new_relic/stats.rb +368 -0
  94. data/lib/new_relic/timer_lib.rb +27 -0
  95. data/lib/new_relic/transaction_analysis.rb +119 -0
  96. data/lib/new_relic/transaction_sample.rb +586 -0
  97. data/lib/new_relic/url_rule.rb +14 -0
  98. data/lib/new_relic/version.rb +55 -0
  99. data/lib/new_relic_api.rb +276 -0
  100. data/lib/newrelic_rpm.rb +49 -0
  101. data/lib/tasks/all.rb +4 -0
  102. data/lib/tasks/install.rake +7 -0
  103. data/lib/tasks/tests.rake +15 -0
  104. data/newrelic.yml +246 -0
  105. data/newrelic_rpm.gemspec +254 -0
  106. data/recipes/newrelic.rb +6 -0
  107. data/test/active_record_fixtures.rb +55 -0
  108. data/test/config/newrelic.yml +48 -0
  109. data/test/config/test_control.rb +36 -0
  110. data/test/new_relic/agent/active_record_instrumentation_test.rb +286 -0
  111. data/test/new_relic/agent/agent_controller_test.rb +294 -0
  112. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  113. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  114. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  115. data/test/new_relic/agent/error_collector_test.rb +163 -0
  116. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  117. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  118. data/test/new_relic/agent/metric_data_test.rb +53 -0
  119. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  120. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  121. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  122. data/test/new_relic/agent/rpm_agent_test.rb +142 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  124. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  125. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  126. data/test/new_relic/agent/task_instrumentation_test.rb +188 -0
  127. data/test/new_relic/agent/testable_agent.rb +13 -0
  128. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  129. data/test/new_relic/agent/transaction_sample_test.rb +192 -0
  130. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  131. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  132. data/test/new_relic/control_test.rb +127 -0
  133. data/test/new_relic/deployments_api_test.rb +69 -0
  134. data/test/new_relic/environment_test.rb +75 -0
  135. data/test/new_relic/metric_parser_test.rb +226 -0
  136. data/test/new_relic/metric_spec_test.rb +177 -0
  137. data/test/new_relic/rack/episodes_test.rb +318 -0
  138. data/test/new_relic/shim_agent_test.rb +9 -0
  139. data/test/new_relic/stats_test.rb +312 -0
  140. data/test/new_relic/version_number_test.rb +89 -0
  141. data/test/test_contexts.rb +28 -0
  142. data/test/test_helper.rb +72 -0
  143. data/ui/helpers/developer_mode_helper.rb +359 -0
  144. data/ui/helpers/google_pie_chart.rb +49 -0
  145. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  146. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  147. data/ui/views/newrelic/_sample.rhtml +20 -0
  148. data/ui/views/newrelic/_segment.rhtml +29 -0
  149. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  150. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  151. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  152. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  153. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  154. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  155. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  156. data/ui/views/newrelic/_table.rhtml +12 -0
  157. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  158. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  159. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  160. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  161. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  162. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  163. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  164. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  165. data/ui/views/newrelic/file/images/textmate.png +0 -0
  166. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  167. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  168. data/ui/views/newrelic/file/stylesheets/style.css +484 -0
  169. data/ui/views/newrelic/index.rhtml +59 -0
  170. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  171. data/ui/views/newrelic/show_sample.rhtml +79 -0
  172. data/ui/views/newrelic/show_source.rhtml +3 -0
  173. data/ui/views/newrelic/threads.rhtml +52 -0
  174. metadata +307 -0
@@ -0,0 +1,192 @@
1
+ ENV['SKIP_RAILS'] = 'true'
2
+ require File.expand_path('../../../test_helper.rb', __FILE__)
3
+
4
+ class NewRelic::TransactionSampleTest < Test::Unit::TestCase
5
+ extend TestContexts
6
+ include TransactionSampleTestHelper
7
+ ::SQL_STATEMENT = "SELECT * from sandwiches"
8
+
9
+ with_running_agent do
10
+
11
+ context "a sql transaction" do
12
+ setup do
13
+ @connection_stub = Mocha::Mockery.instance.named_mock('connection')
14
+ @connection_stub.stubs(:execute).returns('QUERY RESULT')
15
+
16
+ NewRelic::TransactionSample.stubs(:get_connection).returns @connection_stub
17
+ # NewRelic::TransactionSample::Segment.any_instance.stubs(:explain_sql).returns([[["QUERY RESULT"]],[["QUERY RESULT"]]])
18
+ # NewRelic::TransactionSample::Segment.any_instance.expects(:handle_exception_in_explain).never
19
+ @t = make_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
20
+ end
21
+
22
+ should "be recorded" do
23
+ assert_not_nil @t
24
+ end
25
+
26
+ should "not record sql when record_sql => off" do
27
+ s = @t.prepare_to_send(:explain_sql => 0.00000001)
28
+ s.each_segment do |segment|
29
+ assert_nil segment.params[:explanation]
30
+ assert_nil segment.params[:sql]
31
+ end
32
+ end
33
+
34
+ should "record raw sql" do
35
+ s = @t.prepare_to_send(:explain_sql => 0.00000001, :record_sql => :raw)
36
+ got_one = false
37
+ s.each_segment do |segment|
38
+ fail if segment.params[:obfuscated_sql]
39
+ got_one = got_one || segment.params[:explanation] || segment.params[:sql]
40
+ end
41
+ assert got_one
42
+ end
43
+
44
+ should "record obfuscated sql" do
45
+
46
+ s = @t.prepare_to_send(:explain_sql => 0.00000001, :record_sql => :obfuscated)
47
+
48
+ got_one = false
49
+ s.each_segment do |segment|
50
+ got_one = got_one || segment.params[:explanation] || segment.params[:sql]
51
+ end
52
+
53
+ assert got_one
54
+ end
55
+
56
+ should "have sql rows when sql is recorded" do
57
+ s = @t.prepare_to_send(:explain_sql => 0.00000001)
58
+
59
+ assert s.sql_segments.empty?
60
+ s.root_segment[:sql] = 'hello'
61
+ assert !s.sql_segments.empty?
62
+ end
63
+
64
+ should "have sql rows when sql is obfuscated" do
65
+ s = @t.prepare_to_send(:explain_sql => 0.00000001)
66
+
67
+ assert s.sql_segments.empty?
68
+ s.root_segment[:sql_obfuscated] = 'hello'
69
+ assert !s.sql_segments.empty?
70
+ end
71
+
72
+ should "have sql rows when recording non-sql keys" do
73
+ s = @t.prepare_to_send(:explain_sql => 0.00000001)
74
+
75
+ assert s.sql_segments.empty?
76
+ s.root_segment[:key] = 'hello'
77
+ assert !s.sql_segments.empty?
78
+ end
79
+
80
+ should "catch exceptions" do
81
+ @connection_stub.expects(:execute).raises
82
+ # the sql connection will throw
83
+ @t.prepare_to_send(:record_sql => :obfuscated, :explain_sql => 0.00000001)
84
+ end
85
+
86
+ should "have explains" do
87
+
88
+ s = @t.prepare_to_send(:record_sql => :obfuscated, :explain_sql => 0.00000001)
89
+
90
+ explain_count = 0
91
+ s.each_segment do |segment|
92
+ if segment.params[:explanation]
93
+ explanations = segment.params[:explanation]
94
+
95
+ explanations.each do |explanation|
96
+ assert_kind_of Array, explanation
97
+ assert_kind_of Array, explanation[0]
98
+ assert_equal "QUERY RESULT", explanation[0][0]
99
+ explain_count += 1
100
+ end
101
+ end
102
+ end
103
+ assert_equal 2, explain_count
104
+ end
105
+ end
106
+
107
+
108
+ should "not record sql without record_sql option" do
109
+ t = nil
110
+ NewRelic::Agent.disable_sql_recording do
111
+ t = make_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
112
+ end
113
+
114
+ s = t.prepare_to_send(:explain_sql => 0.00000001)
115
+
116
+ s.each_segment do |segment|
117
+ assert_nil segment.params[:explanation]
118
+ assert_nil segment.params[:sql]
119
+ end
120
+ end
121
+
122
+ context "in disabled transaction tracing block" do
123
+ should "not record transactions" do
124
+ NewRelic::Agent.disable_transaction_tracing do
125
+ t = make_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
126
+ assert t.nil?
127
+ end
128
+ end
129
+ end
130
+
131
+ context "a nested sample" do
132
+
133
+ setup do
134
+ @t = NewRelic::TransactionSample.new
135
+
136
+ @t.params[:test] = "hi"
137
+
138
+ s1 = @t.create_segment(1.0, "controller")
139
+
140
+ @t.root_segment.add_called_segment(s1)
141
+
142
+ s2 = @t.create_segment(2.0, "AR1")
143
+
144
+ s2.params[:test] = "test"
145
+
146
+ s1.add_called_segment(s2)
147
+ s2.end_trace 3.0
148
+ s1.end_trace 4.0
149
+
150
+ s3 = @t.create_segment(4.0, "post_filter")
151
+ @t.root_segment.add_called_segment(s3)
152
+ s3.end_trace 6.0
153
+
154
+ s4 = @t.create_segment(6.0, "post_filter")
155
+ @t.root_segment.add_called_segment(s4)
156
+ s4.end_trace 7.0
157
+ end
158
+
159
+ should "exclusive_duration" do
160
+ s1 = @t.root_segment.called_segments.first
161
+ assert_equal 3.0, s1.duration
162
+ assert_equal 2.0, s1.exclusive_duration
163
+ end
164
+
165
+ should "count the segments" do
166
+ assert_equal 4, @t.count_segments
167
+ end
168
+
169
+ should "truncate long samples" do
170
+ @t.truncate(2)
171
+ assert_equal 2, @t.count_segments
172
+
173
+ @t = NewRelic::TransactionSample.new
174
+
175
+ s1 = @t.create_segment(1.0, "controller")
176
+ @t.root_segment.add_called_segment(s1)
177
+
178
+ 100.times do
179
+ s1.add_called_segment(@t.create_segment(1.0, "segment"))
180
+ end
181
+ assert_equal 101, @t.count_segments
182
+ @t.truncate(2)
183
+ assert_equal 2, @t.count_segments
184
+ assert_equal 101, @t.params[:segment_count]
185
+ end
186
+
187
+ end
188
+
189
+ end
190
+
191
+
192
+ end
@@ -0,0 +1,385 @@
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 test_multiple_samples
33
+
34
+ run_sample_trace
35
+ run_sample_trace
36
+ run_sample_trace
37
+ run_sample_trace
38
+
39
+ samples = @sampler.samples
40
+ assert_equal 4, samples.length
41
+ assert_equal "a", samples.first.root_segment.called_segments[0].metric_name
42
+ assert_equal "a", samples.last.root_segment.called_segments[0].metric_name
43
+ end
44
+
45
+ def test_sample_tree
46
+ assert_equal 0, @sampler.scope_depth
47
+
48
+ @sampler.notice_first_scope_push Time.now.to_f
49
+ @sampler.notice_transaction "/path", nil, {}
50
+ @sampler.notice_push_scope "a"
51
+
52
+ @sampler.notice_push_scope "b"
53
+ @sampler.notice_pop_scope "b"
54
+
55
+ @sampler.notice_push_scope "c"
56
+ @sampler.notice_push_scope "d"
57
+ @sampler.notice_pop_scope "d"
58
+ @sampler.notice_pop_scope "c"
59
+
60
+ @sampler.notice_pop_scope "a"
61
+ @sampler.notice_scope_empty
62
+ sample = @sampler.harvest([],0.0).first
63
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
64
+
65
+ end
66
+
67
+ def test_sample__gc_stats
68
+ GC.extend MockGCStats
69
+ # These are effectively Garbage Collects, detected each time GC.time is
70
+ # called by the transaction sampler. One time value in seconds for each call.
71
+ MockGCStats.mock_values = [0,0,0,1,0,0,1,0,0,0,0,0,0,0,0]
72
+ assert_equal 0, @sampler.scope_depth
73
+
74
+ @sampler.notice_first_scope_push Time.now.to_f
75
+ @sampler.notice_transaction "/path", nil, {}
76
+ @sampler.notice_push_scope "a"
77
+
78
+ @sampler.notice_push_scope "b"
79
+ @sampler.notice_pop_scope "b"
80
+
81
+ @sampler.notice_push_scope "c"
82
+ @sampler.notice_push_scope "d"
83
+ @sampler.notice_pop_scope "d"
84
+ @sampler.notice_pop_scope "c"
85
+
86
+ @sampler.notice_pop_scope "a"
87
+ @sampler.notice_scope_empty
88
+
89
+ sample = @sampler.harvest([],0.0).first
90
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
91
+ ensure
92
+ MockGCStats.mock_values = []
93
+ end
94
+
95
+ def test_sample_id
96
+ run_sample_trace do
97
+ assert @sampler.current_sample_id != 0, @sampler.current_sample_id
98
+ end
99
+ end
100
+
101
+ def test_harvest_slowest
102
+
103
+ run_sample_trace
104
+ run_sample_trace
105
+ run_sample_trace { sleep 0.51 }
106
+ run_sample_trace
107
+ run_sample_trace
108
+
109
+ slowest = @sampler.harvest(nil, 0)[0]
110
+ assert slowest.duration >= 0.5, "sample duration: #{slowest.duration}"
111
+
112
+ run_sample_trace { sleep 0.2 }
113
+ not_as_slow = @sampler.harvest(slowest, 0)[0]
114
+ assert not_as_slow == slowest
115
+
116
+ run_sample_trace { sleep 0.601 }
117
+ new_slowest = @sampler.harvest(slowest, 0)[0]
118
+ assert new_slowest != slowest
119
+ assert new_slowest.duration >= 0.600, "Slowest duration must be > 0.6: #{new_slowest.duration}"
120
+ end
121
+
122
+
123
+ def test_preare_to_send
124
+
125
+ run_sample_trace { sleep 0.2 }
126
+ sample = @sampler.harvest(nil, 0)[0]
127
+
128
+ ready_to_send = sample.prepare_to_send
129
+ assert sample.duration == ready_to_send.duration
130
+
131
+ assert ready_to_send.start_time.is_a?(Time)
132
+ end
133
+
134
+ def test_multithread
135
+ threads = []
136
+
137
+ 20.times do
138
+ t = Thread.new(@sampler) do |the_sampler|
139
+ @sampler = the_sampler
140
+ 100.times do
141
+ run_sample_trace { sleep 0.01 }
142
+ end
143
+ end
144
+
145
+ threads << t
146
+ end
147
+ threads.each {|t| t.join }
148
+ end
149
+
150
+ def test_sample_with_parallel_paths
151
+
152
+ assert_equal 0, @sampler.scope_depth
153
+
154
+ @sampler.notice_first_scope_push Time.now.to_f
155
+ @sampler.notice_transaction "/path", nil, {}
156
+ @sampler.notice_push_scope "a"
157
+
158
+ assert_equal 1, @sampler.scope_depth
159
+
160
+ @sampler.notice_pop_scope "a"
161
+ @sampler.notice_scope_empty
162
+
163
+ assert_equal 0, @sampler.scope_depth
164
+
165
+ @sampler.notice_first_scope_push Time.now.to_f
166
+ @sampler.notice_transaction "/path", nil, {}
167
+ @sampler.notice_push_scope "a"
168
+ @sampler.notice_pop_scope "a"
169
+ @sampler.notice_scope_empty
170
+
171
+ assert_equal 0, @sampler.scope_depth
172
+ sample = @sampler.harvest(nil, 0.0).first
173
+ assert_equal "ROOT{a}", sample.to_s_compact
174
+ end
175
+
176
+ def test_double_scope_stack_empty
177
+
178
+ @sampler.notice_first_scope_push Time.now.to_f
179
+ @sampler.notice_transaction "/path", nil, {}
180
+ @sampler.notice_push_scope "a"
181
+ @sampler.notice_pop_scope "a"
182
+ @sampler.notice_scope_empty
183
+ @sampler.notice_scope_empty
184
+ @sampler.notice_scope_empty
185
+ @sampler.notice_scope_empty
186
+
187
+ assert_not_nil @sampler.harvest(nil, 0)[0]
188
+ end
189
+
190
+
191
+ def test_record_sql_off
192
+
193
+ @sampler.notice_first_scope_push Time.now.to_f
194
+
195
+ Thread::current[:record_sql] = false
196
+
197
+ @sampler.notice_sql("test", nil, 0)
198
+
199
+ segment = @sampler.send(:builder).current_segment
200
+
201
+ assert_nil segment[:sql]
202
+ end
203
+
204
+ def test_stack_trace__sql
205
+ @sampler.stack_trace_threshold = 0
206
+
207
+ @sampler.notice_first_scope_push Time.now.to_f
208
+
209
+ @sampler.notice_sql("test", nil, 1)
210
+
211
+ segment = @sampler.send(:builder).current_segment
212
+
213
+ assert segment[:sql]
214
+ assert segment[:backtrace]
215
+ end
216
+ def test_stack_trace__scope
217
+
218
+ @sampler.stack_trace_threshold = 0
219
+ t = Time.now
220
+ @sampler.notice_first_scope_push t.to_f
221
+ @sampler.notice_push_scope 'Bill', (t+1.second).to_f
222
+
223
+ segment = @sampler.send(:builder).current_segment
224
+ assert segment[:backtrace]
225
+ end
226
+
227
+ def test_nil_stacktrace
228
+
229
+ @sampler.stack_trace_threshold = 2
230
+
231
+ @sampler.notice_first_scope_push Time.now.to_f
232
+
233
+ @sampler.notice_sql("test", nil, 1)
234
+
235
+ segment = @sampler.send(:builder).current_segment
236
+
237
+ assert segment[:sql]
238
+ assert_nil segment[:backtrace]
239
+ end
240
+
241
+ def test_big_sql
242
+
243
+ @sampler.notice_first_scope_push Time.now.to_f
244
+
245
+ sql = "SADJKHASDHASD KAJSDH ASKDH ASKDHASDK JASHD KASJDH ASKDJHSAKDJHAS DKJHSADKJSAH DKJASHD SAKJDH SAKDJHS"
246
+
247
+ len = 0
248
+ while len <= NewRelic::Agent::TransactionSampler::MAX_SQL_LENGTH
249
+ @sampler.notice_sql(sql, nil, 0)
250
+ len += sql.length
251
+ end
252
+
253
+ segment = @sampler.send(:builder).current_segment
254
+
255
+ sql = segment[:sql]
256
+
257
+ assert sql.length <= NewRelic::Agent::TransactionSampler::MAX_SQL_LENGTH
258
+ end
259
+
260
+
261
+ def test_segment_obfuscated
262
+
263
+ @sampler.notice_first_scope_push Time.now.to_f
264
+
265
+ orig_sql = "SELECT * from Jim where id=66"
266
+
267
+ @sampler.notice_sql(orig_sql, nil, 0)
268
+
269
+ segment = @sampler.send(:builder).current_segment
270
+
271
+ assert_equal orig_sql, segment[:sql]
272
+ assert_equal "SELECT * from Jim where id=?", segment.obfuscated_sql
273
+ end
274
+
275
+
276
+ def test_param_capture
277
+ [true, false].each do |capture|
278
+ NewRelic::Control.instance.stubs(:capture_params).returns(capture)
279
+ @sampler.notice_first_scope_push Time.now.to_f
280
+ @sampler.notice_transaction('/path', nil, {:param => 'hi'})
281
+ @sampler.notice_scope_empty
282
+
283
+ tt = @sampler.harvest(nil,0)[0]
284
+
285
+ assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
286
+ end
287
+ end
288
+
289
+
290
+ def test_sql_normalization
291
+
292
+ # basic statement
293
+ assert_equal "INSERT INTO X values(?,?, ? , ?)",
294
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "INSERT INTO X values('test',0, 1 , 2)")
295
+
296
+ # escaped literals
297
+ assert_equal "INSERT INTO X values(?, ?,?, ? , ?)",
298
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "INSERT INTO X values('', 'jim''s ssn',0, 1 , 'jim''s son''s son')")
299
+
300
+ # multiple string literals
301
+ assert_equal "INSERT INTO X values(?,?,?, ? , ?)",
302
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "INSERT INTO X values('jim''s ssn','x',0, 1 , 2)")
303
+
304
+ # empty string literal
305
+ # NOTE: the empty string literal resolves to empty string, which for our purposes is acceptable
306
+ assert_equal "INSERT INTO X values(?,?,?, ? , ?)",
307
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "INSERT INTO X values('','x',0, 1 , 2)")
308
+
309
+ # try a select statement
310
+ assert_equal "select * from table where name=? and ssn=?",
311
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "select * from table where name='jim gochee' and ssn=0012211223")
312
+
313
+ # number literals embedded in sql - oh well
314
+ assert_equal "select * from table_? where name=? and ssn=?",
315
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "select * from table_007 where name='jim gochee' and ssn=0012211223")
316
+ end
317
+
318
+ def test_sql_normalization__single_quotes
319
+ assert_equal "INSERT ? into table",
320
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, "INSERT 'this isn''t a real value' into table")
321
+ assert_equal "INSERT ? into table",
322
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT '"' into table])
323
+ assert_equal "INSERT ? into table",
324
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT ' "some text" \" ' into table])
325
+ # could not get this one licked. no biggie
326
+ # assert_equal "INSERT ? into table",
327
+ # NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT '\'' into table])
328
+ assert_equal "INSERT ? into table",
329
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT ''' ' into table])
330
+ end
331
+ def test_sql_normalization__double_quotes
332
+ assert_equal "INSERT ? into table",
333
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT "this isn't a real value" into table])
334
+ assert_equal "INSERT ? into table",
335
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT "'" into table])
336
+ assert_equal "INSERT ? into table",
337
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT " \" " into table])
338
+ assert_equal "INSERT ? into table",
339
+ NewRelic::Agent.instance.send(:default_sql_obfuscator, %q[INSERT " 'some text' " into table])
340
+ end
341
+ def test_sql_obfuscation_filters
342
+ orig = NewRelic::Agent.agent.obfuscator
343
+
344
+ NewRelic::Agent.set_sql_obfuscator(:replace) do |sql|
345
+ sql = "1" + sql
346
+ end
347
+
348
+ sql = "SELECT * FROM TABLE 123 'jim'"
349
+
350
+ assert_equal "1" + sql, NewRelic::Agent.instance.obfuscator.call(sql)
351
+
352
+ NewRelic::Agent.set_sql_obfuscator(:before) do |sql|
353
+ sql = "2" + sql
354
+ end
355
+
356
+ assert_equal "12" + sql, NewRelic::Agent.instance.obfuscator.call(sql)
357
+
358
+ NewRelic::Agent.set_sql_obfuscator(:after) do |sql|
359
+ sql = sql + "3"
360
+ end
361
+
362
+ assert_equal "12" + sql + "3", NewRelic::Agent.instance.obfuscator.call(sql)
363
+
364
+ NewRelic::Agent.agent.set_sql_obfuscator(:replace, &orig)
365
+ end
366
+
367
+
368
+ private
369
+ def run_sample_trace(&proc)
370
+ @sampler.notice_first_scope_push Time.now.to_f
371
+ @sampler.notice_transaction '/path', nil, {}
372
+ @sampler.notice_push_scope "a"
373
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
374
+ @sampler.notice_push_scope "ab"
375
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
376
+ proc.call if proc
377
+ @sampler.notice_pop_scope "ab"
378
+ @sampler.notice_push_scope "lew"
379
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
380
+ @sampler.notice_pop_scope "lew"
381
+ @sampler.notice_pop_scope "a"
382
+ @sampler.notice_scope_empty
383
+ end
384
+
385
+ end