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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
b503e6ff
Commit
b503e6ff
authored
Dec 08, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement simple in memory cache that expires after 5 minutes
parent
53dc9e83
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
13 deletions
+43
-13
lib/gitlab/metrics/method_call.rb
lib/gitlab/metrics/method_call.rb
+19
-7
spec/lib/gitlab/metrics/method_call_spec.rb
spec/lib/gitlab/metrics/method_call_spec.rb
+24
-6
No files found.
lib/gitlab/metrics/method_call.rb
View file @
b503e6ff
...
...
@@ -18,6 +18,24 @@ module Gitlab
end
end
def
self
.
call_measurement_enabled?
return
@call_measurement_enabled
unless
call_measurement_enabled_cache_expired?
MUTEX
.
synchronize
do
return
@call_measurement_enabled
unless
call_measurement_enabled_cache_expired?
@call_measurement_enabled_cache_expires_at
=
Time
.
now
+
5
.
minutes
@call_measurement_enabled
=
Feature
.
get
(
:prometheus_metrics_method_instrumentation
).
enabled?
end
end
def
self
.
call_measurement_enabled_cache_expired?
@call_measurement_enabled
.
nil?
||
@call_measurement_enabled_cache_expires_at
.
nil?
||
@call_measurement_enabled_cache_expires_at
<
Time
.
now
end
def
self
.
call_measurement_enabled_cache_expire
@call_measurement_enabled
=
nil
@call_measurement_enabled_cache_expires_at
=
nil
end
# name - The full name of the method (including namespace) such as
# `User#sign_in`.
#
...
...
@@ -45,7 +63,7 @@ module Gitlab
@cpu_time
+=
cpu_time
@call_count
+=
1
if
call_measurement_enabled?
&&
above_threshold?
if
self
.
class
.
call_measurement_enabled?
&&
above_threshold?
self
.
class
.
call_duration_histogram
.
observe
(
@transaction
.
labels
.
merge
(
labels
),
real_time
/
1000.0
)
end
...
...
@@ -70,12 +88,6 @@ module Gitlab
def
above_threshold?
real_time
>=
Metrics
.
method_call_threshold
end
def
call_measurement_enabled?
Rails
.
cache
.
fetch
(
:prometheus_metrics_method_instrumentation_enabled
,
expires_in:
5
.
minutes
)
do
Feature
.
get
(
:prometheus_metrics_method_instrumentation
).
enabled?
end
end
end
end
end
spec/lib/gitlab/metrics/method_call_spec.rb
View file @
b503e6ff
...
...
@@ -20,18 +20,35 @@ describe Gitlab::Metrics::MethodCall do
context
'prometheus instrumentation is enabled'
do
before
do
allow
(
Feature
.
get
(
:prometheus_metrics_method_instrumentation
)).
to
receive
(
:enabled?
).
and_call_original
described_class
.
call_measurement_enabled_cache_expire
Feature
.
get
(
:prometheus_metrics_method_instrumentation
).
enable
end
it
'feature check is cached for 5 minutes'
do
allow
(
Feature
.
get
(
:prometheus_metrics_method_instrumentation
)).
to
receive
(
:enabled?
).
and_call_original
allow
(
Rails
.
cache
).
to
receive
(
:fetch
).
and_call_original
around
do
|
example
|
Timecop
.
freeze
do
example
.
run
end
end
method_call
.
measure
{
'foo'
}
method_call
.
measure
{
'foo'
}
it
'caches subsequent invocations of feature check'
do
10
.
times
do
method_call
.
measure
{
'foo'
}
end
expect
(
Feature
.
get
(
:prometheus_metrics_method_instrumentation
)).
to
have_received
(
:enabled?
).
once
end
it
'expires feature check cache after 5 minutes'
do
10
.
times
do
method_call
.
measure
{
'foo'
}
end
Timecop
.
travel
(
Time
.
now
+
5
.
minutes
)
do
method_call
.
measure
{
'foo'
}
end
expect
(
Feature
.
get
(
:prometheus_metrics_method_instrumentation
)).
to
have_received
(
:enabled?
).
twice
expect
(
Rails
.
cache
).
to
have_received
(
:fetch
).
with
(
:prometheus_metrics_method_instrumentation_enabled
,
expires_in:
5
.
minutes
).
twice
end
it
'observes the performance of the supplied block'
do
...
...
@@ -45,6 +62,7 @@ describe Gitlab::Metrics::MethodCall do
context
'prometheus instrumentation is disabled'
do
before
do
described_class
.
call_measurement_enabled_cache_expire
Feature
.
get
(
:prometheus_metrics_method_instrumentation
).
disable
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