Class: LogStash::Inputs::Jdbc

Inherits:
Base
  • Object
show all
Extended by:
PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
Includes:
PluginMixins::ECSCompatibilitySupport::TargetCheck, PluginMixins::EventSupport::EventFactoryAdapter, PluginMixins::Jdbc::Common, PluginMixins::Jdbc::Jdbc, PluginMixins::Scheduler
Defined in:
lib/logstash/inputs/jdbc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PluginMixins::Jdbc::Jdbc

#close_jdbc_connection, #execute_statement, #get_column_value, included, #setup_jdbc_config

Methods included from PluginMixins::Jdbc::ValueHandler

#decorate_value, #extract_values_from

Instance Attribute Details

#databaseObject (readonly)

for test mocking/stubbing



236
237
238
# File 'lib/logstash/inputs/jdbc.rb', line 236

def database
  @database
end

#last_run_metadata_file_pathObject (readonly)

path to the file used as last run storage



237
238
239
# File 'lib/logstash/inputs/jdbc.rb', line 237

def 
  @last_run_metadata_file_path
end

Instance Method Details

#registerObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/logstash/inputs/jdbc.rb', line 241

def register
  @logger = self.logger

  if @record_last_run
    if @last_run_metadata_path.nil?
      logstash_data_path = LogStash::SETTINGS.get_value("path.data")
      logstash_data_path = Pathname.new(logstash_data_path).join("plugins", "inputs", "jdbc")
      # Ensure that the filepath exists before writing, since it's deeply nested.
      logstash_data_path.mkpath
      logstash_data_file_path = logstash_data_path.join("logstash_jdbc_last_run")

      ensure_default_metadatafile_location(logstash_data_file_path)

      @last_run_metadata_file_path = logstash_data_file_path.to_path
    else
      #  validate the path is a file and not a directory
      if Pathname.new(@last_run_metadata_path).directory?
        raise LogStash::ConfigurationError.new("The \"last_run_metadata_path\" argument must point to a file, received a directory: \"#{}\"")
      end
      @last_run_metadata_file_path = @last_run_metadata_path
    end
  end

  if @use_column_value
    # Raise an error if @use_column_value is true, but no @tracking_column is set
    if @tracking_column.nil?
      raise(LogStash::ConfigurationError, "Must set :tracking_column if :use_column_value is true.")
    end
  end

  unless @statement.nil? ^ @statement_filepath.nil?
    raise(LogStash::ConfigurationError, "Must set either :statement or :statement_filepath. Only one may be set at a time.")
  end

  @statement = ::File.read(@statement_filepath) if @statement_filepath

  # must validate prepared statement mode after trying to read in from @statement_filepath
  if @use_prepared_statements
    validation_errors = validate_prepared_statement_mode
    unless validation_errors.empty?
      raise(LogStash::ConfigurationError, "Prepared Statement Mode validation errors: " + validation_errors.join(", "))
    end
  end

  set_value_tracker LogStash::PluginMixins::Jdbc::ValueTracking.build_last_value_tracker(self)
  set_statement_handler LogStash::PluginMixins::Jdbc::StatementHandler.build_statement_handler(self)

  @enable_encoding = !@charset.nil? || !@columns_charset.empty?

  if (@jdbc_password_filepath and @jdbc_password)
    raise(LogStash::ConfigurationError, "Only one of :jdbc_password, :jdbc_password_filepath may be set at a time.")
  end

  @jdbc_password = LogStash::Util::Password.new(::File.read(@jdbc_password_filepath).strip) if @jdbc_password_filepath

  if enable_encoding?
    encodings = @columns_charset.values
    encodings << @charset if @charset

    @converters = encodings.each_with_object({}) do |encoding, converters|
      converter = LogStash::Util::Charset.new(encoding)
      converter.logger = self.logger
      converters[encoding] = converter
    end
  end

  load_driver
  begin
    open_jdbc_connection
  rescue Sequel::DatabaseConnectionError,
         Sequel::DatabaseError,
         Sequel::InvalidValue,
         Java::JavaSql::SQLException => e
    details = { exception: e.class, message: e.message }
    details[:cause] = e.cause.inspect if e.cause
    details[:backtrace] = e.backtrace if @logger.debug?
    @logger.warn("Exception when executing JDBC query", details)
    raise(LogStash::ConfigurationError, "Can't create a connection pool to the database")
  end
end

#run(queue) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/logstash/inputs/jdbc.rb', line 331

def run(queue)
  if @schedule
    # scheduler input thread name example: "[my-oracle]|input|jdbc|scheduler"
    scheduler.cron(@schedule) { execute_query(queue) }
    scheduler.join
  else
    execute_query(queue)
  end
end

#set_statement_handler(handler) ⇒ Object

test injection points



323
324
325
# File 'lib/logstash/inputs/jdbc.rb', line 323

def set_statement_handler(handler)
  @statement_handler = handler
end

#set_value_tracker(instance) ⇒ Object



327
328
329
# File 'lib/logstash/inputs/jdbc.rb', line 327

def set_value_tracker(instance)
  @value_tracker = instance
end

#stopObject

def run



341
342
343
# File 'lib/logstash/inputs/jdbc.rb', line 341

def stop
  close_jdbc_connection
end