class ElasticAPM::Metrics::CpuMemSet

@api private

def calculate_deltas(current, previous)

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
  # No change / avoid dividing by 0
  return [0, 0] if system_cpu_total == 0
  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

def collect

def collect
  read!
  super
end

def initialize(config)

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

def read!

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

def sampler_for_os(os)

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