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.



17
18
19
20
21
22
23
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 17

def initialize
  super
  require 'socket'
  require 'docker'
  @hostname = Socket.gethostname
  @with_systemd = File.exists?("#{@cgroup_path}/systemd")
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 25

def configure(conf)
  super

end

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



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
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 62

def emit_container_metric(id, name, metric_type, metric_filename, opts = {})

  if @with_systemd
    path = "#{@cgroup_path}/#{metric_type}/system.slice/docker-#{id}.scope/#{metric_filename}"
  else
    path = "#{@cgroup_path}/#{metric_type}/docker/#{id}/#{metric_filename}"
  end

  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.sub(/^\//, '')
      mes.add(time, data)
    end
    router.emit_stream(tag, mes)
  else
    nil
  end
end

#get_metricsObject

Metrics collection methods



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 44

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



56
57
58
59
60
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 56

def list_container_ids
  Docker::Container.all.map do |container|
    [container.id, container.info["Names"].first]
  end
end

#runObject



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

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

#shutdownObject



101
102
103
104
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 101

def shutdown
  @loop.stop
  @thread.join
end

#startObject



30
31
32
33
34
35
# File 'lib/fluent/plugin/in_docker_metrics.rb', line 30

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