Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
b503e6ff
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b503e6ff
编写于
12月 08, 2017
作者:
P
Pawel Chojnacki
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement simple in memory cache that expires after 5 minutes
上级
53dc9e83
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
13 deletion
+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
未找到文件。
lib/gitlab/metrics/method_call.rb
浏览文件 @
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
浏览文件 @
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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录