Skip to content

Commit

Permalink
add closing event to info instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed Feb 6, 2024
1 parent 9bda714 commit 76a1c19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/waterdrop/instrumentation/logger_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ def on_buffer_purged(event)
end

# @param event [Dry::Events::Event] event that happened with the details
def on_producer_closing(event)
info(event, 'Closing producer')
end

# @param event [Dry::Events::Event] event that happened with the details
# @note While this says "Closing producer", it produces a nice message with time taken:
# "Closing producer took 12 ms" indicating it happened in the past.
def on_producer_closed(event)
info(event, 'Closing producer')
end
Expand Down Expand Up @@ -180,7 +187,11 @@ def debug(event, log_message)
# @param event [Dry::Events::Event] event that happened with the details
# @param log_message [String] message we want to publish
def info(event, log_message)
@logger.info("[#{event[:producer_id]}] #{log_message} took #{event[:time]} ms")
if event.payload.key?(:time)
@logger.info("[#{event[:producer_id]}] #{log_message} took #{event[:time]} ms")
else
@logger.info("[#{event[:producer_id]}] #{log_message}")
end
end

# @param event [Dry::Events::Event] event that happened with the details
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/waterdrop/instrumentation/logger_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@
it { expect(logged_data[1]).to eq(nil) }
end

describe '#on_producer_closing' do
before { listener.on_producer_closing(event) }

it { expect(logged_data[0]).to include(producer.id) }
it { expect(logged_data[0]).to include('INFO') }
it { expect(logged_data[0]).to include('Closing producer') }
end

describe '#on_producer_closed' do
before { listener.on_producer_closed(event) }

Expand Down

0 comments on commit 76a1c19

Please sign in to comment.