Skip to content

Commit 243b9d0

Browse files
committed
Report isolation level on ActiveRecord transaction
1 parent 743f7d4 commit 243b9d0

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/transactions_class_methods.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ class << base
1818

1919
# Contains ActiveRecord::Transactions::ClassMethods to be patched
2020
module ClassMethods
21-
def transaction(...)
22-
tracer.in_span('ActiveRecord.transaction', attributes: { 'code.namespace' => name }) do
23-
super
21+
def transaction(*args, **kwargs, &block)
22+
attributes = { 'code.namespace' => name }
23+
if kwargs[:isolation]
24+
attributes['db.transaction_isolation'] = kwargs[:isolation].to_s
25+
end
26+
tracer.in_span('ActiveRecord.transaction', attributes: attributes) do
27+
super(*args, **kwargs, &block)
2428
end
2529
end
2630

instrumentation/active_record/test/instrumentation/active_record/patches/transactions_class_methods_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,13 @@ def self.name
4848
transaction_span = spans.find { |s| s.attributes['code.namespace'] == 'ActiveRecord::Base' }
4949
_(transaction_span).wont_be_nil
5050
end
51+
52+
it 'records transaction isolation level' do
53+
ActiveRecord::Base.transaction(isolation: :read_uncommitted) { User.create! }
54+
55+
transaction_span = spans.find { |s| s.attributes['code.namespace'] == 'ActiveRecord::Base' }
56+
_(transaction_span).wont_be_nil
57+
_(transaction_span.attributes['db.transaction.isolation']).must_equal 'read_uncommitted'
58+
end
5159
end
5260
end

instrumentation/active_record/test/test_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
ActiveRecord::Base.establish_connection(
3434
adapter: 'sqlite3',
35-
database: 'db/development.sqlite3'
35+
database: 'db/development.sqlite3',
36+
# allow to manipulate the transaction isolation level
37+
flags: ::SQLite3::Constants::Open::READWRITE | SQLite3::Constants::Open::CREATE | ::SQLite3::Constants::Open::SHAREDCACHE
3638
)
3739

3840
# Create ActiveRecord models

0 commit comments

Comments
 (0)