module Fluent::Config

Constants

ARRAY_TYPE
BOOL_TYPE
ENUM_TYPE
FLOAT_TYPE
HASH_TYPE
INTEGER_TYPE
REFORMAT_VALUE
REGEXP_TYPE
SIZE_TYPE
STRING_TYPE
SYMBOL_TYPE
TIME_TYPE

Public Class Methods

array_value(val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 227
def self.array_value(val, opts = {}, name = nil)
  return nil if val.nil?

  param = if val.is_a?(String)
            val.start_with?('[') ? JSON.parse(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, opts, nil) }
  else
    param
  end
end
bool_value(str, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 61
def self.bool_value(str, opts = {}, name = nil)
  return nil if str.nil?

  case str.to_s
  when 'true', 'yes'
    true
  when 'false', 'no'
    false
  when ''
    true
  else
    # Current parser passes comment without actual values, e.g. "param #foo".
    # parser should pass empty string in this case but changing behaviour may break existing environment so keep parser behaviour. Just ignore comment value in boolean handling for now.
    if str.respond_to?('start_with?') && str.start_with?('#')
      true
    elsif opts[:strict]
      raise Fluent::ConfigError, "#{name}: invalid bool value: #{str}"
    else
      nil
    end
  end
end
build(config_path:, encoding: 'utf-8', additional_config: nil, use_v1_config: true) click to toggle source

@param config_path [String] config file path @param encoding [String] encoding of config file @param additional_config [String] config which is added to last of config body @param use_v1_config [Bool] config is formatted with v1 or not @return [Fluent::Config]

# File lib/fluent/config.rb, line 28
def self.build(config_path:, encoding: 'utf-8', additional_config: nil, use_v1_config: true)
  config_fname = File.basename(config_path)
  config_basedir = File.dirname(config_path)
  config_data = File.open(config_path, "r:#{encoding}:utf-8") do |f|
    s = f.read
    if additional_config
      c = additional_config.gsub("\\n", "\n")
      s += "\n#{c}"
    end
    s
  end
  Fluent::Config.parse(config_data, config_fname, config_basedir, use_v1_config)
end
enum_value(val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 121
def self.enum_value(val, opts = {}, name = nil)
  return nil if val.nil?

  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
end
hash_value(val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 200
def self.hash_value(val, opts = {}, name = nil)
  return nil if val.nil?

  param = if val.is_a?(String)
            val.start_with?('{') ? JSON.parse(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, opts, new_key) : value
    end
    newparam
  end
end
new(name = '') click to toggle source
# File lib/fluent/config.rb, line 72
def self.new(name = '')
  Element.new(name, '', {}, [])
end
parse(str, fname, basepath = Dir.pwd, v1_config = nil, syntax: :v1) click to toggle source
# File lib/fluent/config.rb, line 42
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'
    $log.warn("Ruby DSL configuration format is deprecated. Please use original configuration format. https://docs.fluentd.org/configuration/config-file") if $log
    Config::DSL::Parser.parse(str, File.join(basepath, fname))
  else
    raise "[BUG] unknown configuration parser specification:'#{parser}'"
  end
end
reformatted_value(type, val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 23
def self.reformatted_value(type, val, opts = {}, name = nil)
  REFORMAT_VALUE.call(type, val, opts, name)
end
regexp_value(str, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 84
def self.regexp_value(str, opts = {}, name = nil)
  return nil unless str

  return Regexp.compile(str) unless str.start_with?("/")
  right_slash_position = str.rindex("/")
  if right_slash_position < str.size - 3
    raise Fluent::ConfigError, "invalid regexp: missing right slash: #{str}"
  end
  options = str[(right_slash_position + 1)..-1]
  option = 0
  option |= Regexp::IGNORECASE if options.include?("i")
  option |= Regexp::MULTILINE if options.include?("m")
  Regexp.compile(str[1...right_slash_position], option)
end
size_value(str, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 27
def self.size_value(str, opts = {}, name = nil)
  return nil if str.nil?

  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
    INTEGER_TYPE.call(str, opts, name)
  end
end
string_value(val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 99
def self.string_value(val, opts = {}, name = nil)
  return nil if val.nil?

  v = val.to_s
  v = v.frozen? ? v.dup : v # config_param can't assume incoming string is mutable
  v.force_encoding(Encoding::UTF_8)
end
symbol_value(val, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 111
def self.symbol_value(val, opts = {}, name = nil)
  return nil if val.nil? || val.empty?

  val.delete_prefix(":").to_sym
end
time_value(str, opts = {}, name = nil) click to toggle source
# File lib/fluent/config/types.rb, line 44
def self.time_value(str, opts = {}, name = nil)
  return nil if str.nil?

  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
    FLOAT_TYPE.call(str, opts, name)
  end
end