class NewRelic::Agent::Commands::XraySessionCollection
Constants
- NO_PROFILES
Public Class Methods
new(backtrace_service, event_listener)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 14 def initialize(backtrace_service, event_listener) @backtrace_service = backtrace_service # This lock protects access to the sessions hash, but it's expected # that individual session objects within the hash will be manipulated # outside the lock. This is safe because manipulation of the session # objects is expected from only a single thread (the harvest thread) @sessions_lock = Mutex.new @sessions = {} if event_listener event_listener.subscribe(:before_harvest, &method(:cleanup_finished_sessions)) end end
Public Instance Methods
activate_sessions(incoming_ids)
click to toggle source
Session activation
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 94 def activate_sessions(incoming_ids) lookup_metadata_for(ids_to_activate(incoming_ids)).each do |raw| add_session(XraySession.new(raw)) end end
active_thread_profiling_sessions()
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 86 def active_thread_profiling_sessions @sessions_lock.synchronize do @sessions.values.select { |s| s.active? && s.run_profiler? } end end
add_session(session)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 113 def add_session(session) NewRelic::Agent.logger.debug("Adding X-Ray session #{session.inspect}") NewRelic::Agent.increment_metric("Supportability/XraySessions/Starts") @sessions_lock.synchronize { @sessions[session.id] = session } session.activate if session.run_profiler? @backtrace_service.subscribe(session.key_transaction_name, session.command_arguments) end end
cleanup_finished_sessions()
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 69 def cleanup_finished_sessions finished_session_ids.each do |id| NewRelic::Agent.logger.debug("Finished X-Ray session #{id} by duration. Removing it from active sessions.") remove_session_by_id(id) end end
deactivate_for_incoming_sessions(incoming_ids)
click to toggle source
Session deactivation
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 127 def deactivate_for_incoming_sessions(incoming_ids) ids_to_remove(incoming_ids).each do |session_id| remove_session_by_id(session_id) end end
finished_session_ids()
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 151 def finished_session_ids @sessions_lock.synchronize do @sessions.map{|k, s| k if s.finished?}.compact end end
handle_active_xray_sessions(agent_command)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 29 def handle_active_xray_sessions(agent_command) # If X-Rays are disabled, just be quiet about it and don't start the # command. Other hosts might be running the X-Ray, so we don't need # to bark on every get_agent_commands. if !NewRelic::Agent.config[:'xray_session.enabled'] NewRelic::Agent.logger.debug("Not responding to X-Ray command because of config 'xray_session.enabled' = #{NewRelic::Agent.config[:'xray_session.enabled']}") return end incoming_ids = agent_command.arguments["xray_ids"] deactivate_for_incoming_sessions(incoming_ids) activate_sessions(incoming_ids) end
harvest_thread_profiles()
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 54 def harvest_thread_profiles return NO_PROFILES unless NewRelic::Agent::Threading::BacktraceService.is_supported? profiles = active_thread_profiling_sessions.map do |session| NewRelic::Agent.logger.debug("Harvesting profile for X-Ray session #{session.inspect}") @backtrace_service.harvest(session.key_transaction_name) end profiles.reject! {|p| p.empty?} profiles.compact end
ids_to_activate(incoming_ids)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 100 def ids_to_activate(incoming_ids) @sessions_lock.synchronize { incoming_ids - @sessions.keys } end
ids_to_remove(incoming_ids)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 133 def ids_to_remove(incoming_ids) @sessions_lock.synchronize { @sessions.keys - incoming_ids } end
lookup_metadata_for(ids_to_activate)
click to toggle source
Please don't hold the @sessions_lock across me! Calling the service is time-consuming, and will block request threads. Which is rude.
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 106 def lookup_metadata_for(ids_to_activate) return [] if ids_to_activate.empty? NewRelic::Agent.logger.debug("Retrieving metadata for X-Ray sessions #{ids_to_activate.inspect}") new_relic_service.get_xray_metadata(ids_to_activate) end
new_relic_service()
click to toggle source
Internals
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 79 def new_relic_service NewRelic::Agent.instance.service end
remove_session_by_id(id)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 137 def remove_session_by_id(id) session = @sessions_lock.synchronize { @sessions.delete(id) } if session NewRelic::Agent.logger.debug("Removing X-Ray session #{session.inspect}") NewRelic::Agent.increment_metric("Supportability/XraySessions/Stops") if session.run_profiler? @backtrace_service.unsubscribe(session.key_transaction_name) end session.deactivate end end
session_id_for_transaction_name(name)
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 43 def session_id_for_transaction_name(name) @sessions_lock.synchronize do @sessions.each do |id, session| return id if session.key_transaction_name == name end end nil end
stop_all_sessions()
click to toggle source
# File lib/new_relic/agent/commands/xray_session_collection.rb, line 65 def stop_all_sessions deactivate_for_incoming_sessions([]) end