Skip to content

Add metrics to the jdbc input operations #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.2.0
- Add metrics support for events, operations, connections and errors produced during execution (based on exception being raised).

## 4.1.1
- Relax constraint on logstash-core-plugin-api to >= 1.60 <= 2.99

Expand Down
7 changes: 7 additions & 0 deletions lib/logstash/inputs/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class LogStash::Inputs::Jdbc < LogStash::Inputs::Base
public

def register
@metric_errors = metric.namespace(:errors)

require "rufus/scheduler"
prepare_jdbc_connection

Expand Down Expand Up @@ -247,6 +249,7 @@ def stop
private

def execute_query(queue)
metric.increment(:queries)
# update default parameters
@parameters['sql_last_value'] = @sql_last_value
execute_statement(@statement, @parameters) do |row|
Expand All @@ -257,11 +260,13 @@ def execute_query(queue)
event = LogStash::Event.new(row)
decorate(event)
queue << event
metric.increment(:events)
end
end

def update_state_file
if @record_last_run
metric.increment(:state_file_updates)
File.write(@last_run_metadata_path, YAML.dump(@sql_last_value))
end
end
Expand All @@ -279,9 +284,11 @@ def convert(column_name, value)
if column_charset
converter = @converters[column_charset]
converter.convert(value)
metric.increment(:encoding_conversions)
elsif @charset
converter = @converters[@charset]
converter.convert(value)
metric.increment(:encoding_conversions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the usefulness of theses metrics?

else
value
end
Expand Down
10 changes: 9 additions & 1 deletion lib/logstash/plugin_mixins/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@ def jdbc_connect
@logger.error("Failed to connect to database. #{@jdbc_pool_timeout} second timeout exceeded. Tried #{@connection_retry_attempts} times.")
raise e
else
@logger.error("Failed to connect to database. #{@jdbc_pool_timeout} second timeout exceeded. Trying again.")
@logger.error("Failed to connect to database. #{@jdbc_pool_timeout} second timeout exceeded. Trying again.")
@metric_errors.increment(:connection_retries)
end
rescue Sequel::Error => e
if retry_attempts <= 0
@logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", :error_message => e.message, )
raise e
else
@logger.error("Unable to connect to database. Trying again", :error_message => e.message)
@metric_errors.increment(:connection_retries)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The retries name here feels like not the right one. Maybe we call this just :connection ? So it may read as "connection errors" instead of "connection retry errors".

end
end
sleep(@connection_retry_attempts_wait_time)
Expand Down Expand Up @@ -159,6 +161,8 @@ def prepare_jdbc_connection
raise LogStash::ConfigurationError, "#{e}. #{message}"
end
@database = jdbc_connect()
metric.increment(:connections)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be only called at register time? so it will be always 1 ?

@database.extension(:pagination)
if @jdbc_default_timezone
@database.extension(:named_timezones)
Expand Down Expand Up @@ -201,8 +205,11 @@ def execute_statement(statement, parameters)
parameters = symbolized_params(parameters)
query = @database[statement, parameters]
sql_last_value = @use_column_value ? @sql_last_value : Time.now.utc
metric.gauge(:sql_last_value, sql_last_value)

@tracking_column_warning_sent = false
@logger.debug? and @logger.debug("Executing JDBC query", :statement => statement, :parameters => parameters, :count => query.count)
metric.gauge(:rows_in, query.count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose just rows (this is an input, so the in feels implied to me)


if @jdbc_paging_enabled
query.each_page(@jdbc_page_size) do |paged_dataset|
Expand All @@ -220,6 +227,7 @@ def execute_statement(statement, parameters)
success = true
rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError => e
@logger.warn("Exception when executing JDBC query", :exception => e)
@metric_errors.increment(:jdbc_query_errors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if we remove the mention of jdbc and maybe have
DatabaseConnectionError and DatabaseError in two different metric?

else
@sql_last_value = sql_last_value
end
Expand Down
2 changes: 1 addition & 1 deletion logstash-input-jdbc.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-input-jdbc'
s.version = '4.1.1'
s.version = '4.2.0'
s.licenses = ['Apache License (2.0)']
s.summary = "This example input streams a string at a definable interval."
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"
Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/jdbc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@
let(:logger) { double("logger") }

it "should report the staments to logging" do
expect(logger).to receive(:debug).with(kind_of(String)).once
expect(logger).to receive(:debug).with(kind_of(String)).twice
plugin.run(queue)
end
end
Expand Down