Class: NurseAndrea::MemorySampler
- Inherits:
-
Object
- Object
- NurseAndrea::MemorySampler
- Defined in:
- lib/nurse_andrea/memory_sampler.rb
Constant Summary collapse
- INTERVAL_SECONDS =
30
Class Method Summary collapse
-
.rss_bytes ⇒ Object
Returns RSS in bytes.
- .sample_and_enqueue ⇒ Object
- .start ⇒ Object
- .stop ⇒ Object
Class Method Details
.rss_bytes ⇒ Object
Returns RSS in bytes. Works on Linux (/proc) and macOS (ps).
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nurse_andrea/memory_sampler.rb', line 27 def self.rss_bytes if File.exist?("/proc/self/status") line = File.readlines("/proc/self/status").find { |l| l.start_with?("VmRSS:") } return nil unless line kb = line.split[1].to_i kb * 1024 else kb = `ps -o rss= -p #{Process.pid}`.strip.to_i return nil if kb == 0 kb * 1024 end rescue nil end |
.sample_and_enqueue ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/nurse_andrea/memory_sampler.rb', line 42 def self.sample_and_enqueue bytes = rss_bytes return unless bytes && bytes > 0 MetricsShipper.instance.enqueue( name: "process.memory.rss", value: bytes, unit: "bytes", tags: { service: NurseAndrea.config.service_name }, timestamp: Time.now.utc.iso8601(3) ) end |
.start ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/nurse_andrea/memory_sampler.rb', line 5 def self.start return @thread if @thread&.alive? @thread = Thread.new do loop do sleep INTERVAL_SECONDS sample_and_enqueue rescue => e NurseAndrea.debug("[NurseAndrea] MemorySampler error: #{e.}") end end @thread.abort_on_exception = false @thread.name = "NurseAndrea::MemorySampler" @thread end |
.stop ⇒ Object
21 22 23 24 |
# File 'lib/nurse_andrea/memory_sampler.rb', line 21 def self.stop @thread&.kill @thread = nil end |