logstash-integration-jdbc 5.0.6 → 5.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd1883c21c6bbe0cdfb87cf641cc106d59cb5e4b43b9a7c7de547d9e1ff263f4
4
- data.tar.gz: 749710ce22a5c81ce8bb3b024803a2075052cd21aff5870ade488b7ae4dddef7
3
+ metadata.gz: 07f4d0662b489114358564f9c857f22de3dab11f0210eae7ef95ef4117b7dad7
4
+ data.tar.gz: aa469bddf21e83acee4144b3d257099de0038b221ffa4914624c0421b1d6e17d
5
5
  SHA512:
6
- metadata.gz: 0c3b0320dc3dfab3381fb2a62642e37e99445178f8d569e3f63dc0d1c83a4f4959398e4ca87163328380d28d389388bf5741a8dcbffd0a7897c151d518871690
7
- data.tar.gz: 77961a244d1fbfd0c9d5cf0049eb868fdd423869828733fc1a8e76cdede5f8d9bc9731ecf7668691aa0fbdbc805a5864a035cacb2c834c43a9eb6b594460f72c
6
+ metadata.gz: 91f9242f4d6d425272b5737491e519fd91c6757a8e5174a519192cc7871c13ecacb6b49e4874524aff3fafaacf509bf2824feb4a733242fd08700d8af74a17d6
7
+ data.tar.gz: fa2d734620f5147ed32841b0ad75fa4ff30905ea3f5af1ff1da78204c6ea9455192d5bde197f5d860e2e658a4d6a003c42180bc4bfa8fb5999cef8c0d6da2240
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 5.0.7
2
+ - Feat: try hard to log Java cause (chain) [#62](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/62)
3
+
4
+ This allows seeing a full trace from the JDBC driver in case of connection errors.
5
+
6
+ - Refactored Lookup used in jdbc_streaming and jdbc_static to avoid code duplication. [#59](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/59)
7
+
1
8
  ## 5.0.6
2
9
  - DOC:Replaced plugin_header file with plugin_header-integration file. [#40](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/40)
3
10
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  Logstash Integration Plugin for JDBC, including Logstash Input and Filter Plugins
3
3
  # Logstash Plugin
4
4
 
5
- [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-integration-jdbc.svg)](https://travis-ci.org/logstash-plugins/logstash-integration-jdbc)
5
+ [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-integration-jdbc.svg)](https://travis-ci.com/logstash-plugins/logstash-integration-jdbc)
6
6
 
7
7
  This is a plugin for [Logstash](https://github.com/elastic/logstash).
8
8
 
@@ -66,6 +66,7 @@ module LogStash module Filters module Jdbc
66
66
  @prepared_statement = nil
67
67
  @symbol_parameters = nil
68
68
  parse_options
69
+ @load_method_ref = method(:load_data_from_local)
69
70
  end
70
71
 
71
72
  def id_used_as_target?
@@ -81,11 +82,7 @@ module LogStash module Filters module Jdbc
81
82
  end
82
83
 
83
84
  def enhance(local, event)
84
- if @prepared_statement
85
- result = call_prepared(local, event)
86
- else
87
- result = fetch(local, event) # should return a LookupResult
88
- end
85
+ result = retrieve_local_data(local, event, &@load_method_ref) # return a LookupResult
89
86
  if result.failed? || result.parameters_invalid?
90
87
  tag_failure(event)
91
88
  end
@@ -112,6 +109,7 @@ module LogStash module Filters module Jdbc
112
109
  @prepared_parameters.each_with_index { |v, i| hash[:"$p#{i}"] = v }
113
110
  @prepared_param_placeholder_map = hash
114
111
  @prepared_statement = local.prepare(query, hash.keys)
112
+ @load_method_ref = method(:load_data_from_prepared)
115
113
  end
116
114
 
117
115
  private
@@ -128,34 +126,23 @@ module LogStash module Filters module Jdbc
128
126
  end
129
127
  end
130
128
 
131
- def fetch(local, event)
132
- result = LookupResult.new()
133
- if @parameters_specified
134
- params = prepare_parameters_from_event(event, result)
135
- if result.parameters_invalid?
136
- logger.warn? && logger.warn("Parameter field not found in event", :lookup_id => @id, :invalid_parameters => result.invalid_parameters)
137
- return result
138
- end
139
- else
140
- params = {}
129
+ def load_data_from_local(local, query, params, result)
130
+ local.fetch(query, params).each do |row|
131
+ stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
132
+ result.push(stringified)
141
133
  end
142
- begin
143
- logger.debug? && logger.debug("Executing Jdbc query", :lookup_id => @id, :statement => query, :parameters => params)
144
- local.fetch(query, params).each do |row|
145
- stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
146
- result.push(stringified)
147
- end
148
- rescue ::Sequel::Error => e
149
- # all sequel errors are a subclass of this, let all other standard or runtime errors bubble up
150
- result.failed!
151
- logger.warn? && logger.warn("Exception when executing Jdbc query", :lookup_id => @id, :exception => e.message, :backtrace => e.backtrace.take(8))
134
+ end
135
+
136
+ def load_data_from_prepared(_local, _query, params, result)
137
+ @prepared_statement.call(params).each do |row|
138
+ stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
139
+ result.push(stringified)
152
140
  end
153
- # if either of: no records or a Sequel exception occurs the payload is
154
- # empty and the default can be substituted later.
155
- result
156
141
  end
157
142
 
158
- def call_prepared(local, event)
143
+ # the &block is invoked with 4 arguments: local, query[String], params[Hash], result[LookupResult]
144
+ # the result is used as accumulator return variable
145
+ def retrieve_local_data(local, event, &proc)
159
146
  result = LookupResult.new()
160
147
  if @parameters_specified
161
148
  params = prepare_parameters_from_event(event, result)
@@ -168,10 +155,7 @@ module LogStash module Filters module Jdbc
168
155
  end
169
156
  begin
170
157
  logger.debug? && logger.debug("Executing Jdbc query", :lookup_id => @id, :statement => query, :parameters => params)
171
- @prepared_statement.call(params).each do |row|
172
- stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
173
- result.push(stringified)
174
- end
158
+ proc.call(local, query, params, result)
175
159
  rescue ::Sequel::Error => e
176
160
  # all sequel errors are a subclass of this, let all other standard or runtime errors bubble up
177
161
  result.failed!
@@ -122,16 +122,32 @@ module LogStash module PluginMixins module Jdbc
122
122
  # rescue Java::JavaSql::SQLException, ::Sequel::Error => e
123
123
  rescue ::Sequel::Error => e
124
124
  if retry_attempts <= 0
125
- @logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", :error_message => e.message)
125
+ log_java_exception(e.cause)
126
+ @logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", error_details(e, trace: true))
126
127
  raise e
127
128
  else
128
- @logger.error("Unable to connect to database. Trying again", :error_message => e.message)
129
+ @logger.error("Unable to connect to database. Trying again", error_details(e, trace: false))
129
130
  end
130
131
  end
131
132
  sleep(@connection_retry_attempts_wait_time)
132
133
  end
133
134
  end
134
135
 
136
+ def error_details(e, trace: false)
137
+ details = { :message => e.message, :exception => e.class }
138
+ details[:cause] = e.cause if e.cause
139
+ details[:backtrace] = e.backtrace if trace || @logger.debug?
140
+ details
141
+ end
142
+
143
+ def log_java_exception(e)
144
+ return unless e.is_a?(java.lang.Exception)
145
+ # @logger.name using the same convention as LS does
146
+ logger = self.class.name.gsub('::', '.').downcase
147
+ logger = org.apache.logging.log4j.LogManager.getLogger(logger)
148
+ logger.error('', e) # prints nested causes
149
+ end
150
+
135
151
  def open_jdbc_connection
136
152
  # at this point driver is already loaded
137
153
  Sequel.application_timezone = @plugin_timezone.to_sym
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-integration-jdbc'
3
- s.version = '5.0.6'
3
+ s.version = '5.0.7'
4
4
  s.licenses = ['Apache License (2.0)']
5
5
  s.summary = "Integration with JDBC - input and filter plugins"
6
6
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -59,7 +59,7 @@ module LogStash module Filters
59
59
 
60
60
  let(:plugin) { JdbcStatic.new(settings) }
61
61
 
62
- let(:event) { ::LogStash::Event.new("message" => "some text", "ip" => ipaddr) }
62
+ let(:event) { ::LogStash::Event.new("message" => "some text", "ip" => ipaddr) }
63
63
 
64
64
  let(:ipaddr) { ".3.1.1" }
65
65
 
@@ -45,7 +45,7 @@ describe LogStash::Inputs::Jdbc, :integration => true do
45
45
 
46
46
  context "when supplying a non-existent library" do
47
47
  let(:settings) do
48
- super.merge(
48
+ super().merge(
49
49
  "jdbc_driver_library" => "/no/path/to/postgresql.jar"
50
50
  )
51
51
  end
@@ -61,13 +61,29 @@ describe LogStash::Inputs::Jdbc, :integration => true do
61
61
 
62
62
  context "when connecting to a non-existent server" do
63
63
  let(:settings) do
64
- super.merge(
64
+ super().merge(
65
65
  "jdbc_connection_string" => "jdbc:postgresql://localhost:65000/somedb"
66
66
  )
67
67
  end
68
68
 
69
69
  it "should not register correctly" do
70
70
  plugin.register
71
+ allow( plugin ).to receive(:log_java_exception)
72
+ q = Queue.new
73
+ expect do
74
+ plugin.run(q)
75
+ end.to raise_error(::Sequel::DatabaseConnectionError)
76
+ end
77
+
78
+ it "should log (native) Java driver error" do
79
+ plugin.register
80
+ expect( org.apache.logging.log4j.LogManager ).to receive(:getLogger).and_wrap_original do |m, *args|
81
+ logger = m.call(*args)
82
+ expect( logger ).to receive(:error) do |_, e|
83
+ expect( e ).to be_a org.postgresql.util.PSQLException
84
+ end.and_call_original
85
+ logger
86
+ end
71
87
  q = Queue.new
72
88
  expect do
73
89
  plugin.run(q)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-integration-jdbc
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.6
4
+ version: 5.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-20 00:00:00.000000000 Z
11
+ date: 2021-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement