Class: LogStash::Config::AST::PluginSection

Inherits:
Node
  • Object
show all
Defined in:
lib/logstash/config/config_ast.rb

Instance Method Summary collapse

Methods inherited from Node

#text_value_for_comments

Constructor Details

#initialize(*args) ⇒ PluginSection

Global plugin numbering for the janky instance variable naming we use like @filter_<name>_1



170
171
172
# File 'lib/logstash/config/config_ast.rb', line 170

def initialize(*args)
  super(*args)
end

Instance Method Details

#compile_initializerObject

Generate ruby code to initialize all the plugins.



175
176
177
178
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
210
211
212
213
# File 'lib/logstash/config/config_ast.rb', line 175

def compile_initializer
  generate_variables
  code = []
  @variables.each do |plugin, name|


    code << <<-CODE
      @generated_objects[:#{name}] = #{plugin.compile_initializer}
      @#{plugin.plugin_type}s << @generated_objects[:#{name}]
    CODE

    # The flush method for this filter.
    if plugin.plugin_type == "filter"

      code << <<-CODE
        @generated_objects[:#{name}_flush] = lambda do |options, &block|
          @logger.debug? && @logger.debug(\"Flushing\", :plugin => @generated_objects[:#{name}])

          events = @generated_objects[:#{name}].flush(options)

          return if events.nil? || events.empty?

          @logger.debug? && @logger.debug(\"Flushing\", :plugin => @generated_objects[:#{name}], :events => events.map { |x| x.to_hash  })

          #{plugin.compile_starting_here.gsub(/^/, "  ")}

          events.each{|e| block.call(e)}
        end

        if @generated_objects[:#{name}].respond_to?(:flush)
          @periodic_flushers << @generated_objects[:#{name}_flush] if @generated_objects[:#{name}].periodic_flush
          @shutdown_flushers << @generated_objects[:#{name}_flush]
        end
      CODE

    end
  end
  return code.join("\n")
end

#generate_variablesObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/logstash/config/config_ast.rb', line 220

def generate_variables
  return if !@variables.nil?
  @variables = {}
  plugins = recursive_select(Plugin)

  plugins.each do |plugin|
    # Unique number for every plugin.
    LogStash::Config::AST.plugin_instance_index += 1
    # store things as ivars, like @filter_grok_3
    var = :"#{plugin.plugin_type}_#{plugin.plugin_name}_#{LogStash::Config::AST.plugin_instance_index}"
    # puts("var=#{var.inspect}")
    @variables[plugin] = var
  end
  return @variables
end

#variable(object) ⇒ Object



215
216
217
218
# File 'lib/logstash/config/config_ast.rb', line 215

def variable(object)
  generate_variables
  return @variables[object]
end