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

#check_element(map, c) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/fluent/mixin/config_placeholders.rb', line 92

def 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(map,e)}
end

#configure(conf) ⇒ Object



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
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
# File 'lib/fluent/mixin/config_placeholders.rb', line 47

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') )
               `hostname`.chomp
             elsif conf.keys.include?('hostname')
               conf['hostname']
             else
               `hostname`.chomp
             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

  def 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(map,e)}
  end

  check_element(mapping,conf)

  super
end

#has_replace_pattern?(value) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/fluent/mixin/config_placeholders.rb', line 41

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



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

def replace(map, value)
  map.reduce(value){|r,p| r.gsub(p[0], p[1].call())}
end

#uuid_hostname(hostname) ⇒ Object



29
30
31
# File 'lib/fluent/mixin/config_placeholders.rb', line 29

def uuid_hostname(hostname)
  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
# File 'lib/fluent/mixin/config_placeholders.rb', line 25

def uuid_random
  UUIDTools::UUID.random_create.to_s
end

#uuid_timestampObject



33
34
35
# File 'lib/fluent/mixin/config_placeholders.rb', line 33

def uuid_timestamp
  UUIDTools::UUID.timestamp_create.to_s
end