genki-newrelic_rpm 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +316 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +156 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/genki-newrelic_rpm.gemspec +32 -0
  10. data/init.rb +38 -0
  11. data/install.rb +37 -0
  12. data/lib/new_relic/agent.rb +280 -0
  13. data/lib/new_relic/agent/agent.rb +627 -0
  14. data/lib/new_relic/agent/chained_call.rb +13 -0
  15. data/lib/new_relic/agent/collection_helper.rb +61 -0
  16. data/lib/new_relic/agent/error_collector.rb +125 -0
  17. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  18. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  22. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  26. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  27. data/lib/new_relic/agent/instrumentation/net.rb +12 -0
  28. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  29. data/lib/new_relic/agent/instrumentation/rack.rb +77 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  31. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  32. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +38 -0
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  34. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  35. data/lib/new_relic/agent/method_tracer.rb +277 -0
  36. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  37. data/lib/new_relic/agent/sampler.rb +12 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  39. data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
  40. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  41. data/lib/new_relic/agent/shim_agent.rb +21 -0
  42. data/lib/new_relic/agent/stats_engine.rb +24 -0
  43. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  44. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  45. data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
  46. data/lib/new_relic/agent/transaction_sampler.rb +319 -0
  47. data/lib/new_relic/agent/worker_loop.rb +118 -0
  48. data/lib/new_relic/commands/deployments.rb +145 -0
  49. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  50. data/lib/new_relic/control.rb +436 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +22 -0
  53. data/lib/new_relic/control/rails.rb +143 -0
  54. data/lib/new_relic/control/ruby.rb +34 -0
  55. data/lib/new_relic/control/sinatra.rb +14 -0
  56. data/lib/new_relic/histogram.rb +89 -0
  57. data/lib/new_relic/local_environment.rb +285 -0
  58. data/lib/new_relic/merbtasks.rb +6 -0
  59. data/lib/new_relic/metric_data.rb +44 -0
  60. data/lib/new_relic/metric_parser.rb +120 -0
  61. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  62. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  63. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  64. data/lib/new_relic/metric_parser/controller.rb +54 -0
  65. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  66. data/lib/new_relic/metric_parser/errors.rb +6 -0
  67. data/lib/new_relic/metric_parser/external.rb +50 -0
  68. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  69. data/lib/new_relic/metric_parser/view.rb +61 -0
  70. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  71. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  72. data/lib/new_relic/metric_spec.rb +52 -0
  73. data/lib/new_relic/metrics.rb +7 -0
  74. data/lib/new_relic/noticed_error.rb +25 -0
  75. data/lib/new_relic/rack/metric_app.rb +56 -0
  76. data/lib/new_relic/rack/newrelic.ru +25 -0
  77. data/lib/new_relic/rack/newrelic.yml +25 -0
  78. data/lib/new_relic/rack_app.rb +5 -0
  79. data/lib/new_relic/recipes.rb +82 -0
  80. data/lib/new_relic/stats.rb +360 -0
  81. data/lib/new_relic/transaction_analysis.rb +121 -0
  82. data/lib/new_relic/transaction_sample.rb +583 -0
  83. data/lib/new_relic/version.rb +54 -0
  84. data/lib/new_relic_api.rb +315 -0
  85. data/lib/newrelic_rpm.rb +40 -0
  86. data/lib/tasks/all.rb +4 -0
  87. data/lib/tasks/install.rake +7 -0
  88. data/lib/tasks/tests.rake +13 -0
  89. data/newrelic.yml +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 +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +234 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +117 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +118 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +155 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +335 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +51 -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 +78 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +67 -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 +146 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +94 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +214 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +314 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +15 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +45 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +77 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +433 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +327 -0
@@ -0,0 +1,13 @@
1
+
2
+ ##require 'new_relic/agent'
3
+
4
+
5
+ RAILS_ROOT='.' if !defined? RAILS_ROOT
6
+
7
+
8
+ class String
9
+ def titleize
10
+ self
11
+ end
12
+ end
13
+
@@ -0,0 +1,195 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::TransationSampleBuilderTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @builder = NewRelic::Agent::TransactionSampleBuilder.new
7
+ end
8
+
9
+ def test_build_sample
10
+ build_segment("a") do
11
+ build_segment("aa") do
12
+ build_segment("aaa")
13
+ end
14
+ build_segment("ab") do
15
+ build_segment("aba") do
16
+ build_segment("abaa")
17
+ end
18
+ build_segment("aba")
19
+ build_segment("abc") do
20
+ build_segment("abca")
21
+ build_segment("abcd")
22
+ end
23
+ end
24
+ end
25
+ build_segment "b"
26
+ build_segment "c" do
27
+ build_segment "ca"
28
+ build_segment "cb" do
29
+ build_segment "cba"
30
+ end
31
+ end
32
+
33
+ @builder.finish_trace(Time.now.to_f)
34
+ validate_builder
35
+ end
36
+
37
+ def test_freeze
38
+ build_segment "a" do
39
+ build_segment "aa"
40
+ end
41
+
42
+ begin
43
+ builder.sample
44
+ assert false
45
+ rescue Exception => e
46
+ # expected
47
+ end
48
+
49
+ @builder.finish_trace(Time.now.to_f)
50
+
51
+ validate_builder
52
+
53
+ begin
54
+ build_segment "b"
55
+ assert_false
56
+ rescue TypeError => e
57
+ # expected
58
+ end
59
+ end
60
+
61
+ # this is really a test for transaction sample
62
+ def test_omit_segments_with
63
+ build_segment "Controller/my_controller/index" do
64
+ sleep 0.010
65
+
66
+ build_segment "Rails/Application Code Loading" do
67
+ sleep 0.020
68
+
69
+ build_segment "foo/bar" do
70
+ sleep 0.010
71
+ end
72
+ end
73
+
74
+ build_segment "a" do
75
+ build_segment "ab"
76
+ sleep 0.010
77
+ end
78
+ build_segment "b" do
79
+ build_segment "ba"
80
+ sleep 0.05
81
+ build_segment "bb"
82
+ build_segment "bc" do
83
+ build_segment "bca"
84
+ sleep 0.05
85
+ end
86
+ end
87
+ build_segment "c"
88
+ end
89
+ @builder.finish_trace(Time.now.to_f)
90
+
91
+ validate_builder false
92
+
93
+ sample = @builder.sample
94
+
95
+ should_be_a_copy = sample.omit_segments_with('OMIT NOTHING')
96
+ validate_segment should_be_a_copy.root_segment, false
97
+
98
+ assert sample.to_s == should_be_a_copy.to_s
99
+
100
+ without_code_loading = sample.omit_segments_with('Rails/Application Code Loading')
101
+ validate_segment without_code_loading.root_segment, false
102
+
103
+ # after we take out code loading, the delta should be approximately
104
+ # 30 milliseconds
105
+ delta = (sample.duration - without_code_loading.duration) * 1000
106
+
107
+ # Need to allow substantial headroom on the upper bound to prevent
108
+ # spurious errors.
109
+ assert delta >= 30, "delta #{delta} should be between 30 and 100"
110
+ # disable this test for a couple days:
111
+ assert delta <= 100, "delta #{delta} should be between 30 and 100"
112
+
113
+ # ensure none of the segments have this regex
114
+ without_code_loading.each_segment do |segment|
115
+ assert_nil segment.metric_name =~ /Rails\/Application Code Loading/
116
+ end
117
+ end
118
+ def test_unbalanced_handling
119
+ assert_raise RuntimeError do
120
+ build_segment("a") do
121
+ begin
122
+ build_segment("aa") do
123
+ build_segment("aaa") do
124
+ raise "a problem"
125
+ end
126
+ end
127
+ rescue; end
128
+ end
129
+ end
130
+ end
131
+ def test_marshal
132
+ build_segment "a" do
133
+ build_segment "ab"
134
+ end
135
+ build_segment "b" do
136
+ build_segment "ba"
137
+ build_segment "bb"
138
+ build_segment "bc" do
139
+ build_segment "bca"
140
+ end
141
+ end
142
+ build_segment "c"
143
+
144
+ @builder.finish_trace(Time.now.to_f)
145
+ validate_builder
146
+
147
+ dump = Marshal.dump @builder.sample
148
+ sample = Marshal.restore(dump)
149
+ validate_segment(sample.root_segment)
150
+ end
151
+
152
+ def test_parallel_first_level_segments
153
+ build_segment "a" do
154
+ build_segment "ab"
155
+ end
156
+ build_segment "b"
157
+ build_segment "c"
158
+
159
+ @builder.finish_trace(Time.now.to_f)
160
+ validate_builder
161
+ end
162
+
163
+ def validate_builder(check_names = true)
164
+ validate_segment @builder.sample.root_segment, check_names
165
+ end
166
+
167
+ def validate_segment(s, check_names = true)
168
+ p = s.parent_segment
169
+
170
+ unless p.nil? || p.metric_name == 'ROOT'
171
+ assert p.called_segments.include?(s)
172
+ assert_equal p.metric_name.length, s.metric_name.length - 1, "p: #{p.metric_name}, s: #{s.metric_name}" if check_names
173
+ assert p.metric_name < s.metric_name if check_names
174
+ assert p.entry_timestamp <= s.entry_timestamp
175
+ end
176
+
177
+ assert s.exit_timestamp >= s.entry_timestamp
178
+
179
+ children = s.called_segments
180
+ last_segment = s
181
+ children.each do |child|
182
+ assert child.metric_name > last_segment.metric_name if check_names
183
+ assert child.entry_timestamp >= last_segment.entry_timestamp
184
+ last_metric = child
185
+
186
+ validate_segment(child, check_names)
187
+ end
188
+ end
189
+
190
+ def build_segment(metric, time = 0, &proc)
191
+ @builder.trace_entry(metric, Time.now.to_f)
192
+ proc.call if proc
193
+ @builder.trace_exit(metric, Time.now.to_f)
194
+ end
195
+ end
@@ -0,0 +1,146 @@
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
+
65
+ t = make_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
66
+ assert t
67
+ end
68
+
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
+ def test_exclusive_duration
121
+ t = NewRelic::TransactionSample.new
122
+
123
+ t.params[:test] = "hi"
124
+
125
+ s1 = t.create_segment(1.0, "controller")
126
+
127
+ t.root_segment.add_called_segment(s1)
128
+
129
+ s2 = t.create_segment(2.0, "AR1")
130
+
131
+ s2.params[:test] = "test"
132
+
133
+ s2.to_s
134
+
135
+ s1.add_called_segment(s2)
136
+
137
+ s2.end_trace 3.0
138
+ s1.end_trace 4.0
139
+
140
+ t.to_s
141
+
142
+ assert_equal 3.0, s1.duration
143
+ assert_equal 2.0, s1.exclusive_duration
144
+ end
145
+
146
+ end
@@ -0,0 +1,387 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ ##require 'new_relic/agent/transaction_sampler'
3
+
4
+ NewRelic::Agent::TransactionSampler.send :public, :builder
5
+
6
+ class NewRelic::Agent::TransationSamplerTest < Test::Unit::TestCase
7
+
8
+ module MockGCStats
9
+
10
+ def time
11
+ return 0 if @@values.empty?
12
+ raise "too many calls" if @@index >= @@values.size
13
+ @@curtime ||= 0
14
+ @@curtime += (@@values[@@index] * 1e09).to_i
15
+ @@index += 1
16
+ @@curtime
17
+ end
18
+
19
+ def self.mock_values= array
20
+ @@values = array
21
+ @@index = 0
22
+ end
23
+
24
+ end
25
+
26
+ def setup
27
+ Thread::current[:record_sql] = nil
28
+ agent = NewRelic::Agent.instance
29
+ stats_engine = NewRelic::Agent::StatsEngine.new
30
+ agent.stubs(:stats_engine).returns(stats_engine)
31
+ @sampler = NewRelic::Agent::TransactionSampler.new(agent)
32
+ end
33
+
34
+ def test_multiple_samples
35
+
36
+ run_sample_trace
37
+ run_sample_trace
38
+ run_sample_trace
39
+ run_sample_trace
40
+
41
+ samples = @sampler.samples
42
+ assert_equal 4, samples.length
43
+ assert_equal "a", samples.first.root_segment.called_segments[0].metric_name
44
+ assert_equal "a", samples.last.root_segment.called_segments[0].metric_name
45
+ end
46
+
47
+ def test_sample_tree
48
+ assert_equal 0, @sampler.scope_depth
49
+
50
+ @sampler.notice_first_scope_push Time.now.to_f
51
+ @sampler.notice_transaction "/path", nil, {}
52
+ @sampler.notice_push_scope "a"
53
+
54
+ @sampler.notice_push_scope "b"
55
+ @sampler.notice_pop_scope "b"
56
+
57
+ @sampler.notice_push_scope "c"
58
+ @sampler.notice_push_scope "d"
59
+ @sampler.notice_pop_scope "d"
60
+ @sampler.notice_pop_scope "c"
61
+
62
+ @sampler.notice_pop_scope "a"
63
+ @sampler.notice_scope_empty
64
+ sample = @sampler.harvest([],0.0).first
65
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
66
+
67
+ end
68
+
69
+ def test_sample__gc_stats
70
+ GC.extend MockGCStats
71
+ # These are effectively Garbage Collects, detected each time GC.time is
72
+ # called by the transaction sampler. One time value in seconds for each call.
73
+ MockGCStats.mock_values = [0,0,0,1,0,0,1,0,0,0,0,0,0,0,0]
74
+ assert_equal 0, @sampler.scope_depth
75
+
76
+ @sampler.notice_first_scope_push Time.now.to_f
77
+ @sampler.notice_transaction "/path", nil, {}
78
+ @sampler.notice_push_scope "a"
79
+
80
+ @sampler.notice_push_scope "b"
81
+ @sampler.notice_pop_scope "b"
82
+
83
+ @sampler.notice_push_scope "c"
84
+ @sampler.notice_push_scope "d"
85
+ @sampler.notice_pop_scope "d"
86
+ @sampler.notice_pop_scope "c"
87
+
88
+ @sampler.notice_pop_scope "a"
89
+ @sampler.notice_scope_empty
90
+
91
+ sample = @sampler.harvest([],0.0).first
92
+ assert_equal "ROOT{a{b,c{d}}}", sample.to_s_compact
93
+ ensure
94
+ MockGCStats.mock_values = []
95
+ end
96
+
97
+ def test_sample_id
98
+ run_sample_trace do
99
+ assert @sampler.current_sample_id != 0, @sampler.current_sample_id
100
+ end
101
+ end
102
+
103
+ def test_harvest_slowest
104
+
105
+ run_sample_trace
106
+ run_sample_trace
107
+ run_sample_trace { sleep 0.5 }
108
+ run_sample_trace
109
+ run_sample_trace
110
+
111
+ slowest = @sampler.harvest(nil, 0)[0]
112
+ assert slowest.duration >= 0.5
113
+
114
+ run_sample_trace { sleep 0.2 }
115
+ not_as_slow = @sampler.harvest(slowest, 0)[0]
116
+ assert not_as_slow == slowest
117
+
118
+ run_sample_trace { sleep 0.6 }
119
+ new_slowest = @sampler.harvest(slowest, 0)[0]
120
+ assert new_slowest != slowest
121
+ assert new_slowest.duration >= 0.6
122
+ end
123
+
124
+
125
+ def test_preare_to_send
126
+
127
+ run_sample_trace { sleep 0.2 }
128
+ sample = @sampler.harvest(nil, 0)[0]
129
+
130
+ ready_to_send = sample.prepare_to_send
131
+ assert sample.duration == ready_to_send.duration
132
+
133
+ assert ready_to_send.start_time.is_a?(Time)
134
+ end
135
+
136
+ def test_multithread
137
+ threads = []
138
+
139
+ 20.times do
140
+ t = Thread.new(@sampler) do |the_sampler|
141
+ @sampler = the_sampler
142
+ 100.times do
143
+ run_sample_trace { sleep 0.01 }
144
+ end
145
+ end
146
+
147
+ threads << t
148
+ end
149
+ threads.each {|t| t.join }
150
+ end
151
+
152
+ def test_sample_with_parallel_paths
153
+
154
+ assert_equal 0, @sampler.scope_depth
155
+
156
+ @sampler.notice_first_scope_push Time.now.to_f
157
+ @sampler.notice_transaction "/path", nil, {}
158
+ @sampler.notice_push_scope "a"
159
+
160
+ assert_equal 1, @sampler.scope_depth
161
+
162
+ @sampler.notice_pop_scope "a"
163
+ @sampler.notice_scope_empty
164
+
165
+ assert_equal 0, @sampler.scope_depth
166
+
167
+ @sampler.notice_first_scope_push Time.now.to_f
168
+ @sampler.notice_transaction "/path", nil, {}
169
+ @sampler.notice_push_scope "a"
170
+ @sampler.notice_pop_scope "a"
171
+ @sampler.notice_scope_empty
172
+
173
+ assert_equal 0, @sampler.scope_depth
174
+ sample = @sampler.harvest(nil, 0.0).first
175
+ assert_equal "ROOT{a}", sample.to_s_compact
176
+ end
177
+
178
+ def test_double_scope_stack_empty
179
+
180
+ @sampler.notice_first_scope_push Time.now.to_f
181
+ @sampler.notice_transaction "/path", nil, {}
182
+ @sampler.notice_push_scope "a"
183
+ @sampler.notice_pop_scope "a"
184
+ @sampler.notice_scope_empty
185
+ @sampler.notice_scope_empty
186
+ @sampler.notice_scope_empty
187
+ @sampler.notice_scope_empty
188
+
189
+ assert_not_nil @sampler.harvest(nil, 0)[0]
190
+ end
191
+
192
+
193
+ def test_record_sql_off
194
+
195
+ @sampler.notice_first_scope_push Time.now.to_f
196
+
197
+ Thread::current[:record_sql] = false
198
+
199
+ @sampler.notice_sql("test", nil, 0)
200
+
201
+ segment = @sampler.builder.current_segment
202
+
203
+ assert_nil segment[:sql]
204
+ end
205
+
206
+ def test_stack_trace__sql
207
+ @sampler.stack_trace_threshold = 0
208
+
209
+ @sampler.notice_first_scope_push Time.now.to_f
210
+
211
+ @sampler.notice_sql("test", nil, 1)
212
+
213
+ segment = @sampler.builder.current_segment
214
+
215
+ assert segment[:sql]
216
+ assert segment[:backtrace]
217
+ end
218
+ def test_stack_trace__scope
219
+
220
+ @sampler.stack_trace_threshold = 0
221
+ t = Time.now
222
+ @sampler.notice_first_scope_push t.to_f
223
+ @sampler.notice_push_scope 'Bill', (t+1.second).to_f
224
+
225
+ segment = @sampler.builder.current_segment
226
+ assert segment[:backtrace]
227
+ end
228
+
229
+ def test_nil_stacktrace
230
+
231
+ @sampler.stack_trace_threshold = 2
232
+
233
+ @sampler.notice_first_scope_push Time.now.to_f
234
+
235
+ @sampler.notice_sql("test", nil, 1)
236
+
237
+ segment = @sampler.builder.current_segment
238
+
239
+ assert segment[:sql]
240
+ assert_nil segment[:backtrace]
241
+ end
242
+
243
+ def test_big_sql
244
+
245
+ @sampler.notice_first_scope_push Time.now.to_f
246
+
247
+ sql = "SADJKHASDHASD KAJSDH ASKDH ASKDHASDK JASHD KASJDH ASKDJHSAKDJHAS DKJHSADKJSAH DKJASHD SAKJDH SAKDJHS"
248
+
249
+ len = 0
250
+ while len <= NewRelic::Agent::TransactionSampler::MAX_SQL_LENGTH
251
+ @sampler.notice_sql(sql, nil, 0)
252
+ len += sql.length
253
+ end
254
+
255
+ segment = @sampler.builder.current_segment
256
+
257
+ sql = segment[:sql]
258
+
259
+ assert sql.length <= NewRelic::Agent::TransactionSampler::MAX_SQL_LENGTH
260
+ end
261
+
262
+
263
+ def test_segment_obfuscated
264
+
265
+ @sampler.notice_first_scope_push Time.now.to_f
266
+
267
+ orig_sql = "SELECT * from Jim where id=66"
268
+
269
+ @sampler.notice_sql(orig_sql, nil, 0)
270
+
271
+ segment = @sampler.builder.current_segment
272
+
273
+ assert_equal orig_sql, segment[:sql]
274
+ assert_equal "SELECT * from Jim where id=?", segment.obfuscated_sql
275
+ end
276
+
277
+
278
+ def test_param_capture
279
+ [true, false].each do |capture|
280
+ NewRelic::Control.instance.stubs(:capture_params).returns(capture)
281
+ @sampler.notice_first_scope_push Time.now.to_f
282
+ @sampler.notice_transaction('/path', nil, {:param => 'hi'})
283
+ @sampler.notice_scope_empty
284
+
285
+ tt = @sampler.harvest(nil,0)[0]
286
+
287
+ assert_equal (capture) ? 1 : 0, tt.params[:request_params].length
288
+ end
289
+ end
290
+
291
+
292
+ def test_sql_normalization
293
+
294
+ # basic statement
295
+ assert_equal "INSERT INTO X values(?,?, ? , ?)",
296
+ @sampler.default_sql_obfuscator("INSERT INTO X values('test',0, 1 , 2)")
297
+
298
+ # escaped literals
299
+ assert_equal "INSERT INTO X values(?, ?,?, ? , ?)",
300
+ @sampler.default_sql_obfuscator("INSERT INTO X values('', 'jim''s ssn',0, 1 , 'jim''s son''s son')")
301
+
302
+ # multiple string literals
303
+ assert_equal "INSERT INTO X values(?,?,?, ? , ?)",
304
+ @sampler.default_sql_obfuscator("INSERT INTO X values('jim''s ssn','x',0, 1 , 2)")
305
+
306
+ # empty string literal
307
+ # NOTE: the empty string literal resolves to empty string, which for our purposes is acceptable
308
+ assert_equal "INSERT INTO X values(?,?,?, ? , ?)",
309
+ @sampler.default_sql_obfuscator("INSERT INTO X values('','x',0, 1 , 2)")
310
+
311
+ # try a select statement
312
+ assert_equal "select * from table where name=? and ssn=?",
313
+ @sampler.default_sql_obfuscator("select * from table where name='jim gochee' and ssn=0012211223")
314
+
315
+ # number literals embedded in sql - oh well
316
+ assert_equal "select * from table_? where name=? and ssn=?",
317
+ @sampler.default_sql_obfuscator("select * from table_007 where name='jim gochee' and ssn=0012211223")
318
+ end
319
+
320
+ def test_sql_normalization__single_quotes
321
+ assert_equal "INSERT ? into table",
322
+ @sampler.default_sql_obfuscator("INSERT 'this isn''t a real value' into table")
323
+ assert_equal "INSERT ? into table",
324
+ @sampler.default_sql_obfuscator(%q[INSERT '"' into table])
325
+ assert_equal "INSERT ? into table",
326
+ @sampler.default_sql_obfuscator(%q[INSERT ' "some text" \" ' into table])
327
+ # could not get this one licked. no biggie
328
+ # assert_equal "INSERT ? into table",
329
+ # @sampler.default_sql_obfuscator(%q[INSERT '\'' into table])
330
+ assert_equal "INSERT ? into table",
331
+ @sampler.default_sql_obfuscator(%q[INSERT ''' ' into table])
332
+ end
333
+ def test_sql_normalization__double_quotes
334
+ assert_equal "INSERT ? into table",
335
+ @sampler.default_sql_obfuscator(%q[INSERT "this isn't a real value" into table])
336
+ assert_equal "INSERT ? into table",
337
+ @sampler.default_sql_obfuscator(%q[INSERT "'" into table])
338
+ assert_equal "INSERT ? into table",
339
+ @sampler.default_sql_obfuscator(%q[INSERT " \" " into table])
340
+ assert_equal "INSERT ? into table",
341
+ @sampler.default_sql_obfuscator(%q[INSERT " 'some text' " into table])
342
+ end
343
+ def test_sql_obfuscation_filters
344
+ orig = NewRelic::Agent.agent.obfuscator
345
+
346
+ NewRelic::Agent.set_sql_obfuscator(:replace) do |sql|
347
+ sql = "1" + sql
348
+ end
349
+
350
+ sql = "SELECT * FROM TABLE 123 'jim'"
351
+
352
+ assert_equal "1" + sql, NewRelic::Agent.instance.obfuscator.call(sql)
353
+
354
+ NewRelic::Agent.set_sql_obfuscator(:before) do |sql|
355
+ sql = "2" + sql
356
+ end
357
+
358
+ assert_equal "12" + sql, NewRelic::Agent.instance.obfuscator.call(sql)
359
+
360
+ NewRelic::Agent.set_sql_obfuscator(:after) do |sql|
361
+ sql = sql + "3"
362
+ end
363
+
364
+ assert_equal "12" + sql + "3", NewRelic::Agent.instance.obfuscator.call(sql)
365
+
366
+ NewRelic::Agent.agent.set_sql_obfuscator(:replace, &orig)
367
+ end
368
+
369
+
370
+ private
371
+ def run_sample_trace(&proc)
372
+ @sampler.notice_first_scope_push Time.now.to_f
373
+ @sampler.notice_transaction '/path', nil, {}
374
+ @sampler.notice_push_scope "a"
375
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'wheat'", nil, 0)
376
+ @sampler.notice_push_scope "ab"
377
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'white'", nil, 0)
378
+ proc.call if proc
379
+ @sampler.notice_pop_scope "ab"
380
+ @sampler.notice_push_scope "lew"
381
+ @sampler.notice_sql("SELECT * FROM sandwiches WHERE bread = 'french'", nil, 0)
382
+ @sampler.notice_pop_scope "lew"
383
+ @sampler.notice_pop_scope "a"
384
+ @sampler.notice_scope_empty
385
+ end
386
+
387
+ end