Skip to content

Commit

Permalink
Make notifications go off even when an error is raised, so that we ca…
Browse files Browse the repository at this point in the history
…pture the underlying performance data [#4505 state:resolved]

This is important when trying to keep track of many layers of interrelated calls

i.e.:

ActiveRecord::Base.transaction do
  MyModel.find(1) #ActiveRecord::NotFound
end # should capture the full time until the error propagation

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
jaggederest authored and josevalim committed May 2, 2010
1 parent 02028e5 commit 109d3ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions activesupport/lib/active_support/notifications/instrumenter.rb
Expand Up @@ -15,9 +15,13 @@ def initialize(notifier)
# and publish it.
def instrument(name, payload={})
time = Time.now
result = yield(payload) if block_given?
@notifier.publish(name, time, Time.now, @id, payload)
result
begin
yield(payload) if block_given?
ensure
# Notify in an ensure block so that we can be certain end
# events get sent even if an error occurs in the passed-in block
@notifier.publish(name, time, Time.now, @id, payload)
end
end

private
Expand Down
4 changes: 2 additions & 2 deletions activesupport/test/notifications_test.rb
Expand Up @@ -168,7 +168,7 @@ def test_nested_events_can_be_instrumented
assert_equal Hash[:payload => "notifications"], @events.last.payload
end

def test_instrument_does_not_publish_when_exception_is_raised
def test_instrument_publishes_when_exception_is_raised
begin
instrument(:awesome, :payload => "notifications") do
raise "FAIL"
Expand All @@ -178,7 +178,7 @@ def test_instrument_does_not_publish_when_exception_is_raised
end

drain
assert_equal 0, @events.size
assert_equal 1, @events.size
end

def test_event_is_pushed_even_without_block
Expand Down

0 comments on commit 109d3ee

Please sign in to comment.