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
Léo-Paul Géneau
gitlab-ce
Commits
b97d5b65
Commit
b97d5b65
authored
Jun 16, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use include ActiveModel::Model to hold metrics data parsed from yaml.
parent
0e7e7c2f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
43 deletions
+32
-43
lib/gitlab/prometheus/additional_metrics_parser.rb
lib/gitlab/prometheus/additional_metrics_parser.rb
+7
-16
lib/gitlab/prometheus/metric.rb
lib/gitlab/prometheus/metric.rb
+8
-7
lib/gitlab/prometheus/metric_group.rb
lib/gitlab/prometheus/metric_group.rb
+3
-7
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
+12
-11
spec/support/prometheus/metric_builders.rb
spec/support/prometheus/metric_builders.rb
+2
-2
No files found.
lib/gitlab/prometheus/additional_metrics_parser.rb
View file @
b97d5b65
...
@@ -9,26 +9,17 @@ module Gitlab
...
@@ -9,26 +9,17 @@ module Gitlab
private
private
def
metrics_from_list
(
list
)
def
validate!
(
obj
)
list
.
map
{
|
entry
|
metric_from_entry
(
entry
)
}
raise
ParsingError
.
new
(
obj
.
errors
.
full_messages
.
join
(
'\n'
))
unless
obj
.
valid?
end
def
metric_from_entry
(
entry
)
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
end
def
group_from_entry
(
entry
)
def
group_from_entry
(
entry
)
required_fields
=
[
:group
,
:priority
,
:metrics
]
entry
[
:name
]
=
entry
.
delete
(
:group
)
missing_fields
=
required_fields
.
select
{
|
key
|
entry
[
key
].
nil?
}
entry
[
:metrics
]
&
.
map!
do
|
entry
|
Metric
.
new
(
entry
).
tap
(
&
method
(
:validate!
))
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
.
map
(
&
:to_s
)
}
"
)
unless
missing_fields
.
empty?
end
group
=
MetricGroup
.
new
(
entry
[
:group
],
entry
[
:priority
])
MetricGroup
.
new
(
entry
).
tap
(
&
method
(
:validate!
))
group
.
tap
{
|
g
|
g
.
metrics
=
metrics_from_list
(
entry
[
:metrics
])
}
end
end
def
additional_metrics_raw
def
additional_metrics_raw
...
...
lib/gitlab/prometheus/metric.rb
View file @
b97d5b65
module
Gitlab
module
Gitlab
module
Prometheus
module
Prometheus
class
Metric
class
Metric
attr_reader
:group
,
:title
,
:required_metrics
,
:weight
,
:y_label
,
:queries
include
ActiveModel
::
Model
def
initialize
(
title
,
required_metrics
,
weight
,
y_label
,
queries
=
[])
attr_accessor
:title
,
:required_metrics
,
:weight
,
:y_label
,
:queries
@title
=
title
@required_metrics
=
required_metrics
validates
:title
,
:required_metrics
,
:weight
,
:y_label
,
:queries
,
presence:
true
@weight
=
weight
@y_label
=
y_label
||
'Values'
def
initialize
(
params
=
{})
@queries
=
queries
super
(
params
)
@y_label
||=
'Values'
end
end
end
end
end
end
...
...
lib/gitlab/prometheus/metric_group.rb
View file @
b97d5b65
module
Gitlab
module
Gitlab
module
Prometheus
module
Prometheus
class
MetricGroup
class
MetricGroup
attr_reader
:priority
,
:name
include
ActiveModel
::
Model
attr_accessor
:metrics
def
initialize
(
name
,
priority
,
metrics
=
[])
attr_accessor
:name
,
:priority
,
:metrics
@name
=
name
validates
:name
,
:priority
,
:metrics
,
presence:
true
@priority
=
priority
@metrics
=
metrics
end
def
self
.
all
def
self
.
all
AdditionalMetricsParser
.
load_groups_from_yaml
AdditionalMetricsParser
.
load_groups_from_yaml
...
...
spec/lib/gitlab/prometheus/additional_metrics_parser_spec.rb
View file @
b97d5b65
...
@@ -26,7 +26,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -26,7 +26,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
priority: 1
priority: 1
metrics:
metrics:
- title: title
- title: title
required_metrics: []
required_metrics: [
'metric_a'
]
weight: 1
weight: 1
queries: [{query_range: query_range_a}]
queries: [{query_range: query_range_a}]
EOF
EOF
...
@@ -54,7 +54,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -54,7 +54,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
expect
(
metrics
.
count
).
to
eq
(
3
)
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
[
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
[
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
)
expect
(
metrics
[
2
]).
to
have_attributes
(
title:
'title'
,
required_metrics:
%w{metric_a}
,
weight:
1
)
end
end
it
'provides query data'
do
it
'provides query data'
do
...
@@ -78,7 +78,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -78,7 +78,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
it
'throws parsing error'
do
it
'throws parsing error'
do
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/
missing.*
#{
field_name
}
/
)
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/
#{
field_name
}
can't be blank/i
)
end
end
end
end
...
@@ -88,13 +88,13 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -88,13 +88,13 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
it
'throws parsing error'
do
it
'throws parsing error'
do
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/
missing.*
#{
field_name
}
/
)
expect
{
subject
}.
to
raise_error
(
parser_error_class
,
/
#{
field_name
}
can't be blank/i
)
end
end
end
end
end
end
describe
'group required fields'
do
describe
'group required fields'
do
it_behaves_like
'required field'
,
:metrics
do
it_behaves_like
'required field'
,
'metrics'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- group: group_a
- group: group_a
...
@@ -111,10 +111,11 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -111,10 +111,11 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
end
end
it_behaves_like
'required field'
,
:group
do
it_behaves_like
'required field'
,
'name'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- priority: 1
- group:
priority: 1
metrics: []
metrics: []
EOF
EOF
end
end
...
@@ -127,7 +128,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -127,7 +128,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
end
end
it_behaves_like
'required field'
,
:priority
do
it_behaves_like
'required field'
,
'priority'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- group: group_a
- group: group_a
...
@@ -146,7 +147,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -146,7 +147,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
describe
'metrics fields parsing'
do
describe
'metrics fields parsing'
do
it_behaves_like
'required field'
,
:title
do
it_behaves_like
'required field'
,
'title'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- group: group_a
- group: group_a
...
@@ -171,7 +172,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -171,7 +172,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
end
end
it_behaves_like
'required field'
,
:required_metrics
do
it_behaves_like
'required field'
,
'required metrics'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- group: group_a
- group: group_a
...
@@ -196,7 +197,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
...
@@ -196,7 +197,7 @@ describe Gitlab::Prometheus::AdditionalMetricsParser, lib: true do
end
end
end
end
it_behaves_like
'required field'
,
:weight
do
it_behaves_like
'required field'
,
'weight'
do
let
(
:field_nil
)
do
let
(
:field_nil
)
do
<<-
EOF
.
strip_heredoc
<<-
EOF
.
strip_heredoc
- group: group_a
- group: group_a
...
...
spec/support/prometheus/metric_builders.rb
View file @
b97d5b65
...
@@ -9,7 +9,7 @@ module Prometheus
...
@@ -9,7 +9,7 @@ module Prometheus
end
end
def
simple_metric
(
title:
'title'
,
required_metrics:
[],
queries:
[
simple_query
])
def
simple_metric
(
title:
'title'
,
required_metrics:
[],
queries:
[
simple_query
])
Gitlab
::
Prometheus
::
Metric
.
new
(
title
,
required_metrics
,
1
,
nil
,
queries
)
Gitlab
::
Prometheus
::
Metric
.
new
(
title
:
title
,
required_metrics:
required_metrics
,
weight:
1
,
queries:
queries
)
end
end
def
simple_metrics
(
added_metric_name:
'metric_a'
)
def
simple_metrics
(
added_metric_name:
'metric_a'
)
...
@@ -21,7 +21,7 @@ module Prometheus
...
@@ -21,7 +21,7 @@ module Prometheus
end
end
def
simple_metric_group
(
name:
'name'
,
metrics:
simple_metrics
)
def
simple_metric_group
(
name:
'name'
,
metrics:
simple_metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name
,
1
,
metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name:
name
,
priority:
1
,
metrics:
metrics
)
end
end
end
end
end
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