Commit 99a392f1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Include subtransactions created by ActiveRecord in counters

This commit includes subtransactions created directly by ActiveRecord in
our Prometheus counter to detect subtransactions generated by migrations
too.
parent 3177ec13
...@@ -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
......
...@@ -210,10 +210,19 @@ module Gitlab ...@@ -210,10 +210,19 @@ module Gitlab
# A monkeypatch over ActiveRecord::Base.transaction. # A monkeypatch over ActiveRecord::Base.transaction.
# It provides observability into transactional methods. # It provides observability into transactional methods.
def transaction(**options, &block) def transaction(**options, &block)
if options[:requires_new] && gitlab_track_subtransactions?
::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
end end
def gitlab_track_subtransactions?
::Feature.enabled?(:active_record_subtransactions_counter, type: :ops, default_enabled: :yaml) &&
connection.transaction_open?
end
end end
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