Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit 9804850

Browse files
committed
WIP
1 parent a6c669c commit 9804850

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

README.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,49 @@ See below for tested adapters, and example configurations.
66

77
This has not yet been extensively tested with all JDBC drivers and may not yet work for you.
88

9+
If you do find this works for a JDBC driver not listed, let me know and provide a small example configuration.
10+
911
This plugin does not bundle any JDBC jar files, and does expect them to be in a
1012
particular location. Please ensure you read the 4 installation lines below.
1113

14+
## Headlines
15+
- Support for connection pooling added in 0.2.0 [unreleased until #21 is resolved]
16+
- Support for unsafe statement handling (allowing dynamic queries) in 0.2.0 [unreleased until #21 is resolved]
17+
1218
## Versions
1319
- See master branch for logstash v2+
1420
- See v1.5 branch for logstash v1.5
1521
- See v1.4 branch for logstash 1.4
1622

1723
## Installation
1824
- Run `bin/plugin install logstash-output-jdbc` in your logstash installation directory
19-
- Create the directory vendor/jar/jdbc in your logstash installation (`mkdir -p vendor/jar/jdbc/`)
20-
- Add JDBC jar files to vendor/jar/jdbc in your logstash installation
21-
- Configure
25+
- Now either:
26+
- Use driver_class in your configuraton to specify a path to your jar file
27+
- Or:
28+
- Create the directory vendor/jar/jdbc in your logstash installation (`mkdir -p vendor/jar/jdbc/`)
29+
- Add JDBC jar files to vendor/jar/jdbc in your logstash installation
30+
- And then configure (examples below)
2231

2332
## Configuration options
24-
* driver_class, string, JDBC driver class to load
25-
* connection_string, string, JDBC connection string
26-
* statement, array, an array of strings representing the SQL statement to run. Index 0 is the SQL statement that is prepared, all other array entries are passed in as parameters (in order). A parameter may either be a property of the event (i.e. "@timestamp", or "host") or a formatted string (i.e. "%{host} - %{message}" or "%{message}"). If a key is passed then it will be automatically converted as required for insertion into SQL. If it's a formatted string then it will be passed in verbatim.
27-
* flush_size, number, default = 1000, number of entries to buffer before sending to SQL
28-
* idle_flush_time, number, default = 1, number of idle seconds before sending data to SQL, even if the flush_size has not been reached. If you modify this value you should also consider altering max_repeat_exceptions_time
29-
* max_repeat_exceptions, number, default = 5, number of times the same exception can repeat before we stop logstash. Set to a value less than 1 if you never want it to stop
30-
* max_repeat_exceptions_time, number, default = 30, maxium number of seconds between exceptions before they're considered "different" exceptions. If you modify idle_flush_time you should consider this value
33+
34+
| Option | Type | Description | Required? |
35+
| ------ | ---- | ----------- | --------- |
36+
| driver_path | String | File path to jar file containing your JDBC driver. This is optional, and all JDBC jars may be placed in $LOGSTASH_HOME/vendor/jar/jdbc instead. | No |
37+
| connection_string | String | JDBC connection URL | Yes |
38+
| username | String | JDBC username - this is optional as it may be included in the connection string, for many drivers | No |
39+
| password | String | JDBC password - this is optional as it may be included in the connection string, for many drivers | No |
40+
| statement | Array | An array of strings representing the SQL statement to run. Index 0 is the SQL statement that is prepared, all other array entries are passed in as parameters (in order). A parameter may either be a property of the event (i.e. "@timestamp", or "host") or a formatted string (i.e. "%{host} - %{message}" or "%{message}"). If a key is passed then it will be automatically converted as required for insertion into SQL. If it's a formatted string then it will be passed in verbatim. | Yes |
41+
| unsafe_statement | Boolean | If yes, the statement is evaluated for event fields - this allows you to use dynamic table names, etc. **This is highly dangerous** and you should **not** use this unless you are 100% sure that the field(s) you are passing in are 100% safe. Failure to do so will result in possible SQL injections. Please be aware that there is also a potential performance penalty as each event must be evaluated and inserted into SQL one at a time, where as when this is false multiple events are inserted at once. Example statement: [ "insert into %{table_name_field} (column) values(?)", "fieldname" ] | No |
42+
| max_pool_size | Number | Maximum number of connections to open to the SQL server at any 1 time | No |
43+
| connection_timeout | Number | Number of seconds before a SQL connection is closed | No |
44+
| flush_size | Number | Maximum number of entries to buffer before sending to SQL - if this is reached before idle_flush_time | No |
45+
| idle_flush_time | Number | Number of idle seconds before sending data to SQL - even if the flush_size has not yet been reached | No |
46+
| max_repeat_exceptions | Number | Number of times the same exception can repeat before we stop logstash. Set to a value less than 1 if you never want it to stop | No |
47+
| max_repeat_exceptions_time | Number | Maxium number of seconds between exceptions before they're considered "different" exceptions. If you modify idle_flush_time you should consider this value | No |
3148

3249
## Example configurations
50+
If you have a working sample configuration, for a DB thats not listed, pull requests are welcome.
51+
3352
### SQLite3
3453
* Tested using https://bitbucket.org/xerial/sqlite-jdbc
3554
* SQLite setup - `echo "CREATE table log (host text, timestamp datetime, message text);" | sqlite3 test.db`
@@ -42,7 +61,6 @@ output {
4261
stdout { }
4362
4463
jdbc {
45-
driver_class => 'org.sqlite.JDBC'
4664
connection_string => 'jdbc:sqlite:test.db'
4765
statement => [ "INSERT INTO log (host, timestamp, message) VALUES(?, ?, ?)", "host", "@timestamp", "message" ]
4866
}
@@ -58,7 +76,6 @@ input
5876
}
5977
output {
6078
jdbc {
61-
driver_class => 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
6279
connection_string => "jdbc:sqlserver://server:1433;databaseName=databasename;user=username;password=password;autoReconnect=true;"
6380
statement => [ "INSERT INTO log (host, timestamp, message) VALUES(?, ?, ?)", "host", "@timestamp", "message" ]
6481
}
@@ -74,7 +91,6 @@ input
7491
}
7592
output {
7693
jdbc {
77-
driver_class => 'org.postgresql.Driver'
7894
connection_string => 'jdbc:postgresql://hostname:5432/database?user=username&password=password'
7995
statement => [ "INSERT INTO log (host, timestamp, message) VALUES(?, CAST (? AS timestamp), ?)", "host", "@timestamp", "message" ]
8096
}
@@ -92,7 +108,6 @@ input
92108
}
93109
output {
94110
jdbc {
95-
driver_class => "oracle.jdbc.driver.OracleDriver"
96111
connection_string => "jdbc:oracle:thin:USER/PASS@HOST:PORT:SID"
97112
statement => [ "INSERT INTO log (host, timestamp, message) VALUES(?, CAST (? AS timestamp), ?)", "host", "@timestamp", "message" ]
98113
}
@@ -110,9 +125,13 @@ input
110125
}
111126
output {
112127
jdbc {
113-
driver_class => "com.mysql.jdbc.Driver"
114128
connection_string => "jdbc:mysql://HOSTNAME/DATABASE?user=USER&password=PASSWORD"
115129
statement => [ "INSERT INTO log (host, timestamp, message) VALUES(?, CAST (? AS timestamp), ?)", "host", "@timestamp", "message" ]
116130
}
117131
}
118132
```
133+
134+
### MariaDB
135+
This is reportedly working, according to [@db2882](https://github.com/db2882) in issue #20.
136+
No example configuration provided.
137+
If you have a working sample, pull requests are welcome.

lib/logstash/outputs/jdbc.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
1212
config_name "jdbc"
1313

1414
# Driver class - No longer required
15-
config :driver_class, :obsolete => true
15+
config :driver_class, :obsolete => "driver_class is no longer required and can be removed from your configuration"
1616

1717
# Where to find the jar
1818
# Defaults to not required, and to the original behaviour
@@ -104,7 +104,7 @@ def register
104104
end
105105

106106
def receive(event)
107-
return unless output?(event)
107+
return unless output?(event) or event.cancelled?
108108
return unless @statement.length > 0
109109

110110
buffer_receive(event)
@@ -173,28 +173,24 @@ def load_jar_files!
173173

174174
def safe_flush(events, teardown=false)
175175
connection = @pool.getConnection()
176-
177176
statement = connection.prepareStatement(@statement[0])
178177

179178
events.each do |event|
179+
next if event.cancelled?
180180
next if @statement.length < 2
181181
statement = add_statement_event_params(statement, event)
182182

183183
statement.addBatch()
184184
end
185185

186186
begin
187-
@logger.debug("JDBC - Sending SQL", :sql => statement.toString())
188187
statement.executeBatch()
189188
statement.close()
190189
rescue => e
191190
# Raising an exception will incur a retry from Stud::Buffer.
192191
# Since the exceutebatch failed this should mean any events failed to be
193192
# inserted will be re-run. We're going to log it for the lols anyway.
194-
@logger.warn("JDBC - Exception. Will automatically retry", :exception => e)
195-
if e.getNextException() != nil
196-
@logger.warn("JDBC - Exception. Will automatically retry", :exception => e.getNextException())
197-
end
193+
log_jdbc_exception(e)
198194
ensure
199195
connection.close();
200196
end
@@ -204,13 +200,25 @@ def unsafe_flush(events, teardown=false)
204200
connection = @pool.getConnection()
205201

206202
events.each do |event|
203+
next if event.cancelled?
204+
207205
statement = connection.prepareStatement(event.sprintf(@statement[0]))
208-
209206
statement = add_statement_event_params(statement, event) if @statement.length > 1
210207

211-
statement.execute()
212-
statement.close()
213-
connection.close()
208+
begin
209+
statement.execute()
210+
211+
# cancel the event, since we may end up outputting the same event multiple times
212+
# if an exception happens later down the line
213+
event.cancel
214+
rescue => e
215+
# Raising an exception will incur a retry from Stud::Buffer.
216+
# We log for the lols.
217+
log_jdbc_exception(e)
218+
ensure
219+
statement.close()
220+
connection.close()
221+
end
214222
end
215223
end
216224

@@ -237,4 +245,13 @@ def add_statement_event_params(statement, event)
237245

238246
statement
239247
end
248+
249+
def log_jdbc_exception(e)
250+
ce = e
251+
loop do
252+
@logger.error("JDBC Exception encountered: Will automatically retry.", :exception => ce)
253+
ce = e.getNextException()
254+
break if ce == nil
255+
end
256+
end
240257
end # class LogStash::Outputs::jdbc

logstash-output-jdbc.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-jdbc'
3-
s.version = "0.2.0.rc2"
3+
s.version = "0.2.0.rc3"
44
s.licenses = [ "Apache License (2.0)" ]
55
s.summary = "This plugin allows you to output to SQL, via JDBC"
66
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,7 +10,8 @@ Gem::Specification.new do |s|
1010
s.require_paths = [ "lib" ]
1111

1212
# Files
13-
s.files = `git ls-files`.split($\)
13+
s.files = Dir.glob("{lib,vendor,spec}/**/*") + %w(LICENSE.txt README.md)
14+
1415
# Tests
1516
s.test_files = s.files.grep(%r{^(test|spec|features)/})
1617

0 commit comments

Comments
 (0)