module Fluent::Mixin::ConfigPlaceholders

Constants

PLACEHOLDERS_DEFAULT

Attributes

hostname[RW]

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/mixin/config_placeholders.rb, line 50
def configure(conf)
  # Element#has_key? inserts key name into 'used' list, so we should not use that method...
  hostname = if conf.keys.include?('hostname') && has_replace_pattern?( conf.fetch('hostname') )
               Socket.gethostname
             elsif conf.keys.include?('hostname')
               conf['hostname']
             else
               Socket.gethostname
             end

  placeholders = self.respond_to?(:placeholders) ? self.placeholders : PLACEHOLDERS_DEFAULT

  mapping = {}

  placeholders.each do |p|
    case p
    when :dollar
      mapping.update({
          '${hostname}'       => lambda{ hostname },
          '${uuid}'           => lambda{ uuid_random() },
          '${uuid:random}'    => lambda{ uuid_random() },
          '${uuid:hostname}'  => lambda{ uuid_hostname(hostname) },
          '${uuid:timestamp}' => lambda{ uuid_timestamp() },
        })
    when :percent
      mapping.update({
          '%{hostname}'       => lambda{ hostname },
          '%{uuid}'           => lambda{ uuid_random() },
          '%{uuid:random}'    => lambda{ uuid_random() },
          '%{uuid:hostname}'  => lambda{ uuid_hostname(hostname) },
          '%{uuid:timestamp}' => lambda{ uuid_timestamp() },
        })
    when :underscore
      mapping.update({
          '__HOSTNAME__'       => lambda{ hostname },
          '__UUID__'           => lambda{ uuid_random() },
          '__UUID_RANDOM__'    => lambda{ uuid_random() },
          '__UUID_HOSTNAME__'  => lambda{ uuid_hostname(hostname) },
          '__UUID_TIMESTAMP__' => lambda{ uuid_timestamp() },
        })
    else
      raise ArgumentError, "unknown placeholder format: #{p}"
    end
  end

  check_element = ->(map, c) {
    c.arg = replace(map, c.arg)
    c.keys.each do |k|
      v = c.fetch(k, nil)
      if v and v.is_a? String
        c[k] = replace(map, v)
      end
    end
    c.elements.each{|e| check_element.call(map,e)}
  }
  check_element.call(mapping,conf)

  super
end
has_replace_pattern?(value) click to toggle source
# File lib/fluent/mixin/config_placeholders.rb, line 44
def has_replace_pattern?(value)
  value =~ /\$\{(?:hostname|uuid:[a-z]+)\}/ ||
    value =~ /\%\{(?:hostname|uuid:[a-z]+)\}/ ||
    value =~ /__(?:HOSTNAME|UUID_[A-Z]+)__/
end
replace(map, value) click to toggle source
# File lib/fluent/mixin/config_placeholders.rb, line 40
def replace(map, value)
  map.reduce(value){|r,p| r.gsub(p[0], p[1].call())}
end
uuid_hostname(hostname) click to toggle source
# File lib/fluent/mixin/config_placeholders.rb, line 30
def uuid_hostname(hostname)
  require 'uuidtools'
  UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, hostname).to_s
end
uuid_random() click to toggle source

${uuid:timestamp} , %{uuid:timestamp} , UUID_TIMESTAMP UUIDTools::UUID.timestamp_create

> #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>

# File lib/fluent/mixin/config_placeholders.rb, line 25
def uuid_random
  require 'uuidtools'
  UUIDTools::UUID.random_create.to_s
end
uuid_timestamp() click to toggle source
# File lib/fluent/mixin/config_placeholders.rb, line 35
def uuid_timestamp
  require 'uuidtools'
  UUIDTools::UUID.timestamp_create.to_s
end