Class: Fluent::DockerMetricsInput

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

Defined Under Namespace

Classes: BlkioStatsParser, CGroupStatsParser, KeyValueStatsParser, TimerWatcher

Instance Method Summary collapse

Constructor Details

#initializeDockerMetricsInput

Returns a new instance of DockerMetricsInput.



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

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

Instance Method Details

#configure(conf) ⇒ Object



15
16
17
18
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 15

def configure(conf)
  super

end

#emit_container_metric(id, metric_type, metric_filename, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 49

def emit_container_metric(id, metric_type, metric_filename, opts = {})
  path = "#{@cgroup_path}/#{metric_type}/docker/#{id}/#{metric_filename}"
  if File.exists?(path)
    parser = if metric_type != 'blkio'
               KeyValueStatsParser.new(path, metric_filename.gsub('.', '_'))
             else 
               BlkioStatsParser.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'
      end
      data["source"] = "#{@tag_prefix}:#{@hostname}:#{id}"
      mes.add(time, data)
    end
    Engine.emit_stream(tag, mes)
  else
    nil
  end
end

#get_metricsObject

Metrics collection methods



34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 34

def get_metrics
  list_container_ids.each do |id|
    emit_container_metric(id, 'memory', 'memory.stat') 
    emit_container_metric(id, 'cpuacct', 'cpuacct.stat') 
    emit_container_metric(id, 'blkio', 'blkio.io_serviced') 
    emit_container_metric(id, 'blkio', 'blkio.io_service_bytes') 
    emit_container_metric(id, 'blkio', 'blkio.io_service_queued') 
    emit_container_metric(id, 'blkio', 'blkio.sectors') 
  end
end

#list_container_idsObject



45
46
47
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 45

def list_container_ids
  `docker ps --no-trunc -q`.split /\s+/
end

#runObject



26
27
28
29
30
31
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 26

def run
  @loop.run
rescue
  log.error "unexpected error", :error=>$!.to_s
  log.error_backtrace
end

#shutdownObject



75
76
77
78
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 75

def shutdown
  @loop.stop
  @thread.join
end

#startObject



20
21
22
23
24
25
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 20

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