class Mmtrix::Agent::Commands::ThreadProfilerSession

Public Class Methods

new(backtrace_service) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 15
def initialize(backtrace_service)
  @backtrace_service = backtrace_service
end

Public Instance Methods

enabled?() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 59
def enabled?
  Mmtrix::Agent.config[:'thread_profiler.enabled']
end
handle_start_command(agent_command) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 19
def handle_start_command(agent_command)
  raise_unsupported_error unless Mmtrix::Agent::Threading::BacktraceService.is_supported?
  raise_thread_profiler_disabled unless enabled?
  raise_already_started_error if running?
  start(agent_command)
end
handle_stop_command(agent_command) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 26
def handle_stop_command(agent_command)
  report_data = agent_command.arguments.fetch("report_data", true)
  stop(report_data)
end
harvest() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 50
def harvest
  Mmtrix::Agent.logger.debug("Harvesting from Thread Profiler #{@finished_profile.to_log_description unless @finished_profile.nil?}")
  profile = @finished_profile
  @backtrace_service.profile_agent_code = false
  @finished_profile = nil
  @started_at = nil
  profile
end
past_time?() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 71
def past_time?
  @started_at && (Time.now > @started_at + @duration)
end
ready_to_harvest?() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 67
def ready_to_harvest?
  past_time? || stopped?
end
running?() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 63
def running?
  @backtrace_service.subscribed?(Mmtrix::Agent::Threading::BacktraceService::ALL_TRANSACTIONS)
end
start(agent_command) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 31
def start(agent_command)
  Mmtrix::Agent.logger.debug("Starting Thread Profiler.")
  profile = @backtrace_service.subscribe(
    Mmtrix::Agent::Threading::BacktraceService::ALL_TRANSACTIONS,
    agent_command.arguments
  )

  @started_at = Time.now
  @duration = profile.duration if profile
end
stop(report_data) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 42
def stop(report_data)
  return unless running?
  Mmtrix::Agent.logger.debug("Stopping Thread Profiler.")
  @finished_profile = @backtrace_service.harvest(Mmtrix::Agent::Threading::BacktraceService::ALL_TRANSACTIONS)
  @backtrace_service.unsubscribe(Mmtrix::Agent::Threading::BacktraceService::ALL_TRANSACTIONS)
  @finished_profile = nil if !report_data
end
stopped?() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 75
def stopped?
  !!@finished_profile
end

Private Instance Methods

raise_already_started_error() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 102
def raise_already_started_error
  msg = "Profile already in progress. Ignoring agent command to start another."
  raise_command_error(msg)
end
raise_command_error(msg) click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 81
def raise_command_error(msg)
  raise Mmtrix::Agent::Commands::AgentCommandRouter::AgentCommandError.new(msg)
end
raise_thread_profiler_disabled() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 97
def raise_thread_profiler_disabled
  msg = "Not starting Thread Profiler because of config 'thread_profiler.enabled' = #{enabled?}"
  raise_command_error(msg)
end
raise_unsupported_error() click to toggle source
# File lib/mmtrix/agent/commands/thread_profiler_session.rb, line 85
        def raise_unsupported_error
          msg = <<-EOF
Thread profiling is only supported on 1.9.2 and greater versions of Ruby.
We detected running agents capable of profiling, but the profile started with
an agent running Ruby #{RUBY_VERSION}.

Profiling again might select an appropriate agent, but we recommend running a
consistent version of Ruby across your application for better results.
          EOF
          raise_command_error(msg)
        end