Class: Fluent::Plugin::DockerMetadataFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_docker_metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDockerMetadataFilter

Returns a new instance of DockerMetadataFilter.



24
25
26
# File 'lib/fluent/plugin/filter_docker_metadata.rb', line 24

def initialize
  super
end

Class Method Details

.get_metadata(container_id) ⇒ Object



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

def self.(container_id)
  begin
    Docker::Container.get(container_id).info
  rescue Docker::Error::NotFoundError
    nil
  end
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/filter_docker_metadata.rb', line 28

def configure(conf)
  super

  require 'docker'
  require 'json'
  require 'lru_redux'

  Docker.url = @docker_url

  @cache = LruRedux::ThreadSafeCache.new(@cache_size)
  @container_id_regexp_compiled = Regexp.compile(@container_id_regexp)
end

#filter_stream(tag, es) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/filter_docker_metadata.rb', line 41

def filter_stream(tag, es)
  new_es = es
  container_id = tag.match(@container_id_regexp_compiled)
  if container_id && container_id[0]
    container_id = container_id[0]
     = @cache.getset(container_id){DockerMetadataFilter.(container_id)}

    if 
      new_es = Fluent::MultiEventStream.new

      es.each {|time, record|
        record.merge!({
          'container_id' => ['id'],
          'container_name' => ['Name'][1..-1],
          'container_hostname' => ['Config']['Hostname']
        })

        record.merge!({'container_image' => ['Config']['Image'] }) if @image_name
        record.merge!({'image_id' => ['Image']}) if @image_id

        @labels.split(@keys_delimiter).each do |pattern_name|
          lable, caption = pattern_name.split(@values_delimiter)
          raise ConfigError, "label caption is needed" if caption.nil?
          record.merge!({"#{caption}" => ['Config']['Labels'][lable].to_s}) unless ['Config']['Labels'][lable].nil?
        end
        new_es.add(time, record)
      }
    end
  end

  return new_es
end