Module: Fluent::Configurable

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CONFIG_TYPE_REGISTRY =
Registry.new(:config_type, 'fluent/plugin/type_')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



26
27
28
# File 'lib/fluent/configurable.rb', line 26

def self.included(mod)
  mod.extend(ClassMethods)
end

.lookup_type(type) ⇒ Object



117
118
119
# File 'lib/fluent/configurable.rb', line 117

def self.lookup_type(type)
  CONFIG_TYPE_REGISTRY.lookup(type)
end

.register_type(type, callable = nil, &block) ⇒ Object



112
113
114
115
# File 'lib/fluent/configurable.rb', line 112

def self.register_type(type, callable = nil, &block)
  callable ||= block
  CONFIG_TYPE_REGISTRY.register(type, callable)
end

Instance Method Details

#configObject



106
107
108
# File 'lib/fluent/configurable.rb', line 106

def config
  @masked_config ||= @config.to_masked_element
end

#configure(conf, strict_config_value = false) ⇒ Object



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
# File 'lib/fluent/configurable.rb', line 77

def configure(conf, strict_config_value=false)
  @config = conf

  logger = if self.respond_to?(:log)
             self.log
           elsif self.respond_to?(:owner) && self.owner.respond_to?(:log)
             self.owner.log
           elsif defined?($log)
             $log
           else
             nil
           end
  proxy = configure_proxy_generate
  conf.corresponding_proxies << proxy

  # In the nested section, can't get plugin class through proxies so get plugin class here
  plugin_class = Fluent::Plugin.lookup_type_from_class(proxy.name.to_s)
  root = Fluent::Config::SectionGenerator.generate(proxy, conf, logger, plugin_class, [], strict_config_value)
  @config_root_section = root

  root.instance_eval{ @params.keys }.each do |param_name|
    next if param_name.to_s.start_with?('@')
    varname = "@#{param_name}".to_sym
    instance_variable_set(varname, root[param_name])
  end

  self
end

#configure_proxy_generateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/configurable.rb', line 51

def configure_proxy_generate
  proxy = self.class.merged_configure_proxy

  if self.respond_to?(:owner) && self.owner
    owner_proxy = owner.class.merged_configure_proxy
    if proxy.configured_in_section
      owner_proxy = owner_proxy.sections[proxy.configured_in_section]
    end
    proxy.overwrite_defaults(owner_proxy) if owner_proxy
  end

  proxy
end

#configured_section_create(name, conf = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/configurable.rb', line 65

def configured_section_create(name, conf = nil)
  conf ||= Fluent::Config::Element.new(name.to_s, '', {}, [])
  root_proxy = configure_proxy_generate
  proxy = if name.nil? # root
            root_proxy
          else
            root_proxy.sections[name]
          end
  # take care to raise Fluent::ConfigError if conf mismatched to proxy
  Fluent::Config::SectionGenerator.generate(proxy, conf, nil, nil)
end

#initializeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fluent/configurable.rb', line 30

def initialize
  super
  # to simulate implicit 'attr_accessor' by config_param / config_section and its value by config_set_default
  proxy = self.class.merged_configure_proxy
  proxy.params.each_key do |name|
    next if name.to_s.start_with?('@')
    if proxy.defaults.has_key?(name)
      instance_variable_set("@#{name}".to_sym, proxy.defaults[name])
    end
  end
  proxy.sections.each_key do |name|
    next if name.to_s.start_with?('@')
    subproxy = proxy.sections[name]
    if subproxy.multi?
      instance_variable_set("@#{subproxy.variable_name}".to_sym, [])
    else
      instance_variable_set("@#{subproxy.variable_name}".to_sym, nil)
    end
  end
end