# 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
# 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
# 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
# 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
# File lib/fluent/mixin/config_placeholders.rb, line 35 def uuid_timestamp require 'uuidtools' UUIDTools::UUID.timestamp_create.to_s end