Commit c05610c3 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Unify transactions in active_record subscriber

Issue https://gitlab.com/gitlab-org/gitlab/-/issues/323163
parent cfbc15af
......@@ -3,7 +3,7 @@
module Gitlab
module Metrics
class BackgroundTransaction < Transaction
# Separate web transaction instance and web transaction instance
# Separate web transaction instance and background transaction instance
BACKGROUND_THREAD_KEY = :_gitlab_metrics_background_transaction
BACKGROUND_BASE_LABEL_KEYS = %i(endpoint_id feature_category).freeze
......@@ -33,9 +33,7 @@ module Gitlab
end
def labels
return @labels if @labels
@labels = {
@labels ||= {
endpoint_id: current_context&.get_attribute(:caller_id),
feature_category: current_context&.get_attribute(:feature_category)
}
......
......@@ -61,27 +61,19 @@ module Gitlab
end
def increment(counter)
web_transaction&.increment("gitlab_transaction_#{counter}_total".to_sym, 1)
background_transaction&.increment("gitlab_transaction_#{counter}_total".to_sym, 1)
current_transaction&.increment("gitlab_transaction_#{counter}_total".to_sym, 1)
Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1
end
def observe(histogram, event)
web_transaction&.observe(histogram, event.duration / 1000.0) do
current_transaction&.observe(histogram, event.duration / 1000.0) do
buckets DURATION_BUCKET
end
background_transaction&.observe(histogram, event.duration / 1000.0) do
buckets DURATION_BUCKET
end
end
def web_transaction
::Gitlab::Metrics::WebTransaction.current
end
def background_transaction
::Gitlab::Metrics::BackgroundTransaction.current
def current_transaction
::Gitlab::Metrics::WebTransaction.current || ::Gitlab::Metrics::BackgroundTransaction.current
end
end
end
......
......@@ -459,7 +459,7 @@ RSpec.describe Gitlab::Database do
events
end
context 'without a tranastion block' do
context 'without a transaction block' do
it 'does not publish a transaction event' do
events = subscribe_events do
User.first
......
......@@ -83,16 +83,23 @@ end
RSpec.shared_examples 'record ActiveRecord metrics' do |db_role|
context 'when both web and background transaction are available' do
let(:transaction) { double('Gitlab::Metrics::WebTransaction') }
let(:background_transaction) { double('Gitlab::Metrics::WebTransaction') }
before do
allow(::Gitlab::Metrics::WebTransaction).to receive(:current)
.and_return(nil)
.and_return(transaction)
allow(::Gitlab::Metrics::BackgroundTransaction).to receive(:current)
.and_return(nil)
.and_return(background_transaction)
allow(transaction).to receive(:increment)
allow(transaction).to receive(:observe)
end
it 'does not capture the metrics' do
expect_any_instance_of(Gitlab::Metrics::Transaction).not_to receive(:increment) # rubocop:disable RSpec/AnyInstanceOf
expect_any_instance_of(Gitlab::Metrics::Transaction).not_to receive(:observe) # rubocop:disable RSpec/AnyInstanceOf
it_behaves_like 'record ActiveRecord metrics in a metrics transaction', db_role
it 'captures the metrics for web only' do
expect(background_transaction).not_to receive(:observe)
expect(background_transaction).not_to receive(:increment)
subscriber.sql(event)
end
......
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