Module: Fluent::Mixin::ConfigPlaceholders

Defined in:
lib/fluent/mixin/config_placeholders.rb

Constant Summary collapse

PLACEHOLDERS_DEFAULT =

and :percent

[ :dollar, :underscore ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



7
8
9
# File 'lib/fluent/mixin/config_placeholders.rb', line 7

def hostname
  @hostname
end

Instance Method Details

#configure(conf) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
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
100
101
102
103
104
105
106
107
108
# 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) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# 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) ⇒ Object



40
41
42
# 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) ⇒ Object



30
31
32
33
# 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_randomObject

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

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



25
26
27
28
# File 'lib/fluent/mixin/config_placeholders.rb', line 25

def uuid_random
  require 'uuidtools'
  UUIDTools::UUID.random_create.to_s
end

#uuid_timestampObject



35
36
37
38
# File 'lib/fluent/mixin/config_placeholders.rb', line 35

def uuid_timestamp
  require 'uuidtools'
  UUIDTools::UUID.timestamp_create.to_s
end