Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
0e7e7c2f
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,发现更多精彩内容 >>
提交
0e7e7c2f
编写于
6月 09, 2017
作者:
P
Pawel Chojnacki
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test Additional metrics parser and fix query checking tests
上级
f78fd3de
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
265 addition
and
11 deletion
+265
-11
app/models/project_services/prometheus_service.rb
app/models/project_services/prometheus_service.rb
+0
-4
lib/gitlab/prometheus/additional_metrics_parser.rb
lib/gitlab/prometheus/additional_metrics_parser.rb
+11
-4
lib/gitlab/prometheus/queries/query_additional_metrics.rb
lib/gitlab/prometheus/queries/query_additional_metrics.rb
+3
-2
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
+250
-0
spec/support/prometheus/metric_builders.rb
spec/support/prometheus/metric_builders.rb
+1
-1
未找到文件。
app/models/project_services/prometheus_service.rb
浏览文件 @
0e7e7c2f
...
...
@@ -75,10 +75,6 @@ class PrometheusService < MonitoringService
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
end
def
with_reactive_cache
(
*
args
,
&
block
)
yield
calculate_reactive_cache
(
*
args
)
end
# Cache metrics for specific environment
def
calculate_reactive_cache
(
query_class_name
,
*
args
)
return
unless
active?
&&
project
&&
!
project
.
pending_delete?
...
...
lib/gitlab/prometheus/additional_metrics_parser.rb
浏览文件 @
0e7e7c2f
...
...
@@ -14,22 +14,29 @@ module Gitlab
end
def
metric_from_entry
(
entry
)
missing_fields
=
[
:title
,
:required_metrics
,
:weight
,
:queries
].
select
{
|
key
|
!
entry
.
key?
(
key
)
}
required_fields
=
[
:title
,
:required_metrics
,
:weight
,
:queries
]
missing_fields
=
required_fields
.
select
{
|
key
|
entry
[
key
].
nil?
}
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
Metric
.
new
(
entry
[
:title
],
entry
[
:required_metrics
],
entry
[
:weight
],
entry
[
:y_label
],
entry
[
:queries
])
end
def
group_from_entry
(
entry
)
missing_fields
=
[
:group
,
:priority
,
:metrics
].
select
{
|
key
|
!
entry
.
key?
(
key
)
}
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
required_fields
=
[
:group
,
:priority
,
:metrics
]
missing_fields
=
required_fields
.
select
{
|
key
|
entry
[
key
].
nil?
}
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
.
map
(
&
:to_s
)
}
"
)
unless
missing_fields
.
empty?
group
=
MetricGroup
.
new
(
entry
[
:group
],
entry
[
:priority
])
group
.
tap
{
|
g
|
g
.
metrics
=
metrics_from_list
(
entry
[
:metrics
])
}
end
def
additional_metrics_raw
@additional_metrics_raw
||=
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/additional_metrics.yml'
))
&
.
map
(
&
:deep_symbolize_keys
).
freeze
@additional_metrics_raw
||=
load_yaml_file
&
.
map
(
&
:deep_symbolize_keys
).
freeze
end
def
load_yaml_file
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/additional_metrics.yml'
))
end
end
end
...
...
lib/gitlab/prometheus/queries/query_additional_metrics.rb
浏览文件 @
0e7e7c2f
...
...
@@ -37,18 +37,19 @@ module Gitlab
def
query_with_result
(
query
)
query
[
:result
]
&
.
any?
do
|
item
|
item
&
.
[
](
'values'
)
&
.
any?
||
item
&
.
[
](
'value'
)
&
.
any?
item
&
.
[
](
:values
)
&
.
any?
||
item
&
.
[
](
:value
)
&
.
any?
end
end
def
process_query
(
context
,
query
)
query_with_result
=
query
.
dup
query_with_result
[
:result
]
=
result
=
if
query
.
key?
(
:query_range
)
client_query_range
(
query
[
:query_range
]
%
context
,
start:
context
[
:timeframe_start
],
stop:
context
[
:timeframe_end
])
else
client_query
(
query
[
:query
]
%
context
,
time:
context
[
:timeframe_end
])
end
query_with_result
[
:result
]
=
result
&
.
map
(
&
:deep_symbolize_keys
)
query_with_result
end
...
...
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
0 → 100644
浏览文件 @
0e7e7c2f
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
AdditionalMetricsParser
,
lib:
true
do
include
Prometheus
::
MetricBuilders
let
(
:parser_error_class
)
{
Gitlab
::
Prometheus
::
ParsingError
}
describe
'#load_groups_from_yaml'
do
subject
{
described_class
.
load_groups_from_yaml
}
describe
'parsing sample yaml'
do
let
(
:sample_yaml
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: "title"
required_metrics: [ metric_a, metric_b ]
weight: 1
queries: [{ query_range: 'query_range_a', label: label, unit: unit }]
- title: "title"
required_metrics: [metric_a]
weight: 1
queries: [{ query_range: 'query_range_empty' }]
- group: group_b
priority: 1
metrics:
- title: title
required_metrics: []
weight: 1
queries: [{query_range: query_range_a}]
EOF
end
before
do
described_class
.
instance_variable_set
:@additional_metrics_raw
,
nil
allow
(
described_class
).
to
receive
(
:load_yaml_file
)
{
YAML
.
load
(
sample_yaml
)
}
end
it
'parses to two metric groups with 2 and 1 metric respectively'
do
expect
(
subject
.
count
).
to
eq
(
2
)
expect
(
subject
[
0
].
metrics
.
count
).
to
eq
(
2
)
expect
(
subject
[
1
].
metrics
.
count
).
to
eq
(
1
)
end
it
'provide group data'
do
expect
(
subject
[
0
]).
to
have_attributes
(
name:
'group_a'
,
priority:
1
)
expect
(
subject
[
1
]).
to
have_attributes
(
name:
'group_b'
,
priority:
1
)
end
it
'provides metrics data'
do
metrics
=
subject
.
flat_map
(
&
:metrics
)
expect
(
metrics
.
count
).
to
eq
(
3
)
expect
(
metrics
[
0
]).
to
have_attributes
(
title:
'title'
,
required_metrics:
%w(metric_a metric_b)
,
weight:
1
)
expect
(
metrics
[
1
]).
to
have_attributes
(
title:
'title'
,
required_metrics:
%w(metric_a)
,
weight:
1
)
expect
(
metrics
[
2
]).
to
have_attributes
(
title:
'title'
,
required_metrics:
[],
weight:
1
)
end
it
'provides query data'
do
queries
=
subject
.
flat_map
(
&
:metrics
).
flat_map
(
&
:queries
)
expect
(
queries
.
count
).
to
eq
(
3
)
expect
(
queries
[
0
]).
to
eq
(
query_range:
'query_range_a'
,
label:
'label'
,
unit:
'unit'
)
expect
(
queries
[
1
]).
to
eq
(
query_range:
'query_range_empty'
)
expect
(
queries
[
2
]).
to
eq
(
query_range:
'query_range_a'
)
end
end
shared_examples
'required field'
do
|
field_name
|
before
do
described_class
.
instance_variable_set
:@additional_metrics_raw
,
nil
end
context
"when
#{
field_name
}
is nil"
do
before
do
allow
(
described_class
).
to
receive
(
:load_yaml_file
)
{
YAML
.
load
(
field_missing
)
}
end
it
'throws parsing error'
do
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/missing.*
#{
field_name
}
/
)
end
end
context
"when
#{
field_name
}
are not specified"
do
before
do
allow
(
described_class
).
to
receive
(
:load_yaml_file
)
{
YAML
.
load
(
field_nil
)
}
end
it
'throws parsing error'
do
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/missing.*
#{
field_name
}
/
)
end
end
end
describe
'group required fields'
do
it_behaves_like
'required field'
,
:metrics
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
EOF
end
end
it_behaves_like
'required field'
,
:group
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- priority: 1
metrics: []
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- priority: 1
metrics: []
EOF
end
end
it_behaves_like
'required field'
,
:priority
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority:
metrics: []
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
metrics: []
EOF
end
end
end
describe
'metrics fields parsing'
do
it_behaves_like
'required field'
,
:title
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title:
required_metrics: []
weight: 1
queries: []
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- required_metrics: []
weight: 1
queries: []
EOF
end
end
it_behaves_like
'required field'
,
:required_metrics
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
required_metrics:
weight: 1
queries: []
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
weight: 1
queries: []
EOF
end
end
it_behaves_like
'required field'
,
:weight
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
required_metrics: []
weight:
queries: []
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
required_metrics: []
queries: []
EOF
end
end
it_behaves_like
'required field'
,
:queries
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
required_metrics: []
weight: 1
queries:
EOF
end
let
(
:field_missing
)
do
<<-
EOF
.
strip_heredoc
- group: group_a
priority: 1
metrics:
- title: title
required_metrics: []
weight: 1
EOF
end
end
end
end
end
spec/support/prometheus/metric_builders.rb
浏览文件 @
0e7e7c2f
...
...
@@ -21,7 +21,7 @@ module Prometheus
end
def
simple_metric_group
(
name:
'name'
,
metrics:
simple_metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name:
name
,
priority:
1
,
metrics:
metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name
,
1
,
metrics
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录