newrelic_rpm 3.7.0.174.beta → 3.7.0.177

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG +27 -4
  3. data/bin/nrdebug +10 -4
  4. data/lib/new_relic/agent.rb +33 -11
  5. data/lib/new_relic/agent/agent.rb +83 -120
  6. data/lib/new_relic/agent/agent_logger.rb +28 -16
  7. data/lib/new_relic/agent/audit_logger.rb +3 -4
  8. data/lib/new_relic/agent/autostart.rb +20 -8
  9. data/lib/new_relic/agent/commands/agent_command_router.rb +26 -17
  10. data/lib/new_relic/agent/commands/thread_profiler_session.rb +2 -2
  11. data/lib/new_relic/agent/configuration/default_source.rb +146 -59
  12. data/lib/new_relic/agent/configuration/manager.rb +3 -3
  13. data/lib/new_relic/agent/cross_app_monitor.rb +15 -40
  14. data/lib/new_relic/agent/cross_app_tracing.rb +20 -12
  15. data/lib/new_relic/agent/database.rb +24 -0
  16. data/lib/new_relic/agent/error_collector.rb +6 -2
  17. data/lib/new_relic/agent/instrumentation/merb/controller.rb +3 -1
  18. data/lib/new_relic/agent/javascript_instrumentor.rb +187 -0
  19. data/lib/new_relic/agent/new_relic_service.rb +30 -22
  20. data/lib/new_relic/agent/obfuscator.rb +48 -0
  21. data/lib/new_relic/agent/request_sampler.rb +5 -13
  22. data/lib/new_relic/agent/shim_agent.rb +1 -0
  23. data/lib/new_relic/agent/sql_sampler.rb +15 -5
  24. data/lib/new_relic/agent/stats_engine/metric_stats.rb +9 -4
  25. data/lib/new_relic/agent/transaction.rb +0 -1
  26. data/lib/new_relic/agent/transaction_sampler.rb +28 -16
  27. data/lib/new_relic/agent/transaction_state.rb +9 -0
  28. data/lib/new_relic/agent/transaction_timings.rb +5 -1
  29. data/lib/new_relic/agent/worker_loop.rb +0 -10
  30. data/lib/new_relic/cli/deployments.rb +1 -1
  31. data/lib/new_relic/control/instance_methods.rb +1 -1
  32. data/lib/new_relic/helper.rb +3 -1
  33. data/lib/new_relic/rack/browser_monitoring.rb +1 -2
  34. data/lib/new_relic/transaction_sample.rb +11 -13
  35. data/lib/newrelic_rpm.rb +1 -0
  36. data/test/agent_helper.rb +20 -5
  37. data/test/environments/lib/environments/runner.rb +1 -0
  38. data/test/helpers/file_searching.rb +28 -0
  39. data/test/multiverse/lib/multiverse/suite.rb +36 -19
  40. data/test/multiverse/suites/agent_only/collector_exception_handling_test.rb +49 -0
  41. data/test/multiverse/suites/agent_only/http_response_code_test.rb +2 -2
  42. data/test/multiverse/suites/agent_only/rum_instrumentation_test.rb +4 -2
  43. data/test/multiverse/suites/agent_only/service_timeout_test.rb +1 -1
  44. data/test/multiverse/suites/agent_only/set_transaction_name_test.rb +7 -4
  45. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +2 -1
  46. data/test/multiverse/suites/rails/error_tracing_test.rb +34 -4
  47. data/test/multiverse/suites/rails/ignore_test.rb +1 -1
  48. data/test/multiverse/suites/rails/request_statistics_test.rb +1 -3
  49. data/test/multiverse/suites/sequel/sequel_instrumentation_test.rb +10 -7
  50. data/test/multiverse/suites/sinatra/ignoring_test.rb +1 -1
  51. data/test/new_relic/agent/agent/start_worker_thread_test.rb +1 -1
  52. data/test/new_relic/agent/agent_logger_test.rb +108 -114
  53. data/test/new_relic/agent/agent_test.rb +139 -21
  54. data/test/new_relic/agent/audit_logger_test.rb +22 -20
  55. data/test/new_relic/agent/autostart_test.rb +3 -2
  56. data/test/new_relic/agent/commands/agent_command_router_test.rb +51 -32
  57. data/test/new_relic/agent/configuration/default_source_test.rb +8 -2
  58. data/test/new_relic/agent/configuration/manager_test.rb +5 -1
  59. data/test/new_relic/agent/configuration/orphan_configuration_test.rb +57 -0
  60. data/test/new_relic/agent/cross_app_monitor_test.rb +10 -26
  61. data/test/new_relic/agent/database_test.rb +32 -0
  62. data/test/new_relic/agent/error_collector_test.rb +33 -16
  63. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +88 -71
  64. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  65. data/test/new_relic/agent/javascript_instrumentor_test.rb +341 -0
  66. data/test/new_relic/agent/memcache_instrumentation_test.rb +91 -89
  67. data/test/new_relic/agent/method_tracer_test.rb +1 -1
  68. data/test/new_relic/agent/obfuscator_test.rb +77 -0
  69. data/test/new_relic/agent/pipe_channel_manager_test.rb +5 -5
  70. data/test/new_relic/agent/pipe_service_test.rb +1 -1
  71. data/test/new_relic/agent/request_sampler_test.rb +21 -11
  72. data/test/new_relic/agent/sql_sampler_test.rb +52 -8
  73. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +6 -6
  74. data/test/new_relic/agent/stats_engine_test.rb +18 -2
  75. data/test/new_relic/agent/transaction_sampler_test.rb +98 -53
  76. data/test/new_relic/agent/transaction_state_test.rb +44 -0
  77. data/test/new_relic/agent/transaction_test.rb +1 -1
  78. data/test/new_relic/agent/transaction_timings_test.rb +15 -5
  79. data/test/new_relic/agent/worker_loop_test.rb +0 -9
  80. data/test/new_relic/agent_test.rb +9 -21
  81. data/test/new_relic/data_container_tests.rb +72 -0
  82. data/test/new_relic/fake_collector.rb +69 -20
  83. data/test/new_relic/http_client_test_cases.rb +17 -2
  84. data/test/new_relic/license_test.rb +6 -15
  85. data/test/new_relic/multiverse_helpers.rb +2 -3
  86. data/test/new_relic/rack/browser_monitoring_test.rb +15 -37
  87. data/test/new_relic/transaction_sample_test.rb +92 -62
  88. data/test/performance/suites/rum_autoinsertion.rb +0 -3
  89. data/test/rum/x_ua_meta_tag_spaces_around_equals.result.html +10 -0
  90. data/test/rum/x_ua_meta_tag_spaces_around_equals.source.html +10 -0
  91. data/test/test_helper.rb +9 -5
  92. metadata +29 -11
  93. metadata.gz.sig +0 -0
  94. data/lib/new_relic/agent/beacon_configuration.rb +0 -37
  95. data/lib/new_relic/agent/browser_monitoring.rb +0 -257
  96. data/test/new_relic/agent/beacon_configuration_test.rb +0 -44
  97. data/test/new_relic/agent/browser_monitoring_test.rb +0 -474
@@ -8,9 +8,8 @@ require 'new_relic/agent/null_logger'
8
8
 
9
9
  class AuditLoggerTest < Test::Unit::TestCase
10
10
  def setup
11
- @config = {
12
- :'audit_log.enabled' => true
13
- }
11
+ NewRelic::Agent.config.apply_config(:'audit_log.enabled' => true)
12
+
14
13
  @uri = "http://really.notreal"
15
14
  @marshaller = NewRelic::Agent::NewRelicService::Marshaller.new
16
15
  @hostname = 'dummyhost'
@@ -26,7 +25,7 @@ class AuditLoggerTest < Test::Unit::TestCase
26
25
 
27
26
  def setup_fake_logger
28
27
  @fakelog = StringIO.new
29
- @logger = NewRelic::Agent::AuditLogger.new(@config)
28
+ @logger = NewRelic::Agent::AuditLogger.new
30
29
  @logger.stubs(:ensure_log_path).returns(@fakelog)
31
30
  end
32
31
 
@@ -37,22 +36,24 @@ class AuditLoggerTest < Test::Unit::TestCase
37
36
  end
38
37
 
39
38
  def test_never_setup_if_disabled
40
- config = { :'audit_log.enabled' => false }
41
- logger = NewRelic::Agent::AuditLogger.new(config)
42
- logger.log_request(@uri, "hi there", @marshaller)
43
- assert(!logger.setup?, "Expected logger to not have been setup")
39
+ with_config(:'audit_log.enabled' => false) do
40
+ logger = NewRelic::Agent::AuditLogger.new
41
+ logger.log_request(@uri, "hi there", @marshaller)
42
+ assert(!logger.setup?, "Expected logger to not have been setup")
43
+ end
44
44
  end
45
45
 
46
46
  def test_never_prepare_if_disabled
47
- config = { :'audit_log.enabled' => false }
48
- logger = NewRelic::Agent::AuditLogger.new(config)
49
- marshaller = NewRelic::Agent::NewRelicService::Marshaller.new
50
- marshaller.expects(:prepare).never
51
- logger.log_request(@uri, "hi there", @marshaller)
47
+ with_config(:'audit_log.enabled' => false) do
48
+ logger = NewRelic::Agent::AuditLogger.new
49
+ marshaller = NewRelic::Agent::NewRelicService::Marshaller.new
50
+ marshaller.expects(:prepare).never
51
+ logger.log_request(@uri, "hi there", @marshaller)
52
+ end
52
53
  end
53
54
 
54
55
  def test_log_formatter
55
- formatter = NewRelic::Agent::AuditLogger.new(@config).log_formatter
56
+ formatter = NewRelic::Agent::AuditLogger.new.log_formatter
56
57
  time = '2012-01-01 00:00:00'
57
58
  msg = 'hello'
58
59
  result = formatter.call(Logger::INFO, time, 'bleh', msg)
@@ -61,16 +62,17 @@ class AuditLoggerTest < Test::Unit::TestCase
61
62
  end
62
63
 
63
64
  def test_ensure_path_returns_nil_with_bogus_path
64
- opts = { :'audit_log.path' => '/really/really/not/a/path' }
65
- FileUtils.stubs(:mkdir_p).raises(SystemCallError, "i'd rather not")
66
- logger = NewRelic::Agent::AuditLogger.new(@config.merge(opts))
67
- assert_nil(logger.ensure_log_path)
65
+ with_config(:'audit_log.path' => '/really/really/not/a/path') do
66
+ FileUtils.stubs(:mkdir_p).raises(SystemCallError, "i'd rather not")
67
+ logger = NewRelic::Agent::AuditLogger.new
68
+ assert_nil(logger.ensure_log_path)
69
+ end
68
70
  end
69
71
 
70
72
  def test_setup_logger_creates_null_logger_when_ensure_path_fails
71
73
  null_logger = NewRelic::Agent::NullLogger.new
72
74
  NewRelic::Agent::NullLogger.expects(:new).returns(null_logger)
73
- logger = NewRelic::Agent::AuditLogger.new(@config)
75
+ logger = NewRelic::Agent::AuditLogger.new
74
76
  logger.stubs(:ensure_log_path).returns(nil)
75
77
  assert_nothing_raised do
76
78
  logger.setup_logger
@@ -79,7 +81,7 @@ class AuditLoggerTest < Test::Unit::TestCase
79
81
  end
80
82
 
81
83
  def test_log_request_captures_system_call_errors
82
- logger = NewRelic::Agent::AuditLogger.new(@config)
84
+ logger = NewRelic::Agent::AuditLogger.new
83
85
  dummy_sink = StringIO.new
84
86
  dummy_sink.stubs(:write).raises(SystemCallError, "nope")
85
87
  logger.stubs(:ensure_log_path).returns(dummy_sink)
@@ -42,8 +42,9 @@ class AutostartTest < Test::Unit::TestCase
42
42
  db:migrate db:migrate:status db:rollback db:schema:cache:clear
43
43
  db:schema:cache:dump db:schema:dump db:schema:load db:seed db:setup
44
44
  db:structure:dump db:version doc:app log:clear middleware notes notes:custom
45
- rails:template rails:update routes secret spec spec:controllers spec:helpers
46
- spec:models spec:rcov stats test test:all test:all:db test:recent test:single
45
+ rails:template rails:update routes secret spec spec:features spec:requests
46
+ spec:controllers spec:helpers spec:models spec:views spec:routing
47
+ spec:rcov stats test test:all test:all:db test:recent test:single
47
48
  test:uncommitted time:zones:all tmp:clear tmp:create |.each do |task|
48
49
 
49
50
  define_method("test_agent_wont_autostart_if_top_level_rake_task_is_#{task}") do
@@ -3,6 +3,8 @@
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
5
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','data_container_tests'))
7
+
6
8
  require 'new_relic/agent/commands/agent_command_router'
7
9
  require 'new_relic/agent/commands/xray_session'
8
10
 
@@ -36,8 +38,9 @@ class AgentCommandRouterTest < Test::Unit::TestCase
36
38
  @service = stub
37
39
  NewRelic::Agent.agent.stubs(:service).returns(@service)
38
40
  @calls = []
41
+ @events = NewRelic::Agent::EventListener.new
39
42
 
40
- @agent_commands = NewRelic::Agent::Commands::AgentCommandRouter.new
43
+ @agent_commands = NewRelic::Agent::Commands::AgentCommandRouter.new(@events)
41
44
  @agent_commands.handlers["bazzle"] = Proc.new { |args| handle_bazzle_command(args) }
42
45
  @agent_commands.handlers["boom"] = Proc.new { |args| handle_boom_command(args) }
43
46
  end
@@ -46,6 +49,22 @@ class AgentCommandRouterTest < Test::Unit::TestCase
46
49
  agent_commands.backtrace_service.worker_thread.join if agent_commands.backtrace_service.worker_thread
47
50
  end
48
51
 
52
+ # Helpers for DataContainerTests
53
+
54
+ def create_container
55
+ @agent_commands
56
+ end
57
+
58
+ def max_data_items
59
+ 1
60
+ end
61
+
62
+ def populate_container(container, n)
63
+ start_profile('duration' => 1.0)
64
+ advance_time(1.1)
65
+ agent_commands.backtrace_service.worker_thread.join if agent_commands.backtrace_service.worker_thread
66
+ end
67
+
49
68
  # General command routing
50
69
 
51
70
  def test_check_for_and_handle_agent_commands_dispatches_command
@@ -108,29 +127,27 @@ class AgentCommandRouterTest < Test::Unit::TestCase
108
127
  # Harvesting tests
109
128
 
110
129
  if NewRelic::Agent::Threading::BacktraceService.is_supported?
130
+ include NewRelic::BasicDataContainerTests
111
131
 
112
- DISCONNECTING = true
113
- NOT_DISCONNECTING = false
114
-
115
- def test_harvest_data_to_send_not_started
116
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
117
- assert_equal({}, result)
132
+ def test_harvest_not_started
133
+ result = agent_commands.harvest!
134
+ assert_equal([], result)
118
135
  end
119
136
 
120
- def test_harvest_data_to_send_with_profile_in_progress
137
+ def test_harvest_with_profile_in_progress
121
138
  start_profile('duration' => 1.0)
122
139
 
123
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
124
- assert_equal({}, result)
140
+ result = agent_commands.harvest!
141
+ assert_equal([], result)
125
142
  end
126
143
 
127
- def test_harvest_data_to_send_with_profile_completed
144
+ def test_harvest_with_profile_completed
128
145
  start_profile('duration' => 1.0)
129
146
 
130
147
  advance_time(1.1)
131
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
148
+ result = agent_commands.harvest!
132
149
 
133
- assert_not_nil result[:profile_data]
150
+ assert_not_empty result
134
151
  end
135
152
 
136
153
  def test_can_stop_multiple_times_safely
@@ -139,8 +156,8 @@ class AgentCommandRouterTest < Test::Unit::TestCase
139
156
  advance_time(1.1)
140
157
  agent_commands.thread_profiler_session.stop(true)
141
158
 
142
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
143
- assert_not_nil result[:profile_data]
159
+ result = agent_commands.harvest!
160
+ assert_not_empty result
144
161
  end
145
162
 
146
163
  def test_transmits_after_forced_stop
@@ -148,34 +165,36 @@ class AgentCommandRouterTest < Test::Unit::TestCase
148
165
 
149
166
  agent_commands.thread_profiler_session.stop(true)
150
167
 
151
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
152
- assert_not_nil result[:profile_data]
168
+ result = agent_commands.harvest!
169
+ assert_not_empty result
153
170
  end
154
171
 
155
- def test_harvest_data_to_send_with_no_profile_disconnecting
156
- result = agent_commands.harvest_data_to_send(DISCONNECTING)
157
- assert_nil result[:profile_data]
172
+ def test_harvest_following_before_shutdown_with_no_profile
173
+ @events.notify(:before_shutdown)
174
+ result = agent_commands.harvest!
175
+ assert_empty result
158
176
  end
159
177
 
160
- def test_harvest_data_to_send_with_profile_in_progress_but_disconnecting
178
+ def test_harvest_following_before_shutdown_with_active_profile
161
179
  start_profile('duration' => 1.0)
162
180
 
163
- result = agent_commands.harvest_data_to_send(DISCONNECTING)
164
- assert_not_nil result[:profile_data]
181
+ @events.notify(:before_shutdown)
182
+ result = agent_commands.harvest!
183
+ assert_not_empty result
165
184
  end
166
185
 
167
- def test_harvest_data_to_send_with_xray_sessions_in_progress
186
+ def test_harvest_with_xray_sessions_in_progress
168
187
  start_xray_session(123)
169
188
  start_xray_session(456)
170
189
 
171
190
  sample_on_profiles
172
191
 
173
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
192
+ result = agent_commands.harvest!
174
193
 
175
- assert_equal 2, result[:profile_data].length
194
+ assert_equal 2, result.length
176
195
  end
177
196
 
178
- def test_harvest_data_to_send_with_xray_sessions_and_thread_profile_in_progress
197
+ def test_harvest_with_xray_sessions_and_thread_profile_in_progress
179
198
  start_xray_session(123)
180
199
  start_xray_session(456)
181
200
 
@@ -183,12 +202,12 @@ class AgentCommandRouterTest < Test::Unit::TestCase
183
202
 
184
203
  sample_on_profiles
185
204
 
186
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
205
+ result = agent_commands.harvest!
187
206
 
188
- assert_equal 2, result[:profile_data].length
207
+ assert_equal 2, result.length
189
208
  end
190
209
 
191
- def test_harvest_data_to_send_with_xray_sessions_and_completed_thread_profile
210
+ def test_harvest_with_xray_sessions_and_completed_thread_profile
192
211
  start_xray_session(123)
193
212
  start_xray_session(456)
194
213
 
@@ -197,9 +216,9 @@ class AgentCommandRouterTest < Test::Unit::TestCase
197
216
  sample_on_profiles
198
217
  advance_time(1.1)
199
218
 
200
- result = agent_commands.harvest_data_to_send(NOT_DISCONNECTING)
219
+ result = agent_commands.harvest!
201
220
 
202
- assert_equal 3, result[:profile_data].length
221
+ assert_equal 3, result.length
203
222
  end
204
223
 
205
224
  end
@@ -35,8 +35,14 @@ module NewRelic::Agent::Configuration
35
35
  NewRelic::Control.instance.local_env.stubs(:discovered_dispatcher).returns(:unicorn)
36
36
 
37
37
  @default_source.keys.each do |key|
38
- default_value_class = get_config_value_class(fetch_config_value(key))
39
- assert_equal @defaults[key][:type], default_value_class, "Config setting #{key} does not have the correct type."
38
+ actual_type = get_config_value_class(fetch_config_value(key))
39
+ expected_type = @defaults[key][:type]
40
+
41
+ if @defaults[key][:allow_nil]
42
+ assert [NilClass, expected_type].include?(actual_type), "Default value for #{key} should be NilClass or #{expected_type}, is #{actual_type}."
43
+ else
44
+ assert_equal expected_type, actual_type, "Default value for #{key} should be #{expected_type}, is #{actual_type}."
45
+ end
40
46
  end
41
47
  end
42
48
 
@@ -10,7 +10,9 @@ require 'new_relic/agent/threading/backtrace_service'
10
10
  module NewRelic::Agent::Configuration
11
11
  class ManagerTest < Test::Unit::TestCase
12
12
  def setup
13
- @manager = NewRelic::Agent::Configuration::Manager.new
13
+ # Defaults look up against the shared config, so reset and use it
14
+ NewRelic::Agent.reset_config
15
+ @manager = NewRelic::Agent.config
14
16
  end
15
17
 
16
18
  def test_should_use_indifferent_access
@@ -102,6 +104,8 @@ module NewRelic::Agent::Configuration
102
104
  manager.apply_config({:license_key => 'wrong'}, 1)
103
105
 
104
106
  assert_equal 'right', manager['license_key']
107
+ ensure
108
+ ENV.delete('NEWRELIC_LICENSE_KEY')
105
109
  end
106
110
 
107
111
  def test_config_values_should_be_memoized
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+
7
+ class OrphanedConfigTest < Test::Unit::TestCase
8
+ include NewRelic::TestHelpers::FileSearching
9
+
10
+ def setup
11
+ @default_keys = ::NewRelic::Agent::Configuration::DEFAULTS.keys
12
+ end
13
+
14
+ def test_all_agent_config_keys_are_declared_in_default_source
15
+ non_test_files = all_rb_files.reject { |filename| filename.include? 'test.rb' }
16
+
17
+ non_test_files.each do |file|
18
+ lines = File.read(file).split("\n")
19
+
20
+ lines.each_with_index do |line, index|
21
+ config_match = line.match(/Agent\.config\[:['"]?([a-z\._]+)['"]?\]/)
22
+ next unless config_match
23
+
24
+ config_keys = config_match.captures.map do |key|
25
+ key.gsub("'", "").to_sym
26
+ end
27
+
28
+ config_keys.each do |key|
29
+ msg = "#{file}:#{index} - Configuration key #{key} is not described in default_source.rb.\n"
30
+ assert @default_keys.include?(key), msg
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def test_all_default_source_config_keys_are_used_in_the_agent
37
+ non_test_files = all_rb_files.reject { |filename| filename.include? 'test.rb' }
38
+
39
+ non_test_files.each do |file|
40
+ lines = File.read(file).split("\n")
41
+
42
+ lines.each_with_index do |line, index|
43
+ captures = []
44
+ captures << line.scan(/Agent\.config\[:['"]?([a-z\._]+)['"]?\]/)
45
+ captures << line.scan(/register_callback\(:['"]?([a-z\._]+)['"]?\)/)
46
+
47
+ next if captures.empty?
48
+
49
+ captures.flatten.map do |key|
50
+ @default_keys.delete key.gsub("'", "").to_sym
51
+ end
52
+ end
53
+ end
54
+
55
+ assert_empty @default_keys
56
+ end
57
+ end
@@ -40,7 +40,7 @@ module NewRelic::Agent
40
40
  }
41
41
 
42
42
  NewRelic::Agent.config.apply_config( @config )
43
- @monitor.register_event_listeners
43
+ @monitor.on_finished_configuring
44
44
  NewRelic::Agent::TransactionState.get.request_guid = TRANSACTION_GUID
45
45
  end
46
46
 
@@ -101,19 +101,21 @@ module NewRelic::Agent
101
101
  end
102
102
 
103
103
  def test_doesnt_add_header_if_no_id_on_agent
104
- # Since a +nil+ will make it fall back to the config installed in setup,
105
- # we need to remove that first in order to test the no-id case
106
- newconfig = @config.merge( :cross_process_id => nil )
107
- NewRelic::Agent.config.remove_config( @config )
108
-
109
- with_config( newconfig ) do
104
+ with_config( :cross_process_id => '' ) do
110
105
  when_request_runs
111
106
  assert_nil response_app_data
112
107
  end
113
108
  end
114
109
 
115
110
  def test_doesnt_add_header_if_config_disabled
116
- with_config(:"cross_application_tracer.enabled" => false) do
111
+ with_config(:"cross_application_tracer.enabled" => false, :cross_application_tracing => false) do
112
+ when_request_runs
113
+ assert_nil response_app_data
114
+ end
115
+ end
116
+
117
+ def test_doesnt_add_header_if_missing_encoding_key
118
+ with_config( :encoding_key => '' ) do
117
119
  when_request_runs
118
120
  assert_nil response_app_data
119
121
  end
@@ -178,24 +180,6 @@ module NewRelic::Agent
178
180
  when_request_runs(for_id(''))
179
181
  end
180
182
 
181
- def test_decoding_blank
182
- assert_equal "",
183
- NewRelic::Agent::CrossAppMonitor::EncodingFunctions.decode_with_key( 'querty', "" )
184
- end
185
-
186
- def test_encode_with_nil_uses_empty_key
187
- assert_equal "querty",
188
- NewRelic::Agent::CrossAppMonitor::EncodingFunctions.encode_with_key( nil, 'querty' )
189
- end
190
-
191
- def test_encoding_functions_can_roundtrip_utf8_text
192
- str = 'Анастасі́я Олексі́ївна Каме́нських'
193
- encoded = NewRelic::Agent::CrossAppMonitor::EncodingFunctions.obfuscate_with_key( 'potap', str )
194
- decoded = NewRelic::Agent::CrossAppMonitor::EncodingFunctions.decode_with_key( 'potap', encoded )
195
- decoded.force_encoding( 'utf-8' ) if decoded.respond_to?( :force_encoding )
196
- assert_equal str, decoded
197
- end
198
-
199
183
  def test_setting_response_headers_freezes_transaction_name
200
184
  in_transaction do
201
185
  assert !NewRelic::Agent::Transaction.current.name_frozen?
@@ -214,4 +214,36 @@ class NewRelic::Agent::DatabaseTest < Test::Unit::TestCase
214
214
  result = NewRelic::Agent::Database.obfuscate_sql("SELECT * FROM \"table\" WHERE \"col\" = 'value'")
215
215
  assert_equal expected, result
216
216
  end
217
+
218
+ def test_capture_query_short_query
219
+ query = 'a query'
220
+ assert_equal(query, NewRelic::Agent::Database.capture_query(query))
221
+ end
222
+
223
+ def test_capture_query_long_query
224
+ query = 'a' * NewRelic::Agent::Database::MAX_QUERY_LENGTH
225
+ truncated_query = NewRelic::Agent::Database.capture_query(query)
226
+ assert_equal('a' * (NewRelic::Agent::Database::MAX_QUERY_LENGTH - 3) + '...', truncated_query)
227
+ end
228
+
229
+ INVALID_UTF8_STRING = (''.respond_to?(:force_encoding) ? "\x80".force_encoding('UTF-8') : "\x80")
230
+
231
+ def test_capture_query_mis_encoded
232
+ query = INVALID_UTF8_STRING
233
+ original_encoding = encoding_from_string(query)
234
+ expected_query = INVALID_UTF8_STRING.dup
235
+ expected_query.force_encoding('ASCII-8BIT') if expected_query.respond_to?(:force_encoding)
236
+ captured = NewRelic::Agent::Database.capture_query(query)
237
+ assert_equal(original_encoding, encoding_from_string(query)) # input query encoding should remain untouched
238
+ assert_equal(expected_query, captured)
239
+ end
240
+
241
+ # Ruby 1.8 doesn't have String#encoding
242
+ def encoding_from_string(str)
243
+ if str.respond_to?(:encoding)
244
+ str.encoding
245
+ else
246
+ nil
247
+ end
248
+ end
217
249
  end