Class: NewRelic::Agent::Commands::AgentCommandRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/commands/agent_command_router.rb

Defined Under Namespace

Classes: AgentCommandError

Constant Summary collapse

NO_PROFILES_TO_SEND =
{}.freeze
SUCCESS_RESULT =
{}.freeze
ERROR_KEY =
"error"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_listener = nil) ⇒ AgentCommandRouter

Returns a new instance of AgentCommandRouter.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 24

def initialize(event_listener=nil)
  @handlers    = Hash.new { |*| Proc.new { |cmd| self.unrecognized_agent_command(cmd) } }

  @backtrace_service = Threading::BacktraceService.new(event_listener)

  @thread_profiler_session = ThreadProfilerSession.new(@backtrace_service)
  @xray_session_collection = XraySessionCollection.new(@backtrace_service, event_listener)

  @handlers['start_profiler'] = Proc.new { |cmd| thread_profiler_session.handle_start_command(cmd) }
  @handlers['stop_profiler']  = Proc.new { |cmd| thread_profiler_session.handle_stop_command(cmd) }
  @handlers['active_xray_sessions'] = Proc.new { |cmd| xray_session_collection.handle_active_xray_sessions(cmd) }
end

Instance Attribute Details

#backtrace_serviceObject

Returns the value of attribute backtrace_service.



21
22
23
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 21

def backtrace_service
  @backtrace_service
end

#handlersObject (readonly)

Returns the value of attribute handlers.



19
20
21
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 19

def handlers
  @handlers
end

#thread_profiler_sessionObject

Returns the value of attribute thread_profiler_session.



21
22
23
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 21

def thread_profiler_session
  @thread_profiler_session
end

#xray_session_collectionObject

Returns the value of attribute xray_session_collection.



21
22
23
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 21

def xray_session_collection
  @xray_session_collection
end

Instance Method Details

#active_xray_command?(commands) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 54

def active_xray_command?(commands)
  commands.any? {|command| command.name == 'active_xray_sessions'}
end

#call_handler_for(agent_command) ⇒ Object



136
137
138
139
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 136

def call_handler_for(agent_command)
  handler = select_handler(agent_command)
  handler.call(agent_command)
end

#check_for_and_handle_agent_commandsObject



41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 41

def check_for_and_handle_agent_commands
  commands = get_agent_commands

  stop_xray_sessions unless active_xray_command?(commands)

  results = invoke_commands(commands)
  new_relic_service.agent_command_results(results) unless results.empty?
end

#error(err) ⇒ Object



132
133
134
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 132

def error(err)
  { ERROR_KEY => err.message }
end

#format_harvest_data(profiles) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 81

def format_harvest_data(profiles)
  if profiles.empty?
    NewRelic::Agent.logger.debug "No thread profiles with data found to send."
    NO_PROFILES_TO_SEND
  else
    log_profiles(profiles)
    {:profile_data => profiles}
  end
end

#get_agent_commandsObject



96
97
98
99
100
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 96

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_data_to_send(disconnecting) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 60

def harvest_data_to_send(disconnecting)
  profiles = []
  profiles += harvest_from_xray_session_collection
  profiles += harvest_from_thread_profiler_session(disconnecting)

  format_harvest_data(profiles)
end

#harvest_from_thread_profiler_session(disconnecting) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 72

def harvest_from_thread_profiler_session(disconnecting)
  if self.thread_profiler_session.ready_to_harvest?(disconnecting)
    self.thread_profiler_session.stop(true)
    [self.thread_profiler_session.harvest]
  else
    []
  end
end

#harvest_from_xray_session_collectionObject



68
69
70
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 68

def harvest_from_xray_session_collection
  self.xray_session_collection.harvest_thread_profiles
end

#invoke_command(agent_command) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 115

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



102
103
104
105
106
107
108
109
110
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 102

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



91
92
93
94
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 91

def log_profiles(profiles)
  profile_descriptions = profiles.map { |p| p.to_log_description }
  ::NewRelic::Agent.logger.debug "Sending thread profiles [#{profile_descriptions.join(", ")}]"
end

#new_relic_serviceObject



37
38
39
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 37

def new_relic_service
  NewRelic::Agent.instance.service
end

#select_handler(agent_command) ⇒ Object



141
142
143
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 141

def select_handler(agent_command)
  @handlers[agent_command.name]
end

#stop_xray_sessionsObject



50
51
52
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 50

def stop_xray_sessions
  self.xray_session_collection.stop_all_sessions
end

#successObject



128
129
130
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 128

def success
  SUCCESS_RESULT
end

#unrecognized_agent_command(agent_command) ⇒ Object



145
146
147
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 145

def unrecognized_agent_command(agent_command)
  NewRelic::Agent.logger.debug("Unrecognized agent command #{agent_command.inspect}")
end