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



150
151
152
# File 'lib/logstash/config/config_ast.rb', line 150

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

Instance Method Details

#compile_initializerObject

Generate ruby code to initialize all the plugins.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/logstash/config/config_ast.rb', line 155

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/logstash/config/config_ast.rb', line 200

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



195
196
197
198
# File 'lib/logstash/config/config_ast.rb', line 195

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