Class: Instana::Collector::GC

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/collectors/gc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGC

Returns a new instance of GC.



6
7
8
9
10
11
12
13
# File 'lib/instana/collectors/gc.rb', line 6

def initialize
  @payload_key = :gc
  @last_report = {}
  @this_gc = {}
  @last_major_count = 0
  @last_minor_count = 0
  ::GC::Profiler.enable
end

Instance Attribute Details

#payload_keyObject

Returns the value of attribute payload_key.



4
5
6
# File 'lib/instana/collectors/gc.rb', line 4

def payload_key
  @payload_key
end

Instance Method Details

#collectObject

collect

To collect garbage collector related metrics.



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
49
50
51
52
53
54
55
# File 'lib/instana/collectors/gc.rb', line 20

def collect
  @this_gc.clear
  stats = ::GC.stat

  # Time spent in GC.  Report in milliseconds
  @this_gc[:totalTime] = ::GC::Profiler.total_time * 1000
  ::GC::Profiler.clear

  # GC metrics only available on newer Ruby versions
  if RUBY_VERSION >= '2.1'
    # GC runs.  Calculate how many have occurred since the last call
    @this_gc[:minorGcs]  = stats[:minor_gc_count] - @last_minor_count
    @this_gc[:majorGcs]  = stats[:major_gc_count] - @last_major_count

    # Store these counts so that we have something to compare to next
    # time around.
    @last_major_count = stats[:major_gc_count]
    @last_minor_count = stats[:minor_gc_count]
  end

  # GC Heap
  @this_gc[:heap_live] = stats[:heap_live_slot] || stats[:heap_live_slots] || stats[:heap_live_num]
  @this_gc[:heap_free] = stats[:heap_free_slot] || stats[:heap_free_slots] || stats[:heap_free_num]

  @this_gc = ::Instana::Util.enforce_deltas(@this_gc, @last_report)

  unless @this_gc.empty?
    @last_report.merge!(@this_gc)
    @this_gc
  else
    nil
  end
rescue => e
  ::Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
  ::Instana.logger.debug e.backtrace.join("\r\n")
end