Class: Plugin::RecordModifierFilter::DynamicExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/filter_record_modifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(param_key, param_value, prepare_value) ⇒ DynamicExpander

Returns a new instance of DynamicExpander.


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fluent/plugin/filter_record_modifier.rb', line 179

def initialize(param_key, param_value, prepare_value)
  if param_value.include?('${')
    __str_eval_code__ = parse_parameter(param_value)

    # Use class_eval with string instead of define_method for performance.
    # It can't share instructions but this is 2x+ faster than define_method in filter case.
    # Refer: http://tenderlovemaking.com/2013/03/03/dynamic_method_definitions.html
    (class << self; self; end).class_eval <<-EORUBY,  __FILE__, __LINE__ + 1
      def expand(tag, time, record, tag_parts)
        #{__str_eval_code__}
      end
    EORUBY
  else
    @param_value = param_value
  end

  begin
    eval prepare_value if prepare_value
  rescue SyntaxError
    raise ConfigError, "Pass invalid syntax parameter : key = prepare_value, value = #{prepare_value}"
  end

  begin
    # check eval genarates wrong code or not
    expand(nil, nil, nil, nil)
  rescue SyntaxError
    raise ConfigError, "Pass invalid syntax parameter : key = #{param_key}, value = #{param_value}"
  rescue
    # Ignore other runtime errors
  end
end

Instance Method Details

#expand(tag, time, record, tag_parts) ⇒ Object

Default implementation for fixed value. This is overwritten when parameter contains '$xxx' placeholder


212
213
214
# File 'lib/fluent/plugin/filter_record_modifier.rb', line 212

def expand(tag, time, record, tag_parts)
  @param_value
end