Skip to content

Commit df7f714

Browse files
authored
Feat: try hard to log Java cause (chain) (#62)
1 parent 418a5b9 commit df7f714

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 5.y.z
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+
26
- Refactored Lookup used in jdbc_streaming and jdbc_static to avoid code duplication. [#59](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/59)
37

48
## 5.0.6

lib/logstash/plugin_mixins/jdbc/jdbc.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,32 @@ def jdbc_connect
122122
# rescue Java::JavaSql::SQLException, ::Sequel::Error => e
123123
rescue ::Sequel::Error => e
124124
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))
126127
raise e
127128
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))
129130
end
130131
end
131132
sleep(@connection_retry_attempts_wait_time)
132133
end
133134
end
134135

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+
135151
def open_jdbc_connection
136152
# at this point driver is already loaded
137153
Sequel.application_timezone = @plugin_timezone.to_sym

logstash-integration-jdbc.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-integration-jdbc'
3-
s.version = '5.0.6'
3+
s.version = '5.0.7'
44
s.licenses = ['Apache License (2.0)']
55
s.summary = "Integration with JDBC - input and filter plugins"
66
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"

spec/inputs/integration/integ_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@
6868

6969
it "should not register correctly" do
7070
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
7187
q = Queue.new
7288
expect do
7389
plugin.run(q)

0 commit comments

Comments
 (0)