Skip to content

Commit 6442f72

Browse files
Add Pitchfork Memory Stats (#68)
* Add Memory Stats * Conditionally use rack middlewares * 💇‍♀️ * Remove after: :load_config_initializers block * Revert "Remove after: :load_config_initializers block" This reverts commit 58c9d9c. * Disable metrics * Use single block of initialization
1 parent a21dde1 commit 6442f72

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

lib/promenade/pitchfork/mem_stats.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
begin
2+
require "pitchfork/mem_info"
3+
rescue LoadError
4+
# No pitchfork available, dont do anything
5+
end
6+
7+
module Promenade
8+
module Pitchfork
9+
class MemStats
10+
Promenade.gauge :pitchfork_mem_rss do
11+
doc "Resident Set Size of the pitchfork process, Total memory used by the process."
12+
end
13+
14+
Promenade.gauge :pitchfork_shared_mem do
15+
doc "Shared memory of the pitchfork process, memory that is shared between multiple processes."
16+
end
17+
18+
def initialize
19+
return unless defined?(::Pitchfork) && defined?(::Pitchfork::MemInfo)
20+
21+
@mem_info = ::Pitchfork::MemInfo.new(Process.pid)
22+
@parent_mem_info = ::Pitchfork::MemInfo.new(Process.ppid)
23+
end
24+
25+
def instrument
26+
Promenade.metric(:pitchfork_mem_rss).set({}, @mem_info.rss)
27+
Promenade.metric(:pitchfork_shared_mem).set({}, @mem_info.shared_memory)
28+
end
29+
30+
def self.instrument
31+
new.instrument
32+
rescue StandardError
33+
end
34+
end
35+
end
36+
end

lib/promenade/pitchfork/middleware.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "promenade/pitchfork/stats"
2+
require "promenade/pitchfork/mem_stats"
23

34
module Promenade
45
module Pitchfork
@@ -11,12 +12,17 @@ def initialize(app)
1112

1213
def call(env)
1314
if env.key?(RACK_AFTER_REPLY)
14-
env[RACK_AFTER_REPLY] << -> {
15-
::Promenade::Pitchfork::Stats.instrument
16-
}
15+
env[RACK_AFTER_REPLY] << -> { instrument }
1716
end
1817
@app.call(env)
1918
end
19+
20+
private
21+
22+
def instrument
23+
Promenade::Pitchfork::Stats.instrument
24+
Promenade::Pitchfork::MemStats.instrument
25+
end
2026
end
2127
end
2228
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "spec_helper"
2+
require "promenade/pitchfork/mem_stats"
3+
4+
RSpec.describe Promenade::Pitchfork::MemStats do
5+
let(:pitfork_mem_info) { class_double("Pitchfork::MemInfo") }
6+
7+
before do
8+
stub_const("Pitchfork::MemInfo", pitfork_mem_info)
9+
allow(pitfork_mem_info).to receive(:new).and_return(pitfork_mem_info)
10+
allow(pitfork_mem_info).to receive(:rss).and_return(100)
11+
allow(pitfork_mem_info).to receive(:shared_memory).and_return(50)
12+
end
13+
14+
describe "#instrument" do
15+
let(:metric) { instance_double("Promenade::Metric") }
16+
17+
before do
18+
allow(Promenade).to receive(:metric).and_return(metric)
19+
allow(metric).to receive(:set)
20+
end
21+
22+
it "sets the metrics correctly" do
23+
stats = Promenade::Pitchfork::MemStats.new
24+
25+
expect(Promenade).to receive(:metric).with(:pitchfork_mem_rss).and_return(metric)
26+
expect(Promenade).to receive(:metric).with(:pitchfork_shared_mem).and_return(metric)
27+
28+
expect(metric).to receive(:set).with({}, 100)
29+
expect(metric).to receive(:set).with({}, 50)
30+
31+
stats.instrument
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)