newrelic_rpm 3.5.7.59 → 3.5.8.64.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data.tar.gz.sig +3 -2
  2. data/CHANGELOG +34 -3
  3. data/LICENSE +23 -0
  4. data/lib/new_relic/agent.rb +50 -3
  5. data/lib/new_relic/agent/agent.rb +40 -60
  6. data/lib/new_relic/agent/configuration/defaults.rb +9 -3
  7. data/lib/new_relic/agent/configuration/server_source.rb +4 -0
  8. data/lib/new_relic/agent/cross_app_monitor.rb +230 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +274 -0
  10. data/lib/new_relic/agent/database.rb +28 -10
  11. data/lib/new_relic/agent/error_collector.rb +5 -0
  12. data/lib/new_relic/agent/event_listener.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +53 -34
  14. data/lib/new_relic/agent/instrumentation/metric_frame.rb +16 -3
  15. data/lib/new_relic/agent/instrumentation/net.rb +13 -11
  16. data/lib/new_relic/agent/instrumentation/resque.rb +10 -10
  17. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  18. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  19. data/lib/new_relic/agent/pipe_service.rb +8 -12
  20. data/lib/new_relic/agent/rules_engine.rb +72 -0
  21. data/lib/new_relic/agent/shim_agent.rb +0 -1
  22. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  23. data/lib/new_relic/agent/stats.rb +149 -0
  24. data/lib/new_relic/agent/stats_engine.rb +9 -0
  25. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  26. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  27. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  28. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  29. data/lib/new_relic/agent/transaction_info.rb +31 -6
  30. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  31. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  32. data/lib/new_relic/helper.rb +32 -0
  33. data/lib/new_relic/local_environment.rb +24 -32
  34. data/lib/new_relic/okjson.rb +599 -0
  35. data/lib/new_relic/transaction_sample.rb +2 -1
  36. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  37. data/lib/new_relic/version.rb +1 -1
  38. data/newrelic.yml +27 -41
  39. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  40. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  41. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  42. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  43. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  44. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  45. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  46. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  47. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  48. data/test/multiverse/suites/resque/instrumentation_test.rb +2 -2
  49. data/test/multiverse/suites/sinatra/Envfile +2 -0
  50. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  51. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  52. data/test/multiverse/suites/sinatra/sinatra_test.rb +75 -4
  53. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  54. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  55. data/test/new_relic/agent/agent_test.rb +20 -40
  56. data/test/new_relic/agent/agent_test_controller_test.rb +24 -19
  57. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  58. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  59. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  60. data/test/new_relic/agent/database_test.rb +60 -16
  61. data/test/new_relic/agent/error_collector_test.rb +28 -4
  62. data/test/new_relic/agent/event_listener_test.rb +23 -2
  63. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +53 -0
  64. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  65. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +414 -59
  66. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  67. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  68. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  69. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  70. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  71. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  72. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  73. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  74. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  75. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  76. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  77. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  78. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  79. data/test/new_relic/agent/stats_test.rb +197 -0
  80. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  81. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  82. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  83. data/test/new_relic/agent_test.rb +35 -5
  84. data/test/new_relic/control_test.rb +1 -1
  85. data/test/new_relic/fake_collector.rb +87 -9
  86. data/test/new_relic/helper_test.rb +24 -0
  87. data/test/new_relic/metric_data_test.rb +11 -11
  88. data/test/new_relic/metric_spec_test.rb +1 -1
  89. data/test/script/ci.sh +1 -1
  90. data/test/test_contexts.rb +0 -1
  91. data/test/test_helper.rb +21 -3
  92. metadata +32 -16
  93. metadata.gz.sig +0 -0
  94. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  95. data/lib/new_relic/stats.rb +0 -337
  96. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  97. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  98. data/test/new_relic/fakes_sending_data.rb +0 -30
  99. data/test/new_relic/stats_test.rb +0 -421
@@ -131,18 +131,19 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
131
131
  'Apdex/new_relic/agent/agent_test/action_with_error',
132
132
  'HttpDispatcher',
133
133
  'Controller/new_relic/agent/agent_test/action_with_error',
134
- 'Errors/all']
134
+ 'Errors/all',
135
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_error']
135
136
 
136
137
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
137
138
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
138
139
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
139
140
  apdex = engine.get_stats_no_scope("Apdex")
140
- score = apdex.get_apdex
141
- assert_equal 1, score[2], 'failing'
142
- assert_equal 0, score[1], 'tol'
143
- assert_equal 0, score[0], 'satisfied'
144
141
 
142
+ assert_equal 1, apdex.apdex_f, 'failing'
143
+ assert_equal 0, apdex.apdex_t, 'tol'
144
+ assert_equal 0, apdex.apdex_s, 'satisfied'
145
145
  end
146
+
146
147
  def test_controller_error
147
148
  engine.clear_stats
148
149
  assert_raise RuntimeError do
@@ -152,18 +153,19 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
152
153
  'Apdex/new_relic/agent/agent_test/action_with_error',
153
154
  'HttpDispatcher',
154
155
  'Controller/new_relic/agent/agent_test/action_with_error',
155
- 'Errors/all']
156
+ 'Errors/all',
157
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_error']
156
158
 
157
159
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0}
158
160
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_error").call_count
159
161
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
160
162
  apdex = engine.get_stats_no_scope("Apdex")
161
- score = apdex.get_apdex
162
- assert_equal 1, score[2], 'failing'
163
- assert_equal 0, score[1], 'tol'
164
- assert_equal 0, score[0], 'satisfied'
165
163
 
164
+ assert_equal 1, apdex.apdex_f, 'failing'
165
+ assert_equal 0, apdex.apdex_t, 'tol'
166
+ assert_equal 0, apdex.apdex_s, 'satisfied'
166
167
  end
168
+
167
169
  def test_filter_error
168
170
  engine.clear_stats
169
171
  assert_raise RuntimeError do
@@ -173,26 +175,29 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
173
175
  'Apdex/new_relic/agent/agent_test/action_with_before_filter_error',
174
176
  'HttpDispatcher',
175
177
  'Controller/new_relic/agent/agent_test/action_with_before_filter_error',
176
- 'Errors/all']
178
+ 'Errors/all',
179
+ 'Errors/Controller/new_relic/agent/agent_test/action_with_before_filter_error']
177
180
 
178
181
  compare_metrics metrics, engine.metrics.reject{|m| m.index('Response')==0 || m.index('CPU')==0 || m.index('GC')==0}
179
182
  assert_equal 1, engine.get_stats_no_scope("Controller/new_relic/agent/agent_test/action_with_before_filter_error").call_count
180
183
  assert_equal 1, engine.get_stats_no_scope("Errors/all").call_count
181
184
  apdex = engine.get_stats_no_scope("Apdex")
182
- score = apdex.get_apdex
183
- assert_equal 1, score[2], 'failing'
184
- assert_equal 0, score[1], 'tol'
185
- assert_equal 0, score[0], 'satisfied'
185
+
186
+ assert_equal 1, apdex.apdex_f, 'failing'
187
+ assert_equal 0, apdex.apdex_t, 'tol'
188
+ assert_equal 0, apdex.apdex_s, 'satisfied'
186
189
  end
190
+
187
191
  def test_metric__ignore_base
188
192
  engine.clear_stats
189
193
  get :base_action
190
194
  compare_metrics [], engine.metrics
191
195
  end
196
+
192
197
  def test_metric__no_ignore
193
198
  path = 'new_relic/agent/agent_test/index'
194
199
  index_stats = stats("Controller/#{path}")
195
- index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
200
+ index_apdex_stats = engine.get_stats_no_scope("Apdex/#{path}")
196
201
  assert_difference 'index_stats.call_count' do
197
202
  assert_difference 'index_apdex_stats.call_count' do
198
203
  get :index
@@ -205,7 +210,7 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
205
210
  path = 'new_relic/agent/agent_test/action_to_ignore_apdex'
206
211
  cpu_stats = stats("ControllerCPU/#{path}")
207
212
  index_stats = stats("Controller/#{path}")
208
- index_apdex_stats = engine.get_custom_stats("Apdex/#{path}", NewRelic::ApdexStats)
213
+ index_apdex_stats = engine.get_stats_no_scope("Apdex/#{path}")
209
214
  assert_difference 'index_stats.call_count' do
210
215
  assert_no_difference 'index_apdex_stats.call_count' do
211
216
  get :action_to_ignore_apdex
@@ -241,9 +246,9 @@ class NewRelic::Agent::AgentTestControllerTest < ActionController::TestCase
241
246
  end
242
247
 
243
248
  def test_controller_params
244
- agent.transaction_sampler.reset!
245
- get :index, 'number' => "001-555-1212"
246
249
  s = with_config(:'transaction_tracer.transaction_threshold' => 0.0) do
250
+ agent.transaction_sampler.reset!
251
+ get :index, 'number' => "001-555-1212"
247
252
  agent.transaction_sampler.harvest(nil)
248
253
  end
249
254
  assert_equal 1, s.size
@@ -6,7 +6,7 @@ class NewRelic::Agent::BusyCalculatorTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @now = Time.now.to_f
8
8
  NewRelic::Agent::BusyCalculator.reset
9
- @instance_busy = NewRelic::MethodTraceStats.new
9
+ @instance_busy = NewRelic::Agent::Stats.new
10
10
  NewRelic::Agent::BusyCalculator.stubs(:instance_busy_stats).returns(@instance_busy)
11
11
  end
12
12
 
@@ -11,9 +11,10 @@ module NewRelic::Agent::Configuration
11
11
  'transaction_tracer.record_sql' => 'raw',
12
12
  'error_collector.enabled' => true
13
13
  },
14
- 'apdex_t' => 1.0,
15
- 'collect_errors' => false,
16
- 'collect_traces' => true
14
+ 'apdex_t' => 1.0,
15
+ 'collect_errors' => false,
16
+ 'collect_traces' => true,
17
+ 'web_transactions_apdex' => { 'Controller/some/txn' => 1.5 }
17
18
  }
18
19
  @source = ServerSource.new(config)
19
20
  end
@@ -41,5 +42,9 @@ module NewRelic::Agent::Configuration
41
42
  def test_should_ignore_apdex_f_setting_for_transaction_threshold
42
43
  assert_equal nil, @source[:'transaction_tracer.transaction_threshold']
43
44
  end
45
+
46
+ def test_should_not_dot_the_web_transactions_apdex_hash
47
+ assert_equal 1.5, @source[:web_transactions_apdex]['Controller/some/txn']
48
+ end
44
49
  end
45
50
  end
@@ -0,0 +1,237 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ module NewRelic::Agent
4
+ class CrossAppMonitorTest < Test::Unit::TestCase
5
+ NEWRELIC_ID_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_ID_HEADER
6
+ NEWRELIC_TXN_HEADER = NewRelic::Agent::CrossAppMonitor::NEWRELIC_TXN_HEADER
7
+
8
+ AGENT_CROSS_APP_ID = "qwerty"
9
+ REQUEST_CROSS_APP_ID = "42#1234"
10
+ TRANSACTION_GUID = '941B0E8001E444E8'
11
+ REF_TRANSACTION_GUID = '830092CDE59421D4'
12
+
13
+ TRANSACTION_NAME = 'transaction'
14
+ QUEUE_TIME = 1000
15
+ APP_TIME = 2000
16
+
17
+ ENCODING_KEY_NOOP = "\0"
18
+ TRUSTED_ACCOUNT_IDS = [42,13]
19
+
20
+ CROSS_APP_ID_POSITION = 0
21
+ TRANSACTION_NAME_POSITION = 1
22
+ QUEUE_TIME_POSITION = 2
23
+ APP_TIME_POSITION = 3
24
+ CONTENT_LENGTH_POSITION = 4
25
+
26
+ def setup
27
+ NewRelic::Agent.reset_config
28
+ NewRelic::Agent.instance.events.clear
29
+ @response = {}
30
+
31
+ @monitor = NewRelic::Agent::CrossAppMonitor.new()
32
+ @config = {
33
+ :cross_process_id => AGENT_CROSS_APP_ID,
34
+ :encoding_key => ENCODING_KEY_NOOP,
35
+ :trusted_account_ids => TRUSTED_ACCOUNT_IDS
36
+ }
37
+
38
+ NewRelic::Agent.config.apply_config( @config )
39
+ @monitor.register_event_listeners
40
+ NewRelic::Agent::TransactionInfo.get.guid = TRANSACTION_GUID
41
+
42
+ # NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new( {:log_level => 'debug'}, '', Logger.new($stderr) )
43
+ end
44
+
45
+ def teardown
46
+ NewRelic::Agent.config.remove_config( @config )
47
+ NewRelic::Agent.instance.events.clear
48
+ end
49
+
50
+
51
+ #
52
+ # Tests
53
+ #
54
+
55
+ def test_adds_response_header
56
+ with_default_timings
57
+
58
+ when_request_runs
59
+
60
+ assert_equal [AGENT_CROSS_APP_ID, TRANSACTION_NAME, QUEUE_TIME, APP_TIME, -1, TRANSACTION_GUID], unpacked_response
61
+ end
62
+
63
+ def test_strips_bad_characters_in_transaction_name
64
+ NewRelic::Agent::BrowserMonitoring.stubs(:timings).returns(stub(
65
+ :transaction_name => "\"'goo",
66
+ :queue_time_in_seconds => QUEUE_TIME,
67
+ :app_time_in_seconds => APP_TIME))
68
+
69
+ when_request_runs
70
+
71
+ assert_equal "goo", unpacked_response[TRANSACTION_NAME_POSITION]
72
+ end
73
+
74
+ def test_doesnt_write_response_header_if_id_blank
75
+ with_default_timings
76
+
77
+ when_request_runs(for_id(''))
78
+ assert_nil response_app_data
79
+ end
80
+
81
+ def test_doesnt_write_response_header_if_untrusted_id
82
+ with_default_timings
83
+
84
+ when_request_runs(for_id("4#1234"))
85
+ assert_nil response_app_data
86
+ end
87
+
88
+ def test_doesnt_write_response_header_if_improperly_formatted_id
89
+ with_default_timings
90
+
91
+ when_request_runs(for_id("42"))
92
+ assert_nil response_app_data
93
+ end
94
+
95
+ def test_doesnt_add_header_if_no_id_in_request
96
+ when_request_runs({})
97
+ assert_nil response_app_data
98
+ end
99
+
100
+ def test_doesnt_add_header_if_no_id_on_agent
101
+ # Since a +nil+ will make it fall back to the config installed in setup,
102
+ # we need to remove that first in order to test the no-id case
103
+ newconfig = @config.merge( :cross_process_id => nil )
104
+ NewRelic::Agent.config.remove_config( @config )
105
+
106
+ with_config( newconfig ) do
107
+ when_request_runs
108
+ assert_nil response_app_data
109
+ end
110
+ end
111
+
112
+ def test_doesnt_add_header_if_config_disabled
113
+ with_config(:"cross_application_tracer.enabled" => false) do
114
+ when_request_runs
115
+ assert_nil response_app_data
116
+ end
117
+ end
118
+
119
+ def test_includes_content_length
120
+ with_default_timings
121
+
122
+ when_request_runs(for_id(REQUEST_CROSS_APP_ID).merge("Content-Length" => 3000))
123
+ assert_equal 3000, unpacked_response[CONTENT_LENGTH_POSITION]
124
+ end
125
+
126
+ def test_finds_content_length_from_headers
127
+ %w{Content-Length HTTP_CONTENT_LENGTH CONTENT_LENGTH cOnTeNt-LeNgTh}.each do |key|
128
+ request = { key => 42 }
129
+
130
+ assert_equal(42, @monitor.content_length_from_request(request), \
131
+ "Failed to find header on key #{key}")
132
+ end
133
+ end
134
+
135
+ def test_writes_custom_parameters
136
+ with_default_timings
137
+
138
+ NewRelic::Agent.expects(:add_custom_parameters).with(:client_cross_process_id => REQUEST_CROSS_APP_ID)
139
+ NewRelic::Agent.expects(:add_custom_parameters).with(:transaction_guid => TRANSACTION_GUID)
140
+ NewRelic::Agent.expects(:add_custom_parameters).with(:transaction_referring_guid => REF_TRANSACTION_GUID)
141
+
142
+ when_request_runs
143
+ end
144
+
145
+ def test_error_writes_custom_parameters
146
+ with_default_timings
147
+
148
+ options = when_request_has_error
149
+
150
+ assert_equal REQUEST_CROSS_APP_ID, options[:client_cross_process_id]
151
+ end
152
+
153
+ def test_error_doesnt_write_custom_parameters_if_no_id
154
+ with_default_timings
155
+
156
+ options = when_request_has_error(for_id(''))
157
+
158
+ assert_equal false, options.key?(:client_cross_process_id)
159
+ end
160
+
161
+ def test_writes_metric
162
+ with_default_timings
163
+
164
+ metric = mock()
165
+ metric.expects(:record_data_point).with(APP_TIME)
166
+ NewRelic::Agent.instance.stats_engine.stubs(:get_stats_no_scope).returns(metric)
167
+
168
+ when_request_runs
169
+ end
170
+
171
+ def test_doesnt_write_metric_if_id_blank
172
+ with_default_timings
173
+ NewRelic::Agent.instance.stats_engine.expects(:get_stats_no_scope).never
174
+
175
+ when_request_runs(for_id(''))
176
+ end
177
+
178
+ def test_decoding_blank
179
+ assert_equal "",
180
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions.decode_with_key( 'querty', "" )
181
+ end
182
+
183
+ def test_encode_with_nil_uses_empty_key
184
+ assert_equal "querty",
185
+ NewRelic::Agent::CrossAppMonitor::EncodingFunctions.encode_with_key( nil, 'querty' )
186
+ end
187
+
188
+
189
+ #
190
+ # Helpers
191
+ #
192
+
193
+ def when_request_runs(request=for_id(REQUEST_CROSS_APP_ID))
194
+ event_listener = NewRelic::Agent.instance.events
195
+ event_listener.notify(:before_call, request)
196
+ event_listener.notify(:start_transaction, 'a name')
197
+ event_listener.notify(:after_call, request, [200, @response, ''])
198
+ end
199
+
200
+ def when_request_has_error(request=for_id(REQUEST_CROSS_APP_ID))
201
+ options = {}
202
+ event_listener = NewRelic::Agent.instance.events
203
+ event_listener.notify(:before_call, request)
204
+ event_listener.notify(:notice_error, nil, options)
205
+ event_listener.notify(:after_call, request, [500, @response, ''])
206
+
207
+ options
208
+ end
209
+
210
+ def with_default_timings
211
+ NewRelic::Agent::BrowserMonitoring.stubs(:timings).returns(stub(
212
+ :transaction_name => TRANSACTION_NAME,
213
+ :queue_time_in_seconds => QUEUE_TIME,
214
+ :app_time_in_seconds => APP_TIME))
215
+ end
216
+
217
+ def for_id(id)
218
+ encoded_id = id == "" ? "" : Base64.encode64(id)
219
+ encoded_txn_info = Base64.encode64( NewRelic.json_dump([ REF_TRANSACTION_GUID, false ]) )
220
+
221
+ return {
222
+ NEWRELIC_ID_HEADER => encoded_id,
223
+ NEWRELIC_TXN_HEADER => encoded_txn_info,
224
+ }
225
+ end
226
+
227
+ def response_app_data
228
+ @response['X-NewRelic-App-Data']
229
+ end
230
+
231
+ def unpacked_response
232
+ return nil unless response_app_data
233
+ NewRelic.json_load(Base64.decode64(response_app_data))
234
+ end
235
+
236
+ end
237
+ end
@@ -5,13 +5,13 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
5
5
  def teardown
6
6
  NewRelic::Agent::Database::Obfuscator.instance.reset
7
7
  end
8
-
8
+
9
9
  def test_process_resultset
10
10
  resultset = [["column"]]
11
11
  assert_equal([nil, [["column"]]],
12
12
  NewRelic::Agent::Database.process_resultset(resultset))
13
13
  end
14
-
14
+
15
15
  def test_explain_sql_select_with_mysql_connection
16
16
  config = {:adapter => 'mysql'}
17
17
  config.default('val')
@@ -30,7 +30,7 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
30
30
 
31
31
  result = NewRelic::Agent::Database.explain_sql(sql, config)
32
32
  assert_equal(plan.keys.sort, result[0].sort)
33
- assert_equal(plan.values.compact.sort, result[1][0].compact.sort)
33
+ assert_equal(plan.values.compact.sort, result[1][0].compact.sort)
34
34
  end
35
35
 
36
36
  def test_explain_sql_one_select_with_pg_connection
@@ -50,6 +50,44 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
50
50
  NewRelic::Agent::Database.explain_sql(sql, config))
51
51
  end
52
52
 
53
+ def test_dont_collect_explain_for_parameterized_query
54
+ config = {:adapter => 'postgresql'}
55
+ config.default('val')
56
+ connection = mock('connection')
57
+ connection.expects(:execute).never
58
+ NewRelic::Agent::Database.stubs(:get_connection).with(config).returns(connection)
59
+ expects_logging(:debug, 'Unable to collect explain plan for parameterized query.')
60
+
61
+ sql = 'SELECT * FROM table WHERE id = $1'
62
+ assert_equal [], NewRelic::Agent::Database.explain_sql(sql, config)
63
+ end
64
+
65
+ def test_do_collect_explain_for_parameter_looking_literal
66
+ config = {:adapter => 'postgresql'}
67
+ config.default('val')
68
+ connection = mock('connection')
69
+ plan = [{"QUERY PLAN"=>"Some Jazz"}]
70
+ connection.stubs(:execute).returns(plan)
71
+ NewRelic::Agent::Database.stubs(:get_connection).with(config).returns(connection)
72
+
73
+ sql = "SELECT * FROM table WHERE id = 'noise $11'"
74
+ assert_equal([['QUERY PLAN'], [["Some Jazz"]]],
75
+ NewRelic::Agent::Database.explain_sql(sql, config))
76
+ end
77
+
78
+ def test_dont_collect_explain_for_truncated_query
79
+ config = {:adapter => 'postgresql'}
80
+ config.default('val')
81
+ connection = mock('connection')
82
+ connection.expects(:execute).never
83
+ NewRelic::Agent::Database.stubs(:get_connection).with(config).returns(connection)
84
+ expects_logging(:debug, 'Unable to collect explain plan for truncated query.')
85
+
86
+ sql = 'SELECT * FROM table WHERE id IN (1,2,3,4,5...'
87
+ assert_equal [], NewRelic::Agent::Database.explain_sql(sql, config)
88
+
89
+ end
90
+
53
91
  def test_explain_sql_no_sql
54
92
  assert_equal(nil, NewRelic::Agent::Database.explain_sql(nil, nil))
55
93
  end
@@ -71,8 +109,8 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
71
109
  config = mock('config')
72
110
  config.stubs(:[]).returns(nil)
73
111
  assert_equal([], NewRelic::Agent::Database.explain_sql('SELECT', config))
74
- end
75
-
112
+ end
113
+
76
114
  def test_obfuscation_mysql_basic
77
115
  insert = %q[INSERT INTO `X` values("test",0, 1 , 2, 'test')]
78
116
  assert_equal("INSERT INTO `X` values(?,?, ? , ?, ?)",
@@ -90,9 +128,9 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
90
128
  select = NewRelic::Agent::Database::Statement.new(%q[SELECT "table"."column" FROM "table" WHERE "table"."column" = 'value' AND "table"."other_column" = 'other value' LIMIT 1])
91
129
  select.adapter = :postgresql
92
130
  assert_equal(%q[SELECT "table"."column" FROM "table" WHERE "table"."column" = ? AND "table"."other_column" = ? LIMIT ?],
93
- NewRelic::Agent::Database.obfuscate_sql(select))
131
+ NewRelic::Agent::Database.obfuscate_sql(select))
94
132
  end
95
-
133
+
96
134
  def test_obfuscation_escaped_literals
97
135
  insert = %q[INSERT INTO X values('', 'jim''s ssn',0, 1 , 'jim''s son''s son', """jim''s"" hat", "\"jim''s secret\"")]
98
136
  assert_equal("INSERT INTO X values(?, ?,?, ? , ?, ?, ?)",
@@ -104,7 +142,7 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
104
142
  assert_equal(%q[SELECT * FROM `table_007` LIMIT ?],
105
143
  NewRelic::Agent::Database.obfuscate_sql(select))
106
144
  end
107
-
145
+
108
146
  def test_obfuscation_postgresql_integers_in_identifiers
109
147
  select = NewRelic::Agent::Database::Statement.new(%q[SELECT * FROM "table_007" LIMIT 1])
110
148
  select.adapter = :postgresql
@@ -112,25 +150,31 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
112
150
  NewRelic::Agent::Database.obfuscate_sql(select))
113
151
  end
114
152
 
153
+ def test_obfuscation_of_truncated_query
154
+ insert = "INSERT INTO data (blah) VALUES ('abcdefg..."
155
+ assert_equal("Query too large (over 16k characters) to safely obfuscate",
156
+ NewRelic::Agent::Database.obfuscate_sql(insert))
157
+ end
158
+
115
159
  def test_sql_obfuscation_filters
116
160
  NewRelic::Agent::Database.set_sql_obfuscator(:replace) do |string|
117
161
  "1" + string
118
162
  end
119
-
163
+
120
164
  sql = "SELECT * FROM TABLE 123 'jim'"
121
-
165
+
122
166
  assert_equal "1" + sql, NewRelic::Agent::Database.obfuscate_sql(sql)
123
-
167
+
124
168
  NewRelic::Agent::Database.set_sql_obfuscator(:before) do |string|
125
169
  "2" + string
126
170
  end
127
-
171
+
128
172
  assert_equal "12" + sql, NewRelic::Agent::Database.obfuscate_sql(sql)
129
-
173
+
130
174
  NewRelic::Agent::Database.set_sql_obfuscator(:after) do |string|
131
175
  string + "3"
132
176
  end
133
-
177
+
134
178
  assert_equal "12" + sql + "3", NewRelic::Agent::Database.obfuscate_sql(sql)
135
179
 
136
180
  NewRelic::Agent::Database::Obfuscator.instance.reset
@@ -140,11 +184,11 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
140
184
  foo_connection = mock('foo connection')
141
185
  bar_connection = mock('bar connection')
142
186
  NewRelic::Agent::Database::ConnectionManager.instance.instance_eval do
143
- @connections = { :foo => foo_connection, :bar => bar_connection }
187
+ @connections = { :foo => foo_connection, :bar => bar_connection }
144
188
  end
145
189
  foo_connection.expects(:disconnect!)
146
190
  bar_connection.expects(:disconnect!)
147
-
191
+
148
192
  NewRelic::Agent::Database.close_connections
149
193
  end
150
194
  end