Class: Fluent::DockerMetricsInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_docker_metrics.rb

Defined Under Namespace

Classes: BlkioSectorsParser, BlkioStatsParser, CGroupStatsParser, KeyValueStatsParser, TimerWatcher

Instance Method Summary collapse

Constructor Details

#initializeDockerMetricsInput

Returns a new instance of DockerMetricsInput.



10
11
12
13
14
15
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 10

def initialize
  super
  require 'socket'
  require 'docker'
  @hostname = Socket.gethostname
end

Instance Method Details

#configure(conf) ⇒ Object



17
18
19
20
# 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 = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_metricsObject

Metrics collection methods



36
37
38
39
40
41
42
43
44
45
46
# 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_idsObject



48
49
50
51
52
# 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

#runObject



28
29
30
31
32
33
# 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

#shutdownObject



88
89
90
91
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 88

def shutdown
  @loop.stop
  @thread.join
end

#startObject



22
23
24
25
26
27
# 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