From a78ac4404a073186c6764000957f712b9fe049a4 Mon Sep 17 00:00:00 2001 From: fallwith Date: Thu, 16 Nov 2023 17:29:50 -0800 Subject: [PATCH] Simplify CRuby GC.stat info fetching Now that this gem requires Ruby 2.4+, we no longer have to worry about `GC` not responding to `:stat` or the `GC.stat` hash having different key names for the same stat between different Ruby versions. --- lib/new_relic/agent/vm/mri_vm.rb | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/new_relic/agent/vm/mri_vm.rb b/lib/new_relic/agent/vm/mri_vm.rb index 93dedba5f0..51e7c8eaab 100644 --- a/lib/new_relic/agent/vm/mri_vm.rb +++ b/lib/new_relic/agent/vm/mri_vm.rb @@ -24,7 +24,7 @@ def gather_stats(snap) def gather_gc_stats(snap) gather_gc_runs(snap) if supports?(:gc_runs) - gather_derived_stats(snap) if GC.respond_to?(:stat) + gather_derived_stats(snap) end def gather_gc_runs(snap) @@ -33,19 +33,11 @@ def gather_gc_runs(snap) def gather_derived_stats(snap) stat = GC.stat - snap.total_allocated_object = derive_from_gc_stats(%i[total_allocated_objects total_allocated_object], stat) - snap.major_gc_count = derive_from_gc_stats(:major_gc_count, stat) - snap.minor_gc_count = derive_from_gc_stats(:minor_gc_count, stat) - snap.heap_live = derive_from_gc_stats(%i[heap_live_slots heap_live_slot heap_live_num], stat) - snap.heap_free = derive_from_gc_stats(%i[heap_free_slots heap_free_slot heap_free_num], stat) - end - - def derive_from_gc_stats(keys, stat) - Array(keys).each do |key| - value = stat[key] - return value if value - end - nil + snap.total_allocated_object = stat.fetch(:total_allocated_objects, nil) + snap.major_gc_count = stat.fetch(:major_gc_count, nil) + snap.minor_gc_count = stat.fetch(:minor_gc_count, nil) + snap.heap_live = stat.fetch(:heap_live_slots, nil) + snap.heap_free = stat.fetch(:heap_free_slots, nil) end def gather_gc_time(snap)