Skip to content

Commit

Permalink
RUBY-969 Renaming our Thread class
Browse files Browse the repository at this point in the history
Some gems incorrectly monkey-patch Thread without fully qualifying the
system version (::Thread). Under some circumstances they were finding our
NewRelic::Agent::Thread class instead of ::Thread, so we've renamed our class
to avoid this happening.
  • Loading branch information
jasonrclark committed Dec 24, 2012
1 parent da6594f commit 84fb65c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/new_relic/agent/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def deferred_work!(connection_options)
# See #connect for a description of connection_options.
def start_worker_thread(connection_options = {})
log.debug "Creating Ruby Agent worker thread."
@worker_thread = NewRelic::Agent::Thread.new('Worker Loop') do
@worker_thread = NewRelic::Agent::AgentThread.new('Worker Loop') do
deferred_work!(connection_options)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/pipe_channel_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def register_pipe(id)
def start
return if @started == true
@started = true
@thread = NewRelic::Agent::Thread.new('Pipe Channel Manager') do
@thread = NewRelic::Agent::AgentThread.new('Pipe Channel Manager') do
now = nil
loop do
clean_up_pipes
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/stats_engine/samplers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def start_sampler_thread
# start up a thread that will periodically poll for metric samples
return if periodic_samplers.empty?

@sampler_thread = NewRelic::Agent::Thread.new('Sampler Tasks') do
@sampler_thread = NewRelic::Agent::AgentThread.new('Sampler Tasks') do
loop do
now = Time.now
begin
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/thread.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module NewRelic
module Agent

class Thread < ::Thread
class AgentThread < ::Thread
def initialize(label)
NewRelic::Agent.logger.debug("Creating New Relic thread: #{label}")
self[:newrelic_label] = label
Expand Down
8 changes: 4 additions & 4 deletions lib/new_relic/agent/thread_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ def initialize(profile_id, duration, interval, profile_agent_code)
end

def run
Thread.new('Thread Profiler') do
AgentThread.new('Thread Profiler') do
@start_time = now_in_millis

@worker_loop.run(@interval) do
NewRelic::Agent.instance.stats_engine.
record_supportability_metrics_timed("ThreadProfiler/PollingTime") do

@poll_count += 1
Thread.list.each do |t|
AgentThread.list.each do |t|
@sample_count += 1

bucket = Thread.bucket_thread(t, @profile_agent_code)
backtrace = Thread.scrub_backtrace(t, @profile_agent_code)
bucket = AgentThread.bucket_thread(t, @profile_agent_code)
backtrace = AgentThread.scrub_backtrace(t, @profile_agent_code)
aggregate(backtrace, @traces[bucket]) unless bucket == :ignore
end
end
Expand Down
24 changes: 12 additions & 12 deletions test/new_relic/agent/thread_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
class ThreadTest < Test::Unit::TestCase

def test_sets_label
t = NewRelic::Agent::Thread.new("labelled") {}
t = NewRelic::Agent::AgentThread.new("labelled") {}
assert_equal "labelled", t[:newrelic_label]
end

def test_bucket_thread_as_agent_when_profiling
t = NewRelic::Agent::Thread.new("labelled") {}
assert_equal :agent, NewRelic::Agent::Thread.bucket_thread(t, true)
t = NewRelic::Agent::AgentThread.new("labelled") {}
assert_equal :agent, NewRelic::Agent::AgentThread.bucket_thread(t, true)
end

def test_bucket_thread_as_agent_when_not_profiling
t = NewRelic::Agent::Thread.new("labelled") {}
assert_equal :ignore, NewRelic::Agent::Thread.bucket_thread(t, false)
t = NewRelic::Agent::AgentThread.new("labelled") {}
assert_equal :ignore, NewRelic::Agent::AgentThread.bucket_thread(t, false)
end

def test_bucket_thread_as_request
Expand All @@ -24,33 +24,33 @@ def test_bucket_thread_as_request
frame.request = "has a request"
t[:newrelic_metric_frame] = frame

assert_equal :request, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
assert_equal :request, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
end

def test_bucket_thread_as_background
t = ::Thread.new {}
frame = NewRelic::Agent::Instrumentation::MetricFrame.new
t[:newrelic_metric_frame] = frame

assert_equal :background, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
assert_equal :background, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
end

def test_bucket_thread_as_other_if_nil_frame
t = ::Thread.new {}
t[:newrelic_metric_frame] = nil

assert_equal :other, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
assert_equal :other, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
end

def test_bucket_thread_as_other
t = ::Thread.new {}
assert_equal :other, NewRelic::Agent::Thread.bucket_thread(t, DONT_CARE)
assert_equal :other, NewRelic::Agent::AgentThread.bucket_thread(t, DONT_CARE)
end

def test_runs_block
called = false

t = NewRelic::Agent::Thread.new("labelled") { called = true }
t = NewRelic::Agent::AgentThread.new("labelled") { called = true }
t.join

assert called
Expand All @@ -63,12 +63,12 @@ def test_runs_block
]

def test_scrubs_backtrace_when_not_profiling_agent_code
result = NewRelic::Agent::Thread.scrub_backtrace(stub(:backtrace => TRACE), false)
result = NewRelic::Agent::AgentThread.scrub_backtrace(stub(:backtrace => TRACE), false)
assert_equal [TRACE[0], TRACE[2]], result
end

def test_doesnt_scrub_backtrace_when_profiling_agent_code
result = NewRelic::Agent::Thread.scrub_backtrace(stub(:backtrace => TRACE), true)
result = NewRelic::Agent::AgentThread.scrub_backtrace(stub(:backtrace => TRACE), true)
assert_equal TRACE, result
end

Expand Down
6 changes: 3 additions & 3 deletions test/new_relic/agent/threaded_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ThreadedTest < Test::Unit::TestCase
def setup
@original_thread_class = NewRelic::Agent::Thread
@original_thread_class = NewRelic::Agent::AgentThread
swap_thread_class(FakeThread)
end

Expand All @@ -18,8 +18,8 @@ def default_test
private

def swap_thread_class(klass)
NewRelic::Agent.send(:remove_const, "Thread") if NewRelic::Agent.const_defined?("Thread")
NewRelic::Agent.const_set("Thread", klass)
NewRelic::Agent.send(:remove_const, "AgentThread") if NewRelic::Agent.const_defined?("AgentThread")
NewRelic::Agent.const_set("AgentThread", klass)
end
end

Expand Down

0 comments on commit 84fb65c

Please sign in to comment.