Commit 3e840e6e authored by Stan Hu's avatar Stan Hu

Merge branch 'feature/gb/active-record-subtransactions-counter' into 'master'

Include subtransactions created by ActiveRecord::Base in counters

See merge request gitlab-org/gitlab!68764
parents ca7a50bc 296f7f65
...@@ -104,19 +104,6 @@ class ApplicationRecord < ActiveRecord::Base ...@@ -104,19 +104,6 @@ class ApplicationRecord < ActiveRecord::Base
enum(enum_mod.key => values) enum(enum_mod.key => values)
end end
def self.transaction(**options, &block)
if options[:requires_new] && track_subtransactions?
::Gitlab::Database::Metrics.subtransactions_increment(self.name)
end
super(**options, &block)
end
def self.track_subtransactions?
::Feature.enabled?(:active_record_subtransactions_counter, type: :ops, default_enabled: :yaml) &&
connection.transaction_open?
end
def self.cached_column_list def self.cached_column_list
self.column_names.map { |column_name| self.arel_table[column_name] } self.column_names.map { |column_name| self.arel_table[column_name] }
end end
......
---
name: active_record_subtransactions_counter
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/66477
rollout_issue_url:
milestone: '14.1'
type: ops
group: group::pipeline execution
default_enabled: false
...@@ -214,9 +214,13 @@ module Gitlab ...@@ -214,9 +214,13 @@ module Gitlab
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do class_methods do
# A monkeypatch over ActiveRecord::Base.transaction. # A patch over ActiveRecord::Base.transaction that provides
# It provides observability into transactional methods. # observability into transactional methods.
def transaction(**options, &block) def transaction(**options, &block)
if options[:requires_new] && connection.transaction_open?
::Gitlab::Database::Metrics.subtransactions_increment(self.name)
end
ActiveSupport::Notifications.instrument('transaction.active_record', { connection: connection }) do ActiveSupport::Notifications.instrument('transaction.active_record', { connection: connection }) do
super(**options, &block) super(**options, &block)
end end
......
...@@ -164,6 +164,23 @@ RSpec.describe ApplicationRecord do ...@@ -164,6 +164,23 @@ RSpec.describe ApplicationRecord do
end end
end end
end end
# rubocop:disable Database/MultipleDatabases
it 'increments a counter when a transaction is created in ActiveRecord' do
expect(described_class.connection.transaction_open?).to be false
expect(::Gitlab::Database::Metrics)
.to receive(:subtransactions_increment)
.with('ActiveRecord::Base')
.once
ActiveRecord::Base.transaction do
ActiveRecord::Base.transaction(requires_new: true) do
expect(ActiveRecord::Base.connection.transaction_open?).to be true
end
end
end
# rubocop:enable Database/MultipleDatabases
end end
describe '.with_fast_read_statement_timeout' do describe '.with_fast_read_statement_timeout' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment