class Fluent::DockerMetricsInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_docker_metrics.rb, line 10
def initialize
  super
  require 'socket'
  require 'docker'
  @hostname = Socket.gethostname
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_docker_metrics.rb, line 17
def configure(conf)
  super

end
emit_container_metric(id, name, metric_type, metric_filename, opts = {}) click to toggle source
# File lib/fluent/plugin/in_docker_metrics.rb, line 54
def emit_container_metric(id, name, metric_type, metric_filename, opts = {})
  path = "#{@cgroup_path}/#{metric_type}/docker/#{id}/#{metric_filename}"

  if File.exists?(path)
    # the order of these two if's matters
    if metric_filename == 'blkio.sectors'
      parser = BlkioSectorsParser.new(path, metric_filename.gsub('.', '_'))
    elsif metric_type == 'blkio'
      parser = BlkioStatsParser.new(path, metric_filename.gsub('.', '_'))
    else
      parser = KeyValueStatsParser.new(path, metric_filename.gsub('.', '_'))
    end
    time = Engine.now
    tag = "#{@tag_prefix}.#{metric_filename}"
    mes = MultiEventStream.new
    parser.parse_each_line do |data|
      next if not data
      # TODO: address this more elegantly
      if data['key'] =~ /^(?:cpuacct|blkio|memory_stat_pg)/
        data['type'] = 'counter'
      else
        data['type'] = 'gauge'
      end
      data["hostname"] = @hostname
      data["id"] = id
      data["name"] = name
      mes.add(time, data)
    end
    Engine.emit_stream(tag, mes)
  else
    nil
  end
end
get_metrics() click to toggle source

Metrics collection methods

# File lib/fluent/plugin/in_docker_metrics.rb, line 36
def get_metrics
  ids = @container_ids || list_container_ids
  ids.each do |id, name|
    emit_container_metric(id, name, 'memory', 'memory.stat') 
    emit_container_metric(id, name, 'cpuacct', 'cpuacct.stat') 
    emit_container_metric(id, name, 'blkio', 'blkio.io_serviced') 
    emit_container_metric(id, name, 'blkio', 'blkio.io_service_bytes') 
    emit_container_metric(id, name, 'blkio', 'blkio.io_queued') 
    emit_container_metric(id, name, 'blkio', 'blkio.sectors') 
  end
end
list_container_ids() click to toggle source
# File lib/fluent/plugin/in_docker_metrics.rb, line 48
def list_container_ids
  Docker::Container.all.map do |container|
    [container.id, container.info["Names"].first]
  end
end
run() click to toggle source
# File lib/fluent/plugin/in_docker_metrics.rb, line 28
def run
  @loop.run
rescue
  log.error "unexpected error", :error=>$!.to_s
  log.error_backtrace
end
shutdown() click to toggle source
# File lib/fluent/plugin/in_docker_metrics.rb, line 88
def shutdown
  @loop.stop
  @thread.join
end
start() click to toggle source
# File lib/fluent/plugin/in_docker_metrics.rb, line 22
def start
  @loop = Coolio::Loop.new
  tw = TimerWatcher.new(@stats_interval, true, @log, &method(:get_metrics))
  tw.attach(@loop)
  @thread = Thread.new(&method(:run))
end