class ElasticAPM::Metrics::CpuMemSet

@api private

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
Calls superclass method
# File lib/elastic_apm/metrics/cpu_mem_set.rb, line 62
def initialize(config)
  super

  @sampler = sampler_for_os(Metrics.os)
  read! # set initial values to calculate deltas from
end

Public Instance Methods

collect() click to toggle source
Calls superclass method
# File lib/elastic_apm/metrics/cpu_mem_set.rb, line 71
def collect
  read!
  super
end

Private Instance Methods

calculate_deltas(current, previous) click to toggle source
# File lib/elastic_apm/metrics/cpu_mem_set.rb, line 111
def calculate_deltas(current, previous)
  system_cpu_total =
    current.system_cpu_total - previous.system_cpu_total
  system_cpu_usage =
    current.system_cpu_usage - previous.system_cpu_usage
  process_cpu_usage =
    current.process_cpu_usage - previous.process_cpu_usage

  cpu_usage_pct = system_cpu_usage.to_f / system_cpu_total
  cpu_process_pct = process_cpu_usage.to_f / system_cpu_total

  [cpu_usage_pct, cpu_process_pct]
end
read!() click to toggle source
# File lib/elastic_apm/metrics/cpu_mem_set.rb, line 88
def read!
  return if disabled?

  current = @sampler.sample

  unless @previous
    @previous = current
    return
  end

  cpu_usage_pct, cpu_process_pct = calculate_deltas(current, @previous)

  gauge(:'system.cpu.total.norm.pct').value = cpu_usage_pct
  gauge(:'system.memory.actual.free').value = current.system_memory_free
  gauge(:'system.memory.total').value = current.system_memory_total
  gauge(:'system.process.cpu.total.norm.pct').value = cpu_process_pct
  gauge(:'system.process.memory.size').value = current.process_memory_size
  gauge(:'system.process.memory.rss.bytes').value =
    current.process_memory_rss * current.page_size

  @previous = current
end
sampler_for_os(os) click to toggle source
# File lib/elastic_apm/metrics/cpu_mem_set.rb, line 78
def sampler_for_os(os)
  case os
  when :linux then Linux.new
  else
    warn "Disabling system metrics, unsupported host OS '#{os}'"
    disable!
    nil
  end
end