class OneApm::Collector::Commands::XraySessionCollection

Constants

NO_PROFILES

Public Class Methods

new(backtrace_service, event_listener) click to toggle source
# File lib/one_apm/collector/commands/xray_session_collection.rb, line 13
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/one_apm/collector/commands/xray_session_collection.rb, line 90
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/one_apm/collector/commands/xray_session_collection.rb, line 82
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/one_apm/collector/commands/xray_session_collection.rb, line 109
def add_session(session)
  OneApm::Manager.logger.debug("Adding X-Ray session #{session.inspect}")
  OneApm::Manager.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/one_apm/collector/commands/xray_session_collection.rb, line 65
def cleanup_finished_sessions
  finished_session_ids.each do |id|
    OneApm::Manager.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/one_apm/collector/commands/xray_session_collection.rb, line 123
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/one_apm/collector/commands/xray_session_collection.rb, line 147
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/one_apm/collector/commands/xray_session_collection.rb, line 28
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 !OneApm::Manager.config[:'xray_session.enabled']
    OneApm::Manager.logger.debug("Not responding to X-Ray command because of config 'xray_session.enabled' = #{OneApm::Manager.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/one_apm/collector/commands/xray_session_collection.rb, line 50
def harvest_thread_profiles
  return NO_PROFILES unless OneApm::Agent::Threading::BacktraceService.is_supported?

  profiles = active_thread_profiling_sessions.map do |session|
    OneApm::Manager.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/one_apm/collector/commands/xray_session_collection.rb, line 96
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/one_apm/collector/commands/xray_session_collection.rb, line 129
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/one_apm/collector/commands/xray_session_collection.rb, line 102
def lookup_metadata_for(ids_to_activate)
  return [] if ids_to_activate.empty?

  OneApm::Manager.logger.debug("Retrieving metadata for X-Ray sessions #{ids_to_activate.inspect}")
  one_apm_service.get_xray_metadata(ids_to_activate)
end
one_apm_service() click to toggle source

Internals

# File lib/one_apm/collector/commands/xray_session_collection.rb, line 75
def one_apm_service
  OneApm::Manager.agent.service
end
remove_session_by_id(id) click to toggle source
# File lib/one_apm/collector/commands/xray_session_collection.rb, line 133
def remove_session_by_id(id)
  session = @sessions_lock.synchronize { @sessions.delete(id) }

  if session
    OneApm::Manager.logger.debug("Removing X-Ray session #{session.inspect}")
    OneApm::Manager.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/one_apm/collector/commands/xray_session_collection.rb, line 42
def session_id_for_transaction_name(name)
  @sessions_lock.synchronize do
    @sessions.keys.find { |id| @sessions[id].key_transaction_name == name }
  end
end
stop_all_sessions() click to toggle source
# File lib/one_apm/collector/commands/xray_session_collection.rb, line 61
def stop_all_sessions
  deactivate_for_incoming_sessions([])
end