Class: NewRelic::Agent::Commands::AgentCommandRouter
- Inherits:
-
Object
- Object
- NewRelic::Agent::Commands::AgentCommandRouter
show all
- Defined in:
- lib/new_relic/agent/commands/agent_command_router.rb
Defined Under Namespace
Classes: AgentCommandError
Constant Summary
collapse
- SUCCESS_RESULT =
NewRelic::EMPTY_HASH
- ERROR_KEY =
'error'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AgentCommandRouter.
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 22
def initialize(event_listener = nil)
@handlers = Hash.new { |*| proc { |cmd| self.unrecognized_agent_command(cmd) } }
@backtrace_service = Threading::BacktraceService.new(event_listener)
@thread_profiler_session = ThreadProfilerSession.new(@backtrace_service)
@handlers['start_profiler'] = proc { |cmd| thread_profiler_session.handle_start_command(cmd) }
@handlers['stop_profiler'] = proc { |cmd| thread_profiler_session.handle_stop_command(cmd) }
event_listener&.subscribe(:before_shutdown, &method(:on_before_shutdown))
end
|
Instance Attribute Details
#backtrace_service ⇒ Object
Returns the value of attribute backtrace_service.
20
21
22
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 20
def backtrace_service
@backtrace_service
end
|
#handlers ⇒ Object
Returns the value of attribute handlers.
18
19
20
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 18
def handlers
@handlers
end
|
#thread_profiler_session ⇒ Object
Returns the value of attribute thread_profiler_session.
20
21
22
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 20
def thread_profiler_session
@thread_profiler_session
end
|
Instance Method Details
#call_handler_for(agent_command) ⇒ Object
125
126
127
128
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 125
def call_handler_for(agent_command)
handler = select_handler(agent_command)
handler.call(agent_command)
end
|
#check_for_and_handle_agent_commands ⇒ Object
39
40
41
42
43
44
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 39
def check_for_and_handle_agent_commands
commands = get_agent_commands
results = invoke_commands(commands)
new_relic_service.agent_command_results(results) unless results.empty?
end
|
#error(err) ⇒ Object
121
122
123
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 121
def error(err)
{ERROR_KEY => err.message}
end
|
#get_agent_commands ⇒ Object
85
86
87
88
89
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 85
def get_agent_commands
commands = new_relic_service.get_agent_commands
NewRelic::Agent.logger.debug("Received get_agent_commands = #{commands.inspect}")
commands.map { |collector_command| AgentCommand.new(collector_command) }
end
|
#harvest! ⇒ Object
52
53
54
55
56
57
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 52
def harvest!
profiles = []
profiles += harvest_from_thread_profiler_session
log_profiles(profiles)
profiles
end
|
#harvest_from_thread_profiler_session ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 67
def harvest_from_thread_profiler_session
if self.thread_profiler_session.ready_to_harvest?
self.thread_profiler_session.stop(true)
[self.thread_profiler_session.harvest]
else
[]
end
end
|
#invoke_command(agent_command) ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 104
def invoke_command(agent_command)
begin
call_handler_for(agent_command)
return success
rescue AgentCommandError => e
NewRelic::Agent.logger.debug(e)
error(e)
end
end
|
#invoke_commands(agent_commands) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 91
def invoke_commands(agent_commands)
results = {}
agent_commands.each do |agent_command|
results[agent_command.id.to_s] = invoke_command(agent_command)
end
results
end
|
#log_profiles(profiles) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 76
def log_profiles(profiles)
if profiles.empty?
::NewRelic::Agent.logger.debug('No thread profiles with data found to send.')
else
profile_descriptions = profiles.map { |p| p.to_log_description }
::NewRelic::Agent.logger.debug("Sending thread profiles [#{profile_descriptions.join(', ')}]")
end
end
|
#merge!(*args) ⇒ Object
We don't currently support merging thread profiles that failed to send
back into the AgentCommandRouter, so we just no-op this method.
Same with reset! - we don't support asynchronous cancellation of a
running thread profile currently.
63
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 63
def merge!(*args); end
|
#new_relic_service ⇒ Object
35
36
37
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 35
def new_relic_service
NewRelic::Agent.instance.service
end
|
#on_before_shutdown(*args) ⇒ Object
46
47
48
49
50
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 46
def on_before_shutdown(*args)
if self.thread_profiler_session.running?
self.thread_profiler_session.stop(true)
end
end
|
#reset! ⇒ Object
65
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 65
def reset!; end
|
#select_handler(agent_command) ⇒ Object
130
131
132
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 130
def select_handler(agent_command)
@handlers[agent_command.name]
end
|
#success ⇒ Object
117
118
119
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 117
def success
SUCCESS_RESULT
end
|
#unrecognized_agent_command(agent_command) ⇒ Object
134
135
136
|
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 134
def unrecognized_agent_command(agent_command)
NewRelic::Agent.logger.debug("Unrecognized agent command #{agent_command.inspect}")
end
|