factorylabs-newrelic_rpm 2.10.2.2

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