Class: HeapPeriscopeAgent::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/heap_periscope_agent/reporter.rb

Class Method Summary collapse

Class Method Details

.report_once!Object



65
66
67
68
69
70
# File 'lib/heap_periscope_agent/reporter.rb', line 65

def self.report_once!
  @config = HeapPeriscopeAgent.configuration
  @socket = UDPSocket.new
  send_snapshot_report
  @socket.close
end

.startObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/heap_periscope_agent/reporter.rb', line 10

def self.start
  @config = HeapPeriscopeAgent.configuration
  @running = Concurrent::AtomicBoolean.new(false)

  return if @running.true?
  @running.make_true

  log("Reporter starting with interval: #{@config.interval}s.")

  # Enable the standard GC Profiler
  GC::Profiler.enable
  # Store the initial total time to calculate deltas later
  @last_gc_total_time = GC::Profiler.total_time

  @thread = Thread.new do
    @socket = UDPSocket.new
    last_snapshot_time = Time.now

    while @running.true?
      # Periodically send a full snapshot
      if Time.now - last_snapshot_time >= @config.interval
        send_snapshot_report
        last_snapshot_time = Time.now
      end

      # Check for new GC activity since the last check
      send_gc_profiler_report

      sleep(1) # Main loop delay
    end

    log("Reporter loop finished.")
    GC::Profiler.disable # Clean up the profiler
    @socket.close
  end

  @thread.report_on_exception = true
  log("Reporter thread initiated.")
end

.stopObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/heap_periscope_agent/reporter.rb', line 50

def self.stop
  return unless @running&.true?

  log("Stopping reporter...")
  @running.make_false
  if @thread&.join(5)
    log("Reporter thread stopped gracefully.")
  else
    log("Reporter thread did not stop in time, killing.", level: :warn)
    @thread&.kill
  end
  @thread = nil
  log("Reporter stop process complete.")
end