Skip to content

Commit

Permalink
Combine memory statistics into a single metric with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
robertomiranda committed Sep 20, 2024
1 parent 6442f72 commit 8b52bcc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
13 changes: 5 additions & 8 deletions lib/promenade/pitchfork/mem_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@
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."
Promenade.gauge :pitchfork_memory_usage_kilobytes do
doc "Memory usage in kilobytes, broken down by type (RSS, PSS, SHARED_MEMORY)"
end

Promenade.gauge :pitchfork_shared_mem do
doc "Shared memory of the pitchfork process, memory that is shared between multiple processes."
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_kilobytes).set({ type: "Rss" }, @mem_info.rss)
Promenade.metric(:pitchfork_memory_usage_kilobytes).set({ type: "Pss" }, @mem_info.pss)
Promenade.metric(:pitchfork_memory_usage_kilobytes).set({ type: "Shared" }, @mem_info.shared_memory)
end

def self.instrument
Expand Down
9 changes: 5 additions & 4 deletions spec/promenade/pitchfork/mem_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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_kilobytes).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"}, 100)
expect(metric).to receive(:set).with({:type=>"Pss"}, 50)
expect(metric).to receive(:set).with({:type=>"Shared"}, 50)

stats.instrument
end
Expand Down

0 comments on commit 8b52bcc

Please sign in to comment.