class InfluxDB::Process::Instrumentation

Constants

TYPES

Public Class Methods

new(influxdb, memory_series: nil, object_series: nil, interval: nil, process: nil) click to toggle source
# File lib/influxdb/process.rb, line 9
def initialize(influxdb, memory_series: nil, object_series: nil, interval: nil, process: nil)
  @influxdb = influxdb
  @memory_series = memory_series || 'process_memory'
  @object_series = object_series || 'process_objects'
  @interval = interval || 10
  @process = ENV['INFLUXDB_PROCESS_NAME'] || process || $PROGRAM_NAME

  @tags = {process: @process}

  @pid = ::Process.pid
  @system_memory_file = "/proc/#{@pid}/statm"
  @can_read_system_memory = File.exist?(@system_memory_file)
  @page_size = `getconf PAGESIZE`.to_i rescue 4096

  @memory = {}
  @objects = {}
end

Public Instance Methods

instrument() click to toggle source
# File lib/influxdb/process.rb, line 27
def instrument
  update_memory
  update_objects

  @influxdb.write_point(@memory_series, tags: @tags, values: @memory)
  @influxdb.write_point(@object_series, tags: @tags, values: @objects)
end
start() click to toggle source
# File lib/influxdb/process.rb, line 35
def start
  Thread.new do
    loop do
      instrument
      sleep(@interval)
    end
  end
end

Private Instance Methods

update_memory() click to toggle source
# File lib/influxdb/process.rb, line 71
def update_memory
  ObjectSpace.count_objects_size.each do |type, size|
    @memory[TYPES.fetch(type)] = size
  end

  if defined?(ActiveRecord::Base)
    @memory[:ar_models] = ObjectSpace.memsize_of_all(ActiveRecord::Base)
  end

  if @can_read_system_memory
    size, resident, share, text, _lib, data, _dt = File.read(@system_memory_file).split(' ').map do |pages|
      pages.to_i * @page_size
    end

    @memory[:total]    = size
    @memory[:resident] = resident
    @memory[:shared]   = share
    @memory[:program]  = text
    @memory[:data]     = data
  end
end
update_objects() click to toggle source
# File lib/influxdb/process.rb, line 93
def update_objects
  ObjectSpace.count_objects.each do |type, count|
    @objects[TYPES.fetch(type)] = count
  end

  gc = GC.stat

  @objects[:heap_available_slots] = gc[:heap_available_slots]
  @objects[:heap_live_slots]      = gc[:heap_live_slots]
  @objects[:heap_free_slots]      = gc[:heap_free_slots]
end