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
ae5268ce
Commit
ae5268ce
authored
Jun 05, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Additional Metrics tests
parent
6a70509a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
126 additions
and
18 deletions
+126
-18
config/additional_metrics.yml
config/additional_metrics.yml
+0
-1
lib/gitlab/prometheus/queries/additional_metrics_query.rb
lib/gitlab/prometheus/queries/additional_metrics_query.rb
+22
-13
spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb
...itlab/prometheus/queries/additional_metrics_query_spec.rb
+49
-0
spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
...b/gitlab/prometheus/queries/matched_metrics_query_spec.rb
+1
-1
spec/support/prometheus/additional_metrics_query_helper.rb
spec/support/prometheus/additional_metrics_query_helper.rb
+51
-0
spec/support/prometheus/matched_metrics_query_helper.rb
spec/support/prometheus/matched_metrics_query_helper.rb
+3
-3
No files found.
config/additional_metrics.yml
View file @
ae5268ce
...
@@ -9,7 +9,6 @@
...
@@ -9,7 +9,6 @@
queries
:
queries
:
-
query_range
:
'
avg(container_memory_usage_bytes{%{environment_filter}})
/
2^20'
-
query_range
:
'
avg(container_memory_usage_bytes{%{environment_filter}})
/
2^20'
label
:
Container memory
label
:
Container memory
display_empty
:
true
unit
:
MiB
unit
:
MiB
-
title
:
"
Current
memory
usage"
-
title
:
"
Current
memory
usage"
required_metrics
:
required_metrics
:
...
...
lib/gitlab/prometheus/queries/additional_metrics_query.rb
View file @
ae5268ce
...
@@ -17,26 +17,42 @@ module Gitlab::Prometheus::Queries
...
@@ -17,26 +17,42 @@ module Gitlab::Prometheus::Queries
def
query_metrics
(
query_context
)
def
query_metrics
(
query_context
)
query_processor
=
method
(
:process_query
).
curry
[
query_context
]
query_processor
=
method
(
:process_query
).
curry
[
query_context
]
matched_metrics
.
map
do
|
group
|
groups
=
matched_metrics
.
map
do
|
group
|
metrics
=
group
.
metrics
.
map
do
|
metric
|
metrics
=
group
.
metrics
.
map
do
|
metric
|
{
{
title:
metric
.
title
,
title:
metric
.
title
,
weight:
metric
.
weight
,
weight:
metric
.
weight
,
y_label:
metric
.
y_label
,
y_label:
metric
.
y_label
,
queries:
metric
.
queries
.
map
(
&
query_processor
)
queries:
metric
.
queries
.
map
(
&
query_processor
)
.
select
(
&
method
(
:query_with_result
))
}
}
end
end
{
{
group:
group
.
name
,
group:
group
.
name
,
priority:
group
.
priority
,
priority:
group
.
priority
,
metrics:
metrics
metrics:
metrics
.
select
(
&
method
(
:metric_with_any_queries
))
}
}
end
end
groups
.
select
(
&
method
(
:group_with_any_metrics
))
end
end
private
private
def
metric_with_any_queries
(
metric
)
metric
[
:queries
]
&
.
count
>
0
end
def
group_with_any_metrics
(
group
)
group
[
:metrics
]
&
.
count
>
0
end
def
query_with_result
(
query
)
query
[
:result
]
&
.
any?
do
|
item
|
item
&
.
[
](
:values
)
&
.
any?
||
item
&
.
[
](
:value
)
&
.
any?
end
end
def
process_query
(
context
,
query
)
def
process_query
(
context
,
query
)
query_with_result
=
query
.
dup
query_with_result
=
query
.
dup
query_with_result
[
:result
]
=
query_with_result
[
:result
]
=
...
@@ -48,22 +64,15 @@ module Gitlab::Prometheus::Queries
...
@@ -48,22 +64,15 @@ module Gitlab::Prometheus::Queries
query_with_result
query_with_result
end
end
def
process_result
(
query_result
)
contains_metrics
=
query_result
.
all?
do
|
item
|
item
&
.
[
](
:values
)
&
.
any?
||
item
&
.
[
](
:value
)
&
.
any?
end
contains_metrics
def
available_metrics
@available_metrics
||=
client_label_values
||
[]
end
end
def
matched_metrics
def
matched_metrics
label_values
=
client_label_values
||
[]
Gitlab
::
Prometheus
::
MetricGroup
.
all
result
=
Gitlab
::
Prometheus
::
MetricGroup
.
all
.
map
do
|
group
|
result
=
Gitlab
::
Prometheus
::
MetricGroup
.
all
.
map
do
|
group
|
group
.
metrics
.
select!
do
|
metric
|
group
.
metrics
.
select!
do
|
metric
|
matcher
=
Regexp
.
compile
(
metric
.
detect
)
metric
.
required_metrics
.
all?
(
&
available_metrics
.
method
(
:include?
))
label_values
.
any?
&
matcher
.
method
(
:match
)
end
end
group
group
end
end
...
...
spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb
0 → 100644
View file @
ae5268ce
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsQuery
,
lib:
true
do
include
Prometheus
::
AdditionalMetricsQueryHelper
let
(
:metric_group_class
)
{
Gitlab
::
Prometheus
::
MetricGroup
}
let
(
:metric_class
)
{
Gitlab
::
Prometheus
::
Metric
}
let
(
:client
)
{
double
(
'prometheus_client'
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
subject
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
environment
.
id
)
}
context
'with one group where two metrics is found'
do
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
])
allow
(
client
).
to
receive
(
:label_values
).
and_return
(
metric_names
)
end
context
'some querie return results'
do
before
do
expect
(
client
).
to
receive
(
:query_range
).
with
(
'query_range_a'
,
any_args
).
and_return
(
query_range_result
)
expect
(
client
).
to
receive
(
:query_range
).
with
(
'query_range_b'
,
any_args
).
and_return
(
query_range_result
)
expect
(
client
).
to
receive
(
:query_range
).
with
(
'query_range_empty'
,
any_args
).
and_return
([])
end
it
'return results only for queries with results'
do
puts
query_result
expected
=
{
group:
'name'
,
priority:
1
,
metrics:
[
{
title:
'title'
,
weight:
nil
,
y_label:
'Values'
,
queries:
[
{
query_range:
'query_range_a'
,
result:
query_range_result
},
{
query_range:
'query_range_b'
,
label:
'label'
,
unit:
'unit'
,
result:
query_range_result
}
]
}
]
}
expect
(
query_result
).
to
eq
([
expected
])
end
end
end
end
spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
View file @
ae5268ce
...
@@ -10,7 +10,7 @@ describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do
...
@@ -10,7 +10,7 @@ describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do
subject
{
described_class
.
new
(
client
)
}
subject
{
described_class
.
new
(
client
)
}
context
'with one group where two metrics
are
found'
do
context
'with one group where two metrics
is
found'
do
before
do
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
])
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
])
allow
(
client
).
to
receive
(
:label_values
).
and_return
(
metric_names
)
allow
(
client
).
to
receive
(
:label_values
).
and_return
(
metric_names
)
...
...
spec/support/prometheus/additional_metrics_query_helper.rb
0 → 100644
View file @
ae5268ce
module
Prometheus
module
AdditionalMetricsQueryHelper
def
metric_names
%w{metric_a metric_b}
end
def
simple_queries
[{
query_range:
'query_range_a'
},
{
query_range:
'query_range_b'
,
label:
'label'
,
unit:
'unit'
}]
end
def
simple_query
(
suffix
=
'a'
)
[{
query_range:
"query_range_
#{
suffix
}
"
}]
end
def
simple_metrics
[
Gitlab
::
Prometheus
::
Metric
.
new
(
'title'
,
%w(metric_a metric_b)
,
nil
,
nil
,
simple_queries
),
Gitlab
::
Prometheus
::
Metric
.
new
(
'title'
,
%w{metric_a}
,
nil
,
nil
,
simple_query
(
'empty'
)),
Gitlab
::
Prometheus
::
Metric
.
new
(
'title'
,
%w{metric_c}
,
nil
,
nil
)
]
end
def
simple_metric_group
(
name
=
'name'
,
metrics
=
simple_metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name
,
1
,
metrics
)
end
def
query_result
[
{
'metric'
:
{},
'value'
:
[
1488772511.004
,
'0.000041021495238095323'
]
}
]
end
def
query_range_result
[
{
'metric'
:
{},
'values'
:
[
[
1488758662.506
,
'0.00002996364761904785'
],
[
1488758722.506
,
'0.00003090239047619091'
]
]
}
]
end
end
end
spec/support/prometheus/matched_metrics_query_helper.rb
View file @
ae5268ce
...
@@ -5,12 +5,12 @@ module Prometheus
...
@@ -5,12 +5,12 @@ module Prometheus
end
end
def
simple_metrics
(
metric_name
=
'metric_a'
)
def
simple_metrics
(
metric_name
=
'metric_a'
)
[
metric_class
.
new
(
'title'
,
%W(
#{
metric_name
}
metric_b)
,
nil
,
nil
),
[
Gitlab
::
Prometheus
::
Metric
.
new
(
'title'
,
%W(
#{
metric_name
}
metric_b)
,
nil
,
nil
),
metric_class
.
new
(
'title'
,
[
metric_name
],
nil
,
nil
)]
Gitlab
::
Prometheus
::
Metric
.
new
(
'title'
,
[
metric_name
],
nil
,
nil
)]
end
end
def
simple_metric_group
(
name
=
'name'
,
metrics
=
simple_metrics
)
def
simple_metric_group
(
name
=
'name'
,
metrics
=
simple_metrics
)
metric_group_class
.
new
(
name
,
1
,
metrics
)
Gitlab
::
Prometheus
::
MetricGroup
.
new
(
name
,
1
,
metrics
)
end
end
def
series_info_with_environment
(
*
more_metrics
)
def
series_info_with_environment
(
*
more_metrics
)
...
...
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