Module: Fluent::Config

Defined in:
lib/fluent/config.rb,
lib/fluent/config/dsl.rb,
lib/fluent/config/types.rb,
lib/fluent/config/parser.rb,
lib/fluent/config/element.rb,
lib/fluent/config/section.rb,
lib/fluent/config/v1_parser.rb,
lib/fluent/config/basic_parser.rb,
lib/fluent/config/literal_parser.rb,
lib/fluent/config/configure_proxy.rb

Defined Under Namespace

Modules: DSL, SectionGenerator Classes: BasicParser, ConfigureProxy, Element, LiteralParser, Parser, Section, V1Parser

Constant Summary collapse

STRING_TYPE =
Proc.new { |val, opts| val }
ENUM_TYPE =
Proc.new { |val, opts|
  s = val.to_sym
  list = opts[:list]
  raise "Plugin BUG: config type 'enum' requires :list of symbols" unless list.is_a?(Array) && list.all?{|v| v.is_a? Symbol }
  unless list.include?(s)
    raise ConfigError, "valid options are #{list.join(',')} but got #{val}"
  end
  s
}
INTEGER_TYPE =
Proc.new { |val, opts| val.to_i }
FLOAT_TYPE =
Proc.new { |val, opts| val.to_f }
SIZE_TYPE =
Proc.new { |val, opts| Config.size_value(val) }
BOOL_TYPE =
Proc.new { |val, opts| Config.bool_value(val) }
TIME_TYPE =
Proc.new { |val, opts| Config.time_value(val) }
REFORMAT_VALUE =
->(type, value) {
  if value.nil?
    value
  else
    case type
    when :string  then value.to_s
    when :integer then value.to_i
    when :float   then value.to_f
    when :size then Config.size_value(value)
    when :bool then Config.bool_value(value)
    when :time then Config.time_value(value)
    else
      raise "unknown type in REFORMAT: #{type}"
    end
  end
}
HASH_TYPE =
Proc.new { |val, opts|
  param = if val.is_a?(String)
            val.start_with?('{') ? JSON.load(val) : Hash[val.strip.split(/\s*,\s*/).map{|v| v.split(':', 2)}]
          else
            val
          end
  if param.class != Hash
    raise ConfigError, "hash required but got #{val.inspect}"
  end
  if opts.empty?
    param
  else
    newparam = {}
    param.each_pair do |key, value|
      new_key = opts[:symbolize_keys] ? key.to_sym : key
      newparam[new_key] = opts[:value_type] ? REFORMAT_VALUE.call(opts[:value_type], value) : value
    end
    newparam
  end
}
ARRAY_TYPE =
Proc.new { |val, opts|
  param = if val.is_a?(String)
            val.start_with?('[') ? JSON.load(val) : val.strip.split(/\s*,\s*/)
          else
            val
          end
  if param.class != Array
    raise ConfigError, "array required but got #{val.inspect}"
  end
  if opts[:value_type]
    param.map{|v| REFORMAT_VALUE.call(opts[:value_type], v) }
  else
    param
  end
}

Class Method Summary collapse

Class Method Details

.bool_value(str) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/config/types.rb', line 53

def self.bool_value(str)
  return nil if str.nil?
  case str.to_s
  when 'true', 'yes'
    true
  when 'false', 'no'
    false
  when ''
    true
  else
    nil
  end
end

.new(name = '') ⇒ Object



52
53
54
# File 'lib/fluent/config.rb', line 52

def self.new(name = '')
  Element.new(name, '', {}, [])
end

.parse(str, fname, basepath = Dir.pwd, v1_config = nil, syntax: :v1) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/config.rb', line 23

def self.parse(str, fname, basepath = Dir.pwd, v1_config = nil, syntax: :v1)
  parser = if fname =~ /\.rb$/ || syntax == :ruby
             :ruby
           elsif v1_config.nil?
             case syntax
             when :v1 then :v1
             when :v0 then :v0
             else
               raise ArgumentError, "Unknown Fluentd configuration syntax: '#{syntax}'"
             end
           elsif v1_config then :v1
           else :v0
           end
  case parser
  when :v1
    require 'fluent/config/v1_parser'
    V1Parser.parse(str, fname, basepath, Kernel.binding)
  when :v0
    # TODO: show deprecated message in v1
    require 'fluent/config/parser'
    Parser.parse(str, fname, basepath)
  when :ruby
    require 'fluent/config/dsl'
    Config::DSL::Parser.parse(str, File.join(basepath, fname))
  else
    raise "[BUG] unknown configuration parser specification:'#{parser}'"
  end
end

.size_value(str) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluent/config/types.rb', line 23

def self.size_value(str)
  case str.to_s
  when /([0-9]+)k/i
    $~[1].to_i * 1024
  when /([0-9]+)m/i
    $~[1].to_i * (1024 ** 2)
  when /([0-9]+)g/i
    $~[1].to_i * (1024 ** 3)
  when /([0-9]+)t/i
    $~[1].to_i * (1024 ** 4)
  else
    str.to_i
  end
end

.time_value(str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/config/types.rb', line 38

def self.time_value(str)
  case str.to_s
  when /([0-9]+)s/
    $~[1].to_i
  when /([0-9]+)m/
    $~[1].to_i * 60
  when /([0-9]+)h/
    $~[1].to_i * 60 * 60
  when /([0-9]+)d/
    $~[1].to_i * 24 * 60 * 60
  else
    str.to_f
  end
end