diff --git a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/transactions_class_methods.rb b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/transactions_class_methods.rb index bd6e0b5b64..90036db0e1 100644 --- a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/transactions_class_methods.rb +++ b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/transactions_class_methods.rb @@ -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 } + if kwargs[:isolation] + attributes['db.transaction_isolation'] = kwargs[:isolation].to_s + end + tracer.in_span('ActiveRecord.transaction', attributes: attributes) do + super(*args, **kwargs, &block) end end diff --git a/instrumentation/active_record/test/instrumentation/active_record/patches/transactions_class_methods_test.rb b/instrumentation/active_record/test/instrumentation/active_record/patches/transactions_class_methods_test.rb index 8195934912..b8918b857d 100644 --- a/instrumentation/active_record/test/instrumentation/active_record/patches/transactions_class_methods_test.rb +++ b/instrumentation/active_record/test/instrumentation/active_record/patches/transactions_class_methods_test.rb @@ -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 diff --git a/instrumentation/active_record/test/test_helper.rb b/instrumentation/active_record/test/test_helper.rb index 3138f52d8f..d791d56d90 100644 --- a/instrumentation/active_record/test/test_helper.rb +++ b/instrumentation/active_record/test/test_helper.rb @@ -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