Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ded1106b13 | ||
|
|
2b27f39088 | ||
|
|
7b337a8b91 | ||
|
|
927e532b2a | ||
|
|
26a32a3f08 | ||
|
|
6bb84b165f | ||
|
|
4e0292d222 | ||
|
|
909cae01b3 | ||
|
|
6f2bd2ab3e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
Gemfile.bak
|
Gemfile.bak
|
||||||
.bundle
|
.bundle
|
||||||
|
.vagrant
|
||||||
|
|||||||
8
.travis.yml
Normal file
8
.travis.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
language: ruby
|
||||||
|
cache: bundler
|
||||||
|
rvm:
|
||||||
|
- jruby
|
||||||
|
before_script:
|
||||||
|
- wget http://search.maven.org/remotecontent?filepath=org/apache/derby/derby/10.12.1.1/derby-10.12.1.1.jar -O /tmp/derby.jar
|
||||||
|
- export JDBC_DERBY_JAR=/tmp/derby.jar
|
||||||
|
script: bundle exec rspec
|
||||||
@@ -1,6 +1,13 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
All notable changes to this project will be documented in this file, from 0.2.0.
|
All notable changes to this project will be documented in this file, from 0.2.0.
|
||||||
|
|
||||||
|
## [0.2.7] - 2016-05-29
|
||||||
|
- Backport retry exception logic from v5 branch
|
||||||
|
- Backport improved timestamp compatibility from v5 branch
|
||||||
|
|
||||||
|
## [0.2.6] - 2016-05-02
|
||||||
|
- Fix for exception infinite loop
|
||||||
|
|
||||||
## [0.2.5] - 2016-04-11
|
## [0.2.5] - 2016-04-11
|
||||||
### Added
|
### Added
|
||||||
- Basic tests running against DerbyDB
|
- Basic tests running against DerbyDB
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -1,4 +1,7 @@
|
|||||||
# logstash-output-jdbc
|
# logstash-output-jdbc
|
||||||
|
|
||||||
|
[](https://travis-ci.org/theangryangel/logstash-output-jdbc)
|
||||||
|
|
||||||
This plugin is provided as an external plugin and is not part of the Logstash project.
|
This plugin is provided as an external plugin and is not part of the Logstash project.
|
||||||
|
|
||||||
This plugin allows you to output to SQL databases, using JDBC adapters.
|
This plugin allows you to output to SQL databases, using JDBC adapters.
|
||||||
@@ -6,7 +9,7 @@ See below for tested adapters, and example configurations.
|
|||||||
|
|
||||||
This has not yet been extensively tested with all JDBC drivers and may not yet work for you.
|
This has not yet been extensively tested with all JDBC drivers and may not yet work for you.
|
||||||
|
|
||||||
If you do find this works for a JDBC driver not listed, let me know and provide a small example configuration.
|
If you do find this works for a JDBC driver without an example, let me know and provide a small example configuration if you can.
|
||||||
|
|
||||||
This plugin does not bundle any JDBC jar files, and does expect them to be in a
|
This plugin does not bundle any JDBC jar files, and does expect them to be in a
|
||||||
particular location. Please ensure you read the 4 installation lines below.
|
particular location. Please ensure you read the 4 installation lines below.
|
||||||
@@ -15,10 +18,11 @@ particular location. Please ensure you read the 4 installation lines below.
|
|||||||
See CHANGELOG.md
|
See CHANGELOG.md
|
||||||
|
|
||||||
## Versions
|
## Versions
|
||||||
Released versions are are tagged as of v0.2.1, and available via rubygems.
|
Released versions are available via rubygems, and typically tagged.
|
||||||
|
|
||||||
For development:
|
For development:
|
||||||
- See master branch for logstash v2
|
- See master branch for logstash v5
|
||||||
|
- See v2.x branch for logstash v2
|
||||||
- See v1.5 branch for logstash v1.5
|
- See v1.5 branch for logstash v1.5
|
||||||
- See v1.4 branch for logstash 1.4
|
- See v1.4 branch for logstash 1.4
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,33 @@ require "java"
|
|||||||
require "logstash-output-jdbc_jars"
|
require "logstash-output-jdbc_jars"
|
||||||
require "logstash-output-jdbc_ring-buffer"
|
require "logstash-output-jdbc_ring-buffer"
|
||||||
|
|
||||||
|
# Write events to a SQL engine, using JDBC.
|
||||||
|
#
|
||||||
|
# It is upto the user of the plugin to correctly configure the plugin. This
|
||||||
|
# includes correctly crafting the SQL statement, and matching the number of
|
||||||
|
# parameters correctly.
|
||||||
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
|
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
|
||||||
# Adds buffer support
|
# Adds buffer support
|
||||||
include Stud::Buffer
|
include Stud::Buffer
|
||||||
|
|
||||||
|
STRFTIME_FMT = '%Y-%m-%d %T.%L'.freeze
|
||||||
|
|
||||||
|
RETRYABLE_SQLSTATE_CLASSES = [
|
||||||
|
# Classes of retryable SQLSTATE codes
|
||||||
|
# Not all in the class will be retryable. However, this is the best that
|
||||||
|
# we've got right now.
|
||||||
|
# If a custom state code is required, set it in retry_sql_states.
|
||||||
|
'08', # Connection Exception
|
||||||
|
'24', # Invalid Cursor State (Maybe retry-able in some circumstances)
|
||||||
|
'25', # Invalid Transaction State
|
||||||
|
'40', # Transaction Rollback
|
||||||
|
'53', # Insufficient Resources
|
||||||
|
'54', # Program Limit Exceeded (MAYBE)
|
||||||
|
'55', # Object Not In Prerequisite State
|
||||||
|
'57', # Operator Intervention
|
||||||
|
'58', # System Error
|
||||||
|
].freeze
|
||||||
|
|
||||||
config_name "jdbc"
|
config_name "jdbc"
|
||||||
|
|
||||||
# Driver class - Reintroduced for https://github.com/theangryangel/logstash-output-jdbc/issues/26
|
# Driver class - Reintroduced for https://github.com/theangryangel/logstash-output-jdbc/issues/26
|
||||||
@@ -194,56 +217,73 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def safe_flush(events, teardown=false)
|
def safe_flush(events, teardown=false)
|
||||||
connection = @pool.getConnection()
|
connection = nil
|
||||||
statement = connection.prepareStatement(@statement[0])
|
statement = nil
|
||||||
|
|
||||||
events.each do |event|
|
begin
|
||||||
next if event.cancelled?
|
connection = @pool.getConnection()
|
||||||
next if @statement.length < 2
|
rescue => e
|
||||||
statement = add_statement_event_params(statement, event)
|
log_jdbc_exception(e)
|
||||||
|
raise
|
||||||
statement.addBatch()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
statement = connection.prepareStatement(@statement[0])
|
||||||
|
|
||||||
|
events.each do |event|
|
||||||
|
next if event.cancelled?
|
||||||
|
next if @statement.length < 2
|
||||||
|
statement = add_statement_event_params(statement, event)
|
||||||
|
|
||||||
|
statement.addBatch()
|
||||||
|
end
|
||||||
|
|
||||||
statement.executeBatch()
|
statement.executeBatch()
|
||||||
statement.close()
|
statement.close()
|
||||||
@exceptions_tracker << nil
|
@exceptions_tracker << nil
|
||||||
|
|
||||||
rescue => e
|
rescue => e
|
||||||
# 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.
|
|
||||||
log_jdbc_exception(e)
|
log_jdbc_exception(e)
|
||||||
|
if retry_exception?(e)
|
||||||
|
raise
|
||||||
|
end
|
||||||
ensure
|
ensure
|
||||||
connection.close();
|
statement.close() unless statement.nil?
|
||||||
|
connection.close() unless connection.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unsafe_flush(events, teardown=false)
|
def unsafe_flush(events, teardown=false)
|
||||||
connection = @pool.getConnection()
|
connection = nil
|
||||||
|
statement = nil
|
||||||
|
begin
|
||||||
|
connection = @pool.getConnection()
|
||||||
|
rescue => e
|
||||||
|
log_jdbc_exception(e)
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
events.each do |event|
|
begin
|
||||||
next if event.cancelled?
|
events.each do |event|
|
||||||
|
next if event.cancelled?
|
||||||
|
|
||||||
statement = connection.prepareStatement(event.sprintf(@statement[0]))
|
statement = connection.prepareStatement(event.sprintf(@statement[0]))
|
||||||
statement = add_statement_event_params(statement, event) if @statement.length > 1
|
statement = add_statement_event_params(statement, event) if @statement.length > 1
|
||||||
|
|
||||||
begin
|
|
||||||
statement.execute()
|
statement.execute()
|
||||||
|
|
||||||
# cancel the event, since we may end up outputting the same event multiple times
|
# cancel the event, since we may end up outputting the same event multiple times
|
||||||
# if an exception happens later down the line
|
# if an exception happens later down the line
|
||||||
event.cancel
|
event.cancel
|
||||||
@exceptions_tracker << nil
|
@exceptions_tracker << nil
|
||||||
rescue => e
|
|
||||||
# Raising an exception will incur a retry from Stud::Buffer.
|
|
||||||
# We log for the lols.
|
|
||||||
log_jdbc_exception(e)
|
|
||||||
ensure
|
|
||||||
statement.close()
|
|
||||||
connection.close()
|
|
||||||
end
|
end
|
||||||
|
rescue => e
|
||||||
|
log_jdbc_exception(e)
|
||||||
|
if retry_exception?(e)
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
statement.close() unless statement.nil?
|
||||||
|
connection.close() unless connection.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -251,11 +291,17 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
|
|||||||
@statement[1..-1].each_with_index do |i, idx|
|
@statement[1..-1].each_with_index do |i, idx|
|
||||||
case event[i]
|
case event[i]
|
||||||
when Time
|
when Time
|
||||||
# Most reliable solution, cross JDBC driver
|
# See LogStash::Timestamp, below, for the why behind strftime.
|
||||||
statement.setString(idx + 1, event[i].iso8601())
|
statement.setString(idx + 1, event[i].strftime(STRFTIME_FMT))
|
||||||
when LogStash::Timestamp
|
when LogStash::Timestamp
|
||||||
# Most reliable solution, cross JDBC driver
|
# XXX: Using setString as opposed to setTimestamp, because setTimestamp
|
||||||
statement.setString(idx + 1, event[i].to_iso8601())
|
# doesn't behave correctly in some drivers (Known: sqlite)
|
||||||
|
#
|
||||||
|
# Additionally this does not use `to_iso8601`, since some SQL databases
|
||||||
|
# choke on the 'T' in the string (Known: Derby).
|
||||||
|
#
|
||||||
|
# strftime appears to be the most reliable across drivers.
|
||||||
|
statement.setString(idx + 1, event[i].time.strftime(STRFTIME_FMT))
|
||||||
when Fixnum, Integer
|
when Fixnum, Integer
|
||||||
statement.setInt(idx + 1, event[i])
|
statement.setInt(idx + 1, event[i])
|
||||||
when Float
|
when Float
|
||||||
@@ -278,12 +324,20 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
|
|||||||
statement
|
statement
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_jdbc_exception(e)
|
def log_jdbc_exception(exception)
|
||||||
ce = e
|
current_exception = exception
|
||||||
loop do
|
loop do
|
||||||
@logger.error("JDBC Exception encountered: Will automatically retry.", :exception => ce)
|
@logger.error("JDBC Exception encountered: Will automatically retry.", :exception => current_exception)
|
||||||
ce = e.getNextException()
|
current_exception = current_exception.getNextException()
|
||||||
break if ce == nil
|
break if current_exception == nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def retry_exception?(exception)
|
||||||
|
if exception.respond_to? 'getSQLState'
|
||||||
|
return RETRYABLE_SQLSTATE_CLASSES.include?(e.getSQLState[0,2])
|
||||||
|
end
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
end # class LogStash::Outputs::jdbc
|
end # class LogStash::Outputs::jdbc
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = 'logstash-output-jdbc'
|
s.name = 'logstash-output-jdbc'
|
||||||
s.version = "0.2.5"
|
s.version = "0.2.8"
|
||||||
s.licenses = [ "Apache License (2.0)" ]
|
s.licenses = [ "Apache License (2.0)" ]
|
||||||
s.summary = "This plugin allows you to output to SQL, via JDBC"
|
s.summary = "This plugin allows you to output to SQL, via JDBC"
|
||||||
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
|
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
|
||||||
|
|||||||
@@ -10,10 +10,7 @@ describe LogStash::Outputs::Jdbc do
|
|||||||
"driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
|
"driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
|
||||||
"connection_string" => "jdbc:derby:memory:testdb;create=true",
|
"connection_string" => "jdbc:derby:memory:testdb;create=true",
|
||||||
"driver_jar_path" => ENV['JDBC_DERBY_JAR'],
|
"driver_jar_path" => ENV['JDBC_DERBY_JAR'],
|
||||||
# Grumble. Grumble.
|
"statement" => [ "insert into log (created_at, message) values(?, ?)", "@timestamp" "message" ]
|
||||||
# Derby doesn't like 'T' in timestamps as of current writing, so for now
|
|
||||||
# we'll just use CURRENT_TIMESTAMP as opposed to the event @timestamp
|
|
||||||
"statement" => [ "insert into log (created_at, message) values(CURRENT_TIMESTAMP, ?)", "message" ]
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -88,6 +85,7 @@ describe LogStash::Outputs::Jdbc do
|
|||||||
end
|
end
|
||||||
stmt.close()
|
stmt.close()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
expect(count).to be > 0
|
expect(count).to be > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user