Skip to content
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

log consumer opens, closes, and exceptions #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions lib/logstash/inputs/kafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ def register

public
def run(logstash_queue)
@runner_consumers = consumer_threads.times.map { |i| create_consumer("#{client_id}-#{i}") }
@runner_threads = @runner_consumers.map { |consumer| thread_runner(logstash_queue, consumer) }
@runner_consumers = consumer_threads.times.map do |index|
create_consumer("#{client_id}-#{index}")
end
@runner_threads = @runner_consumers.map.with_index do |consumer, index|
thread_runner(logstash_queue, consumer, index)
end
@runner_threads.each { |t| t.join }
end # def run

Expand All @@ -237,9 +241,11 @@ def kafka_consumers
end

private
def thread_runner(logstash_queue, consumer)
def thread_runner(logstash_queue, consumer, consumer_index)
consumer_identifier = "#{client_id}-#{consumer_index}"
Thread.new do
begin
logger.info("opening consumer #{consumer_identifier}")
unless @topics_pattern.nil?
nooplistener = org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener.new
pattern = java.util.regex.Pattern.compile(@topics_pattern)
Expand Down Expand Up @@ -271,8 +277,15 @@ def thread_runner(logstash_queue, consumer)
end
end
rescue org.apache.kafka.common.errors.WakeupException => e
raise e if !stop?
unless stop?
logger.error("wakeup exception in consumer #{consumer_identifier}: #{e}")
raise e
end
rescue => e
logger.error("uncaught exception in consumer #{consumer_identifier}: #{e}")
Copy link

Choose a reason for hiding this comment

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

Can we use structured logging in these places? Logstash loggers let you say:

logger.error("uncaught exception in consumer", :consumer_identifier => consumer_identifier, :message => e.message, :error_class => e.class.name, :error_backtrace => e.backtrace)

raise e
ensure
logger.info("closing consumer #{consumer_identifier}")
consumer.close
end
end
Expand Down