Skip to content

Commit

Permalink
Turn off const_missing patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Kayser committed Jan 12, 2009
1 parent 773227e commit 639c65e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/new_relic/agent/agent.rb
Expand Up @@ -8,7 +8,7 @@


# This must be turned off before we ship
VALIDATE_BACKGROUND_THREAD_LOADING = true
VALIDATE_BACKGROUND_THREAD_LOADING = false

# The NewRelic Agent collects performance data from ruby applications in realtime as the
# application runs, and periodically sends that data to the NewRelic server.
Expand Down Expand Up @@ -362,12 +362,13 @@ def launch_worker_thread

if VALIDATE_BACKGROUND_THREAD_LOADING
require 'new_relic/agent/patch_const_missing'
self.class.newrelic_enable_warning
end

@worker_thread = Thread.new do
begin
if VALIDATE_BACKGROUND_THREAD_LOADING
self.class.new_relic_set_agent_thread(Thread.current)
self.class.newrelic_set_agent_thread(Thread.current)
end
run_worker_loop
rescue IgnoreSilentlyException
Expand Down
32 changes: 21 additions & 11 deletions lib/new_relic/agent/patch_const_missing.rb
@@ -1,21 +1,31 @@
# This class is for debugging purposes only.
#
class Class
class Module
@@newrelic_agent_thread = nil
def new_relic_const_missing(*args)
if Thread.current == @agent_thread
STDERR.puts "Agent background thread shouldn't be calling const_missing!!!"
STDERR.puts caller.join("\n")
exit -1
if Thread.current == @@newrelic_agent_thread
msg = "Agent background thread shouldn't be calling const_missing (#{args.inspect}) \n"
msg << caller[0..4].join(" \n")
NewRelic::Config.instance.log.warn msg
end

original_const_missing(*args)
end

alias_method :original_const_missing, :const_missing
alias_method :const_missing, :new_relic_const_missing
def newrelic_enable_warning
Module.class_eval do
if !defined?(original_const_missing)
alias_method :original_const_missing, :const_missing
alias_method :const_missing, :new_relic_const_missing
end
end
end
def newrelic_disable_warning
Module.class_eval do
alias_method :const_missing, :original_const_missing if defined?(original_const_missing)
end
end

def new_relic_set_agent_thread(thread)
@agent_thread = thread
def newrelic_set_agent_thread(thread)
@@newrelic_agent_thread = thread
end
end
15 changes: 15 additions & 0 deletions test/new_relic/agent/tc_agent.rb
Expand Up @@ -57,6 +57,21 @@ def test_setup_log_default
@agent.shutdown
end

def test_classloading_patch
require 'new_relic/agent/patch_const_missing'
NewRelic::Agent::Agent.newrelic_set_agent_thread(Thread.current)
# try loading some non-existent class
NewRelic::Config.instance.log.expects(:warn).at_least_once.with{|args| args =~ /calling const_missing.*:FooBar/}
NewRelic::Config.instance.log.expects(:warn).with{|args| args =~ /calling const_missing.*:FooBaz/}.never
NewRelic::Agent::Agent.newrelic_enable_warning
assert_raise NameError do
FooBar::Bat
end
NewRelic::Agent::Agent.newrelic_disable_warning
assert_raise NameError do
FooBaz::Bat
end
end
def test_info
props = NewRelic::Config.instance.app_config_info
list = props.assoc('Plugin List').last.sort
Expand Down

0 comments on commit 639c65e

Please sign in to comment.