Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c05610c3
Commit
c05610c3
authored
Mar 09, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unify transactions in active_record subscriber
Issue
https://gitlab.com/gitlab-org/gitlab/-/issues/323163
parent
cfbc15af
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
22 deletions
+19
-22
lib/gitlab/metrics/background_transaction.rb
lib/gitlab/metrics/background_transaction.rb
+2
-4
lib/gitlab/metrics/subscribers/active_record.rb
lib/gitlab/metrics/subscribers/active_record.rb
+4
-12
spec/lib/gitlab/database_spec.rb
spec/lib/gitlab/database_spec.rb
+1
-1
spec/support/shared_examples/metrics/active_record_subscriber_shared_examples.rb
...mples/metrics/active_record_subscriber_shared_examples.rb
+12
-5
No files found.
lib/gitlab/metrics/background_transaction.rb
View file @
c05610c3
...
...
@@ -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
)
}
...
...
lib/gitlab/metrics/subscribers/active_record.rb
View file @
c05610c3
...
...
@@ -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
...
...
spec/lib/gitlab/database_spec.rb
View file @
c05610c3
...
...
@@ -459,7 +459,7 @@ RSpec.describe Gitlab::Database do
events
end
context
'without a tran
as
tion block'
do
context
'without a tran
sac
tion block'
do
it
'does not publish a transaction event'
do
events
=
subscribe_events
do
User
.
first
...
...
spec/support/shared_examples/metrics/active_record_subscriber_shared_examples.rb
View file @
c05610c3
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment