Class: NewRelic::Agent::Threading::AgentThread

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/threading/agent_thread.rb

Class Method Summary collapse

Class Method Details

.backing_thread_classObject



71
72
73
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 71

def self.backing_thread_class
  @backing_thread_class
end

.backing_thread_class=(clazz) ⇒ Object



75
76
77
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 75

def self.backing_thread_class=(clazz)
  @backing_thread_class = clazz
end

.bucket_thread(thread, profile_agent_code) ⇒ Object

THREAD_LOCAL_ACCESS



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 38

def self.bucket_thread(thread, profile_agent_code) # THREAD_LOCAL_ACCESS
  if thread.key?(:newrelic_label)
    profile_agent_code ? :agent : :ignore
  else
    state = Tracer.state_for(thread)
    txn = state.current_transaction

    if txn && !txn.recording_web_transaction?
      :background
    elsif txn&.recording_web_transaction?
      :request
    else
      :other
    end
  end
end

.create(label, &blk) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 9

def self.create(label, &blk)
  ::NewRelic::Agent.logger.debug("Creating AgentThread: #{label}")
  wrapped_blk = proc do
    if (txn = ::NewRelic::ThreadLocalStorage[:newrelic_tracer_state]&.current_transaction)
      ::NewRelic::Agent.logger.warn("AgentThread created with current transaction #{txn.best_name}")
    end
    begin
      yield
    rescue => e
      ::NewRelic::Agent.logger.error("AgentThread #{label} exited with error", e)
    rescue Exception => e
      ::NewRelic::Agent.logger.error("AgentThread #{label} exited with exception. Re-raising in case of interrupt.", e)
      raise
    ensure
      ::NewRelic::Agent.logger.debug("Exiting AgentThread: #{label}")
    end
  end
  thread = nil
  NewRelic::Agent.disable_all_tracing { thread = backing_thread_class.new(&wrapped_blk) }
  thread[:newrelic_label] = label
  thread
end

.listObject

Simplifies testing if we don’t directly use ::Thread.list, so keep the accessor for it here on AgentThread to use and stub.



34
35
36
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 34

def self.list
  backing_thread_class.list
end

.scrub_backtrace(thread, profile_agent_code) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/new_relic/agent/threading/agent_thread.rb', line 55

def self.scrub_backtrace(thread, profile_agent_code)
  begin
    bt = thread.backtrace
  rescue Exception => e
    ::NewRelic::Agent.logger.debug("Failed to backtrace #{thread.inspect}: #{e.class.name}: #{e.to_s}")
  end
  return nil unless bt

  bt.reject! { |t| t.include?('/newrelic_rpm-') } unless profile_agent_code
  bt
end