Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ class << base

# Contains ActiveRecord::Transactions::ClassMethods to be patched
module ClassMethods
def transaction(...)
tracer.in_span('ActiveRecord.transaction', attributes: { 'code.namespace' => name }) do
super
def transaction(*args, **kwargs, &block)
attributes = { 'code.namespace' => name }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

attributes = {
  'code.namespace' => name,
  'db.transaction_isolation' => kwargs[:isolation]
}.compact!

Copy link
Contributor

Choose a reason for hiding this comment

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

@xuan-cao-swi this would potentially set the attributes hash to nil.

https://ruby-doc.org/3.4.1/Hash.html#method-i-compact-21

compact! → self or nil
Returns self with all its nil-valued entries removed (in place):
Returns nil if no entries were removed.

if kwargs[:isolation]
attributes['db.transaction_isolation'] = kwargs[:isolation].to_s
end
tracer.in_span('ActiveRecord.transaction', attributes: attributes) do
super(*args, **kwargs, &block)
Copy link
Contributor

Choose a reason for hiding this comment

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

super is enough

end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ def self.name
transaction_span = spans.find { |s| s.attributes['code.namespace'] == 'ActiveRecord::Base' }
_(transaction_span).wont_be_nil
end

it 'records transaction isolation level' do
ActiveRecord::Base.transaction(isolation: :read_uncommitted) { User.create! }

transaction_span = spans.find { |s| s.attributes['code.namespace'] == 'ActiveRecord::Base' }
_(transaction_span).wont_be_nil
_(transaction_span.attributes['db.transaction.isolation']).must_equal 'read_uncommitted'
end
end
end
4 changes: 3 additions & 1 deletion instrumentation/active_record/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@

ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/development.sqlite3'
database: 'db/development.sqlite3',
# allow to manipulate the transaction isolation level
flags: ::SQLite3::Constants::Open::READWRITE | SQLite3::Constants::Open::CREATE | ::SQLite3::Constants::Open::SHAREDCACHE
)

# Create ActiveRecord models
Expand Down
Loading