Class: OneApm::Collector::Commands::ThreadProfilerSession

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/collector/commands/thread_profiler_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(backtrace_service) ⇒ ThreadProfilerSession

Returns a new instance of ThreadProfilerSession.



13
14
15
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 13

def initialize(backtrace_service)
  @backtrace_service = backtrace_service
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 57

def enabled?
  OneApm::Manager.config[:'thread_profiler.enabled']
end

#handle_start_command(agent_command) ⇒ Object



17
18
19
20
21
22
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 17

def handle_start_command(agent_command)
  raise_unsupported_error unless OneApm::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) ⇒ Object



24
25
26
27
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 24

def handle_stop_command(agent_command)
  report_data = agent_command.arguments.fetch("report_data", true)
  stop(report_data)
end

#harvestObject



48
49
50
51
52
53
54
55
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 48

def harvest
  OneApm::Manager.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?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 69

def past_time?
  @started_at && (Time.now > @started_at + @duration)
end

#ready_to_harvest?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 65

def ready_to_harvest?
  past_time? || stopped?
end

#running?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 61

def running?
  @backtrace_service.subscribed?(OneApm::Agent::Threading::BacktraceService::OA_ALL_TRANSACTIONS)
end

#start(agent_command) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 29

def start(agent_command)
  OneApm::Manager.logger.debug("Starting Thread Profiler.")
  profile = @backtrace_service.subscribe(
    OneApm::Agent::Threading::BacktraceService::OA_ALL_TRANSACTIONS,
    agent_command.arguments
  )

  @started_at = Time.now
  @duration = profile.duration if profile
end

#stop(report_data) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 40

def stop(report_data)
  return unless running?
  OneApm::Manager.logger.debug("Stopping Thread Profiler.")
  @finished_profile = @backtrace_service.harvest(OneApm::Agent::Threading::BacktraceService::OA_ALL_TRANSACTIONS)
  @backtrace_service.unsubscribe(OneApm::Agent::Threading::BacktraceService::OA_ALL_TRANSACTIONS)
  @finished_profile = nil if !report_data
end

#stopped?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/one_apm/collector/commands/thread_profiler_session.rb', line 73

def stopped?
  !!@finished_profile
end