Class: OneApm::Agent::Threading::ThreadProfile

Inherits:
Object
  • Object
show all
Includes:
Coerce
Defined in:
lib/one_apm/agent/threading/thread_profile.rb

Constant Summary collapse

OA_THREAD_PROFILER_NODES =
20_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Coerce

#event_params, #float, #int, #int_or_nil, #log_failure, #string

Constructor Details

#initialize(command_arguments = {}) ⇒ ThreadProfile

Returns a new instance of ThreadProfile.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 25

def initialize(command_arguments={})
  @command_arguments  = command_arguments
  @profile_id         = command_arguments.fetch('profile_id', -1)
  @duration           = command_arguments.fetch('duration', 120)
  @sample_period      = command_arguments.fetch('sample_period', 0.1)
  @profile_agent_code = command_arguments.fetch('profile_agent_code', false)
  @xray_id            = command_arguments.fetch('x_ray_id', nil)
  @finished = false

  @traces = {
    :agent      => BacktraceRoot.new,
    :background => BacktraceRoot.new,
    :other      => BacktraceRoot.new,
    :request    => BacktraceRoot.new
  }

  @poll_count = 0
  @backtrace_count = 0
  @failure_count = 0
  @unique_threads = []

  @created_at = Time.now
end

Instance Attribute Details

#backtrace_countObject (readonly)

Returns the value of attribute backtrace_count.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def backtrace_count
  @backtrace_count
end

#command_argumentsObject (readonly)

Returns the value of attribute command_arguments.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def command_arguments
  @command_arguments
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def created_at
  @created_at
end

#durationObject (readonly)

Returns the value of attribute duration.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def duration
  @duration
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def failure_count
  @failure_count
end

#finished_atObject

Returns the value of attribute finished_at.



23
24
25
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 23

def finished_at
  @finished_at
end

#poll_countObject (readonly)

Returns the value of attribute poll_count.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def poll_count
  @poll_count
end

#profile_agent_codeObject (readonly)

Returns the value of attribute profile_agent_code.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def profile_agent_code
  @profile_agent_code
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def profile_id
  @profile_id
end

#sample_periodObject (readonly)

Returns the value of attribute sample_period.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def sample_period
  @sample_period
end

#tracesObject (readonly)

Returns the value of attribute traces.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def traces
  @traces
end

#xray_idObject (readonly)

Returns the value of attribute xray_id.



12
13
14
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 12

def xray_id
  @xray_id
end

Instance Method Details

#aggregate(backtrace, bucket, thread) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 74

def aggregate(backtrace, bucket, thread)
  if backtrace.nil?
    @failure_count += 1
  else
    @backtrace_count += 1
    @traces[bucket].aggregate(backtrace)
    @unique_threads << thread unless @unique_threads.include?(thread)
  end
end

#convert_N_trace_nodes_to_arrays(count_to_keep) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 84

def convert_N_trace_nodes_to_arrays(count_to_keep)
  all_nodes = @traces.values.map { |n| n.flattened }.flatten

  OneApm::Manager.agent.stats_engine.
    tl_record_supportability_metric_count("ThreadProfiler/NodeCount", all_nodes.size)

  all_nodes.sort! do |a, b|
    # we primarily prefer higher runnable_count
    comparison = b.runnable_count <=> a.runnable_count
    # we secondarily prefer lower depth
    comparison = a.depth          <=> b.depth if comparison == 0
    # it is thus impossible for any child to preceed their parent
    comparison
  end

  all_nodes.each_with_index do |n, i|
    break if i >= count_to_keep
    n.mark_for_array_conversion
  end
  all_nodes.each_with_index do |n, i|
    break if i >= count_to_keep
    n.complete_array_conversion
  end
end

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 65

def empty?
  @backtrace_count == 0
end

#generate_tracesObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 113

def generate_traces
  convert_N_trace_nodes_to_arrays(OA_THREAD_PROFILER_NODES)

  {
    "OTHER"      => @traces[:other     ].as_array,
    "REQUEST"    => @traces[:request   ].as_array,
    "AGENT"      => @traces[:agent     ].as_array,
    "BACKGROUND" => @traces[:background].as_array
  }
end

#increment_poll_countObject



53
54
55
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 53

def increment_poll_count
  @poll_count += 1
end

#requested_periodObject



49
50
51
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 49

def requested_period
  @sample_period
end

#sample_countObject



57
58
59
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 57

def sample_count
  xray? ? @backtrace_count : @poll_count
end

#to_collector_array(encoder) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 124

def to_collector_array(encoder)
  encoded_trace_tree = encoder.encode(generate_traces, :skip_normalization => true)
  result = [
    int(self.profile_id),
    float(self.created_at),
    float(self.finished_at),
    int(self.sample_count),
    encoded_trace_tree,
    int(self.unique_thread_count),
    0 # runnable thread count, which we don't track
  ]
  result << int(@xray_id) if xray?
  result
end

#to_log_descriptionObject



139
140
141
142
143
144
145
146
147
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 139

def to_log_description
  id = if xray?
         "@xray_id: #{xray_id}"
       else
         "@profile_id: #{profile_id}"
       end

  "#<ThreadProfile:#{object_id} #{id} @command_arguments=#{@command_arguments.inspect}>"
end

#unique_thread_countObject



69
70
71
72
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 69

def unique_thread_count
  return 0 if @unique_threads.nil?
  @unique_threads.length
end

#xray?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/one_apm/agent/threading/thread_profile.rb', line 61

def xray?
  !!@xray_id
end