From a98c51e6c4e1df3dee92ff6bed638b310035ea83 Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Wed, 25 Sep 2024 10:42:03 +0100 Subject: [PATCH] Combine memory statistics into a single metric with labels (#70) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Combine memory statistics into a single metric with labels Use base unit, bytes * 💇‍♀️ --- lib/promenade/pitchfork/mem_stats.rb | 14 +++++--------- spec/promenade/pitchfork/mem_stats_spec.rb | 9 +++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/promenade/pitchfork/mem_stats.rb b/lib/promenade/pitchfork/mem_stats.rb index 11bb977..73a9f88 100644 --- a/lib/promenade/pitchfork/mem_stats.rb +++ b/lib/promenade/pitchfork/mem_stats.rb @@ -7,24 +7,20 @@ module Promenade module Pitchfork class MemStats - Promenade.gauge :pitchfork_mem_rss do - doc "Resident Set Size of the pitchfork process, Total memory used by the process." - end - - Promenade.gauge :pitchfork_shared_mem do - doc "Shared memory of the pitchfork process, memory that is shared between multiple processes." + Promenade.gauge :pitchfork_memory_usage_bytes do + doc "Memory usage in bytes, broken down by type (RSS, PSS, SHARED_MEMORY)" end def initialize return unless defined?(::Pitchfork) && defined?(::Pitchfork::MemInfo) @mem_info = ::Pitchfork::MemInfo.new(Process.pid) - @parent_mem_info = ::Pitchfork::MemInfo.new(Process.ppid) end def instrument - Promenade.metric(:pitchfork_mem_rss).set({}, @mem_info.rss) - Promenade.metric(:pitchfork_shared_mem).set({}, @mem_info.shared_memory) + Promenade.metric(:pitchfork_memory_usage_bytes).set({ type: "RSS" }, @mem_info.rss * 1024) + Promenade.metric(:pitchfork_memory_usage_bytes).set({ type: "PSS" }, @mem_info.pss * 1024) + Promenade.metric(:pitchfork_memory_usage_bytes).set({ type: "Shared" }, @mem_info.shared_memory * 1024) end def self.instrument diff --git a/spec/promenade/pitchfork/mem_stats_spec.rb b/spec/promenade/pitchfork/mem_stats_spec.rb index 5e1df91..09c4a89 100644 --- a/spec/promenade/pitchfork/mem_stats_spec.rb +++ b/spec/promenade/pitchfork/mem_stats_spec.rb @@ -8,6 +8,7 @@ stub_const("Pitchfork::MemInfo", pitfork_mem_info) allow(pitfork_mem_info).to receive(:new).and_return(pitfork_mem_info) allow(pitfork_mem_info).to receive(:rss).and_return(100) + allow(pitfork_mem_info).to receive(:pss).and_return(50) allow(pitfork_mem_info).to receive(:shared_memory).and_return(50) end @@ -22,11 +23,11 @@ it "sets the metrics correctly" do stats = Promenade::Pitchfork::MemStats.new - expect(Promenade).to receive(:metric).with(:pitchfork_mem_rss).and_return(metric) - expect(Promenade).to receive(:metric).with(:pitchfork_shared_mem).and_return(metric) + expect(Promenade).to receive(:metric).with(:pitchfork_memory_usage_bytes).and_return(metric) - expect(metric).to receive(:set).with({}, 100) - expect(metric).to receive(:set).with({}, 50) + expect(metric).to receive(:set).with({ type: "RSS" }, 102400) + expect(metric).to receive(:set).with({ type: "PSS" }, 51200) + expect(metric).to receive(:set).with({ type: "Shared" }, 51200) stats.instrument end