Permalink
Browse files

RUBY-893 better handling of unknown application names

  • Loading branch information...
gnarg authored and rsaul committed Sep 11, 2012
1 parent a9b240c commit 308569ba82b1017df527e2677073196f66b6993b
@@ -315,7 +315,12 @@ def log_dispatcher
# Logs the configured application names
def log_app_names
- log.info "Application: #{Agent.config.app_names.join(", ")}"
+ names = Agent.config.app_names
+ if names.respond_to?(:any?) && names.any?
+ log.info "Application: #{names.join(", ")}"
+ else
+ log.error 'Unable to determine application name. Please set the application name in your newrelic.yml or in a NEW_RELIC_APP_NAME environment variable.'
+ end
end
# Connecting in the foreground blocks further startup of the
@@ -9,6 +9,11 @@ module Instance
def config
@@manager ||= Manager.new
end
+
+ # for testing
+ def reset_config
+ @@manager = Manager.new
+ end
end
class DottedHash < ::Hash
@@ -89,6 +89,7 @@ def app_names
case self[:app_name]
when Array then self[:app_name]
when String then self[:app_name].split(';')
+ else []
end
end
@@ -3,6 +3,16 @@ class NewRelic::Agent::Agent::StartTest < Test::Unit::TestCase
require 'new_relic/agent/agent'
include NewRelic::Agent::Agent::Start
+ def setup
+ ENV['NEW_RELIC_APP_NAME'] = 'start_test'
+ NewRelic::Agent.reset_config
+ end
+
+ def teardown
+ ENV['NEW_RELIC_APP_NAME'] = nil
+ NewRelic::Agent.reset_config
+ end
+
def test_already_started_positive
control = mocked_control
control.expects(:log!).with("Agent Started Already!", :error)
@@ -43,14 +53,39 @@ def test_log_dispatcher_negative
end
end
- def test_log_app_names
+ def test_log_app_names_string
with_config(:app_name => 'zam;zam;zabam') do
log = mocked_log
log.expects(:info).with("Application: zam, zam, zabam")
log_app_names
end
end
+ def test_log_app_names_array
+ with_config(:app_name => ['zam', 'zam', 'zabam']) do
+ log = mocked_log
+ log.expects(:info).with("Application: zam, zam, zabam")
+ log_app_names
+ end
+ end
+
+ def test_log_app_names_with_env_var
+ # bad app name after env - used to cover the yaml config
+ with_config({:app_name => false}, 1) do
+ log = mocked_log
+ log.expects(:info).with("Application: start_test") # set in setup
+ log_app_names
+ end
+ end
+
+ def test_log_app_names_with_unknown
+ with_config(:app_name => false) do
+ log = mocked_log
+ log.expects(:error).with('Unable to determine application name. Please set the application name in your newrelic.yml or in a NEW_RELIC_APP_NAME environment variable.')
+ log_app_names
+ end
+ end
+
def test_check_config_and_start_agent_disabled
self.expects(:monitoring?).returns(false)
check_config_and_start_agent
@@ -123,6 +123,23 @@ def test_replacing_a_layer_by_class
.index(NewRelic::Agent::Configuration::ManualSource)
end
+ def test_app_names_single
+ @manager.apply_config(:app_name => 'test')
+ assert_equal ['test'], @manager.app_names
+ end
+
+ def test_app_names_list
+ @manager.apply_config(:app_name => 'test0;test1;test2')
+ assert_equal ['test0', 'test1', 'test2'], @manager.app_names
+ end
+
+ def test_app_names_nil
+ # nil has special meaning, so we emulate with an object that
+ # will not respond as expected
+ @manager.apply_config(:app_name => Object.new)
+ assert_equal [], @manager.app_names
+ end
+
class TestSource < ::Hash
def test_config_accessor
'some value'

0 comments on commit 308569b

Please sign in to comment.