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

Add exception to instrumentation payload #145

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 21 additions & 18 deletions lib/karafka/core/monitoring/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,30 @@ def instrument(event_id, payload = EMPTY_HASH)
return yield if assigned_listeners.empty?
Copy link
Author

Choose a reason for hiding this comment

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

I'm thinking that we might also want to remove this early safeguard (if assigned_listeners.empty?) here above, given there is a similar safeguard in the ensure block below.
I understand that we are adding the overhead of two calls to monotonic_now in such case, in exchange for one less assigned_listeners.empty? check.


start = monotonic_now
result = yield
time = monotonic_now - start
elsif assigned_listeners.empty?
# Skip measuring or doing anything if no one listening
return
begin
yield
rescue Exception => e
payload[:exception] = e
raise e
ensure
time = monotonic_now - start
end
end

event = Event.new(
event_id,
time ? payload.merge(time: time) : payload
)

assigned_listeners.each do |listener|
if listener.is_a?(Proc)
listener.call(event)
else
listener.send(@events_methods_map[event_id], event)
ensure
if assigned_listeners && !assigned_listeners.empty?
event = Event.new(
event_id,
time ? payload.merge(time: time) : payload
)

assigned_listeners.each do |listener|
if listener.is_a?(Proc)
listener.call(event)
else
listener.send(@events_methods_map[event_id], event)
end
end
end

result
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/karafka/core/monitoring/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@
expect { notifications.instrument('na') }.to raise_error(expected_error)
end
end

context 'when the instrumented code errors' do
let(:result) { raise ArgumentError, "error message" }

it 'expect to raise the error' do
expect { instrumentation }.to raise_error(ArgumentError, "error message")
end

context 'with a subscriber' do
let(:subscriber_block) { double('block') }

before do
allow(subscriber_block).to receive(:call)
notifications.subscribe(event_name, &subscriber_block.method(:call))
end

it 'expect to provide exception information in event' do
expect(subscriber_block).to receive(:call) do |event|
expect(event[:exception]).to be_a(ArgumentError)
expect(event[:exception].message).to eq("error message")
end

expect { instrumentation }.to raise_error(ArgumentError)
end
end
end
end

describe '#subscribe' do
Expand Down