Skip to content

Commit

Permalink
Merge pull request #49262 from ipc103/transaction-instrumentation-pay…
Browse files Browse the repository at this point in the history
…load-outcome

Add outcome to transaction tracking payload
  • Loading branch information
rafaelfranca committed Sep 14, 2023
2 parents 4d4f105 + cf8ab2f commit 9e34e3b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG.md
Expand Up @@ -16,7 +16,7 @@

* Add instrumentation for Active Record transactions

Allows subscribing to transaction events for tracking/instrumentation. The event payload contains the connection, as well as timing details.
Allows subscribing to transaction events for tracking/instrumentation. The event payload contains the connection and the outcome (commit, rollback, restart, incomplete), as well as timing details.

```ruby
ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
Expand Down
Expand Up @@ -78,21 +78,24 @@ class TransactionInstrumenter
def initialize(payload = {})
@handle = nil
@started = false
@payload = payload
@payload = nil
@base_payload = payload
end

def start
return if @started
@started = true

@payload = @base_payload.dup
@handle = ActiveSupport::Notifications.instrumenter.build_handle("transaction.active_record", @payload)
@handle.start
end

def finish
def finish(outcome)
return unless @started
@started = false

@payload[:outcome] = outcome
@handle.finish
end
end
Expand Down Expand Up @@ -163,7 +166,7 @@ def restartable?
end

def incomplete!
@instrumenter.finish
@instrumenter.finish(:incomplete)
end

def materialize!
Expand Down Expand Up @@ -334,7 +337,7 @@ def materialize!
def restart
return unless materialized?

@instrumenter.finish
@instrumenter.finish(:restart)
@instrumenter.start

connection.rollback_to_savepoint(savepoint_name)
Expand All @@ -345,13 +348,13 @@ def rollback
connection.rollback_to_savepoint(savepoint_name) if materialized?
end
@state.rollback!
@instrumenter.finish
@instrumenter.finish(:rollback)
end

def commit
connection.release_savepoint(savepoint_name) if materialized?
@state.commit!
@instrumenter.finish
@instrumenter.finish(:commit)
end

def full_rollback?; false; end
Expand All @@ -372,7 +375,7 @@ def materialize!
def restart
return unless materialized?

@instrumenter.finish
@instrumenter.finish(:restart)

if connection.supports_restart_db_transaction?
@instrumenter.start
Expand All @@ -386,13 +389,13 @@ def restart
def rollback
connection.rollback_db_transaction if materialized?
@state.full_rollback!
@instrumenter.finish
@instrumenter.finish(:rollback)
end

def commit
connection.commit_db_transaction if materialized?
@state.full_commit!
@instrumenter.finish
@instrumenter.finish(:commit)
end
end

Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/transaction_instrumentation_test.rb
Expand Up @@ -13,6 +13,7 @@ def test_transaction_instrumentation_on_commit
notified = false
subscriber = ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
assert event.payload[:connection]
assert_equal :commit, event.payload[:outcome]
notified = true
end

Expand All @@ -31,6 +32,7 @@ def test_transaction_instrumentation_on_rollback
notified = false
subscriber = ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
assert event.payload[:connection]
assert_equal :rollback, event.payload[:outcome]
notified = true
end

Expand Down Expand Up @@ -60,6 +62,9 @@ def test_transaction_instrumentation_with_savepoints
end

assert_equal 2, events.count
savepoint, real = events
assert_equal :commit, savepoint.payload[:outcome]
assert_equal :commit, real.payload[:outcome]
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
Expand Down Expand Up @@ -100,6 +105,9 @@ def test_transaction_instrumentation_with_restart_parent_transaction_on_rollback
end

assert_equal 2, events.count
restart, real = events
assert_equal :restart, restart.payload[:outcome]
assert_equal :rollback, real.payload[:outcome]
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
Expand Down Expand Up @@ -140,6 +148,10 @@ def test_transaction_instrumentation_with_restart_savepoint_parent_transactions
end

assert_equal 3, events.count
restart, savepoint, real = events
assert_equal :restart, restart.payload[:outcome]
assert_equal :commit, savepoint.payload[:outcome]
assert_equal :commit, real.payload[:outcome]
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
Expand Down Expand Up @@ -239,6 +251,7 @@ def test_transaction_instrumentation_on_failed_rollback

notified = false
subscriber = ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
assert_equal :incomplete, event.payload[:outcome]
notified = true
end

Expand Down

0 comments on commit 9e34e3b

Please sign in to comment.