Class: Datadog::Profiling::OldRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/profiling/old_recorder.rb

Overview

Stores profiling events gathered by the Stack collector

Defined Under Namespace

Classes: UnknownEventError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_classes, max_size, last_flush_time: Time.now.utc) ⇒ OldRecorder

Returns a new instance of OldRecorder.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/datadog/profiling/old_recorder.rb', line 10

def initialize(
  event_classes,
  max_size,
  last_flush_time: Time.now.utc
)
  @buffers = {}
  @last_flush_time = last_flush_time
  @max_size = max_size

  # Add a buffer for each class
  event_classes.each do |event_class|
    @buffers[event_class] = Profiling::Buffer.new(max_size)
  end

  # Event classes can only be added ahead of time
  @buffers.freeze
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



8
9
10
# File 'lib/datadog/profiling/old_recorder.rb', line 8

def max_size
  @max_size
end

Instance Method Details

#[](event_class) ⇒ Object



28
29
30
# File 'lib/datadog/profiling/old_recorder.rb', line 28

def [](event_class)
  @buffers[event_class]
end

#push(events) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/profiling/old_recorder.rb', line 32

def push(events)
  if events.is_a?(Array)
    # Push multiple events
    event_class = events.first.class
    raise UnknownEventError, event_class unless @buffers.key?(event_class)

    @buffers[event_class].concat(events)
  else
    # Push single event
    event_class = events.class
    raise UnknownEventError, event_class unless @buffers.key?(event_class)

    @buffers[event_class].push(events)
  end
end

#reset_after_forkObject



74
75
76
77
78
79
80
# File 'lib/datadog/profiling/old_recorder.rb', line 74

def reset_after_fork
  Datadog.logger.debug('Resetting OldRecorder in child process after fork')

  # NOTE: A bit of a heavy-handed approach, but it doesn't happen often and this class will be removed soon anyway
  serialize
  nil
end

#serializeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/profiling/old_recorder.rb', line 48

def serialize
  event_count = 0

  event_groups, start, finish = update_time do
    @buffers.collect do |event_class, buffer|
      events = buffer.pop
      next if events.empty?

      event_count += events.length
      EventGroup.new(event_class, events)
    end.compact
  end

  return if event_count.zero? # We don't want to report empty profiles

  encoded_pprof =
    Datadog::Profiling::Encoding::Profile::Protobuf.encode(
      event_count: event_count,
      event_groups: event_groups,
      start: start,
      finish: finish,
    )

  [start, finish, encoded_pprof]
end