logstash-output-jdbc/lib/logstash/outputs/jdbc.rb

103 lines
3.1 KiB
Ruby
Raw Normal View History

2014-04-15 11:32:41 +00:00
# encoding: utf-8
require "logstash/outputs/base"
require "logstash/namespace"
2014-05-31 13:33:59 +00:00
require "stud/buffer"
2014-04-15 11:32:41 +00:00
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
2014-05-31 13:33:59 +00:00
# Adds buffer support
include Stud::Buffer
2014-04-15 11:32:41 +00:00
config_name "jdbc"
milestone 1
# Driver class
config :driver_class, :validate => :string
# connection string
config :connection_string, :validate => :string, :required => true
# [ "insert into table (message) values(?)", "%{message}" ]
config :statement, :validate => :array, :required => true
2014-05-31 13:33:59 +00:00
# We buffer a certain number of events before flushing that out to SQL.
# This setting controls how many events will be buffered before sending a
# batch of events.
2014-05-31 13:33:59 +00:00
config :flush_size, :validate => :number, :default => 1000
# The amount of time since last flush before a flush is forced.
#
# This setting helps ensure slow event rates don't get stuck in Logstash.
# For example, if your `flush_size` is 100, and you have received 10 events,
# and it has been more than `idle_flush_time` seconds since the last flush,
# Logstash will flush those 10 events automatically.
#
# This helps keep both fast and slow log streams moving along in
# a timely manner.
config :idle_flush_time, :validate => :number, :default => 1
2014-04-15 11:32:41 +00:00
public
def register
@logger.info("Starting up JDBC")
2014-04-16 15:41:45 +00:00
require "java"
2014-04-15 11:32:41 +00:00
jarpath = File.join(File.dirname(__FILE__), "../../../vendor/jar/jdbc/*.jar")
@logger.info(jarpath)
Dir[jarpath].each do |jar|
@logger.debug("JDBC loaded jar", :jar => jar)
require jar
end
import @driver_class
driver = Object.const_get(@driver_class[@driver_class.rindex('.') + 1, @driver_class.length]).new
@connection = driver.connect(@connection_string, java.util.Properties.new)
2014-04-16 15:41:45 +00:00
@logger.debug("JDBC", :driver => driver, :connection => @connection)
2014-05-31 13:33:59 +00:00
if (@flush_size > 1000)
@logger.warn("JDBC - flush size is set to > 1000. May have performance penalties, depending on your SQL engine.")
end
buffer_initialize(
:max_items => @flush_size,
:max_interval => @idle_flush_time,
:logger => @logger
)
2014-04-15 11:32:41 +00:00
end
def receive(event)
return unless output?(event)
2014-04-16 15:41:45 +00:00
return unless @statement.length > 0
2014-05-31 13:33:59 +00:00
buffer_receive(event)
end
2014-04-16 15:41:45 +00:00
2014-05-31 13:33:59 +00:00
def flush(events, teardown=false)
statement = @connection.prepareStatement(@statement[0])
events.each do |event|
@statement[1..-1].each_with_index { |i, idx| statement.setString(idx + 1, event.sprintf(i)) } if @statement.length > 1
statement.addBatch()
end
2014-04-16 15:41:45 +00:00
2014-04-22 09:01:41 +00:00
begin
@logger.debug("Sending SQL to server", :sql => statement.toString())
2014-05-31 13:33:59 +00:00
statement.executeBatch()
rescue => e
2014-05-31 13:33:59 +00:00
# Raising an exception will incur a retry from Stud::Buffer.
# Since the exceutebatch failed this should mean any events failed to be
# inserted will be re-run. We're going to log it for the lols anyway.
@logger.error("JDBC Exception", :exception => e)
2014-04-22 09:01:41 +00:00
end
2014-05-31 13:33:59 +00:00
2014-04-16 15:41:45 +00:00
statement.close()
end
def teardown
2014-05-31 13:33:59 +00:00
buffer_flush(:final => true)
2014-04-16 15:41:45 +00:00
@connection.close()
super
2014-04-15 11:32:41 +00:00
end
end # class LogStash::Outputs::jdbc