Sha256: b9b9360c151677f8e169910e51e9481d0352b59de921ba7ab05386cf337962fb

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'open3'

class Fluent::KubernetesOutput < Fluent::Output
  Fluent::Plugin.register_output('kubernetes', self)

  config_param :container_id, :string
  config_param :tag, :string
  config_param :kubernetes_pod_regex, :string, default: '^[^_]+_([^\.]+)\.[^_]+_([^\.]+)\.([^\.]+)'

  def initialize
    super
  end

  def configure(conf)
    super

    require 'docker'
  end

  def emit(tag, es, chain)
    es.each do |time,record|
      Fluent::Engine.emit('kubernetes',
                          time,
                          enrich_record(tag, record))
    end

    chain.next
  end

  private

  def interpolate(tag, str)
    tag_parts = tag.split('.')

    str.gsub(/\$\{tag_parts\[(\d+)\]\}/) { |m| tag_parts[$1.to_i] }
  end

  def enrich_record(tag, record)
    if @container_id
      id = interpolate(tag, @container_id)
      record['container_id'] = id
      container = Docker::Container.get(id)
      if container
        container_name = container.json['Name']
        if container_name
          record["container_name"] = container_name[1..-1]
          regex = Regexp.new(@kubernetes_pod_regex)
          match = container_name.match(regex)
          if match
            pod_container_name, pod_name, pod_namespace =
              match.captures
            record["pod_namespace"] = pod_namespace
            record["pod"] = pod_name
            record["pod_container"] = pod_container_name
          end
        end
      end
    end
    record
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-kubernetes-0.2.3 lib/fluent/plugin/out_kubernetes.rb