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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
e74896df
Commit
e74896df
authored
May 31, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Matched Metrics tests
parent
7e17d763
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
39 deletions
+97
-39
config/additional_metrics.yml
config/additional_metrics.yml
+10
-4
lib/gitlab/prometheus/metric.rb
lib/gitlab/prometheus/metric.rb
+8
-13
lib/gitlab/prometheus/metric_group.rb
lib/gitlab/prometheus/metric_group.rb
+1
-1
lib/gitlab/prometheus/queries/matched_metrics_query.rb
lib/gitlab/prometheus/queries/matched_metrics_query.rb
+28
-21
spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
...b/gitlab/prometheus/queries/matched_metrics_query_spec.rb
+50
-0
No files found.
config/additional_metrics.yml
View file @
e74896df
...
@@ -3,25 +3,31 @@
...
@@ -3,25 +3,31 @@
metrics
:
metrics
:
-
title
:
"
Memory
usage"
-
title
:
"
Memory
usage"
y_label
:
"
Values"
y_label
:
"
Values"
detect
:
container_memory_usage_bytes
required_metrics
:
-
container_memory_usage_bytes
weight
:
1
weight
:
1
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"
detect
:
container_memory_usage_bytes
required_metrics
:
-
container_memory_usage_bytes
weight
:
1
weight
:
1
queries
:
queries
:
-
query
:
'
avg(container_memory_usage_bytes{%{environment_filter}})
/
2^20'
-
query
:
'
avg(container_memory_usage_bytes{%{environment_filter}})
/
2^20'
display_empty
:
false
unit
:
MiB
unit
:
MiB
-
title
:
"
CPU
usage"
-
title
:
"
CPU
usage"
detect
:
container_cpu_usage_seconds_total
required_metrics
:
-
container_cpu_usage_seconds_total
weight
:
1
weight
:
1
queries
:
queries
:
-
query_range
:
'
avg(rate(container_cpu_usage_seconds_total{%{environment_filter}}[2m]))
*
100'
-
query_range
:
'
avg(rate(container_cpu_usage_seconds_total{%{environment_filter}}[2m]))
*
100'
-
title
:
"
Current
CPU
usage"
-
title
:
"
Current
CPU
usage"
detect
:
container_cpu_usage_seconds_total
required_metrics
:
-
container_cpu_usage_seconds_total
weight
:
1
weight
:
1
queries
:
queries
:
-
query
:
'
avg(rate(container_cpu_usage_seconds_total{%{environment_filter}}[2m]))
*
100'
-
query
:
'
avg(rate(container_cpu_usage_seconds_total{%{environment_filter}}[2m]))
*
100'
lib/gitlab/prometheus/metric.rb
View file @
e74896df
module
Gitlab::Prometheus
module
Gitlab::Prometheus
class
Metric
class
Metric
attr_reader
:group
,
:title
,
:
detect
,
:weight
,
:y_label
,
:queries
attr_reader
:group
,
:title
,
:
required_metrics
,
:weight
,
:y_label
,
:queries
def
initialize
(
group
,
title
,
detect
,
weight
,
y_label
,
queries
=
[])
def
initialize
(
title
,
required_metrics
,
weight
,
y_label
,
queries
=
[])
@group
=
group
@title
=
title
@title
=
title
@
detect
=
detect
@
required_metrics
=
required_metrics
@weight
=
weight
@weight
=
weight
@y_label
=
y_label
||
'Values'
@y_label
=
y_label
||
'Values'
@queries
=
queries
@queries
=
queries
end
end
def
self
.
metric_from_entry
(
group
,
entry
)
def
self
.
metric_from_entry
(
entry
)
missing_fields
=
[
:title
,
:
detect
,
:weight
,
:queries
].
select
{
|
key
|
!
entry
.
has_key?
(
key
)
}
missing_fields
=
[
:title
,
:
required_metrics
,
:weight
,
:queries
].
select
{
|
key
|
!
entry
.
has_key?
(
key
)
}
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
Metric
.
new
(
group
,
entry
[
:title
],
entry
[
:detect
],
entry
[
:weight
],
entry
[
:y_label
],
entry
[
:queries
])
Metric
.
new
(
entry
[
:title
],
entry
[
:required_metrics
],
entry
[
:weight
],
entry
[
:y_label
],
entry
[
:queries
])
end
end
def
self
.
metrics_from_list
(
group
,
list
)
def
self
.
metrics_from_list
(
list
)
list
.
map
{
|
entry
|
metric_from_entry
(
group
,
entry
)
}
list
.
map
{
|
entry
|
metric_from_entry
(
entry
)
}
end
def
self
.
additional_metrics_raw
@additional_metrics_raw
||=
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/additional_metrics.yml'
)).
map
(
&
:deep_symbolize_keys
)
end
end
end
end
end
end
lib/gitlab/prometheus/metric_group.rb
View file @
e74896df
...
@@ -22,7 +22,7 @@ module Gitlab::Prometheus
...
@@ -22,7 +22,7 @@ module Gitlab::Prometheus
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
group
=
MetricGroup
.
new
(
entry
[
:group
],
entry
[
:priority
])
group
=
MetricGroup
.
new
(
entry
[
:group
],
entry
[
:priority
])
group
.
metrics
=
Metric
.
metrics_from_list
(
group
,
entry
[
:metrics
])
group
.
metrics
=
Metric
.
metrics_from_list
(
entry
[
:metrics
])
group
group
end
end
...
...
lib/gitlab/prometheus/queries/matched_metrics_query.rb
View file @
e74896df
...
@@ -16,31 +16,28 @@ module Gitlab::Prometheus::Queries
...
@@ -16,31 +16,28 @@ module Gitlab::Prometheus::Queries
private
private
def
groups_data
def
groups_data
metrics_
series
=
metrics_with_serie
s
(
Gitlab
::
Prometheus
::
MetricGroup
.
all
)
metrics_
groups
=
groups_with_active_metric
s
(
Gitlab
::
Prometheus
::
MetricGroup
.
all
)
lookup
=
active_series_lookup
(
metrics_
serie
s
)
lookup
=
active_series_lookup
(
metrics_
group
s
)
groups
=
{}
groups
=
{}
metrics_series
.
each
do
|
metrics
,
series
|
metrics_groups
.
each
do
|
group
|
groups
[
metrics
.
group
]
||=
{
active_metrics:
0
,
metrics_missing_requirements:
0
}
groups
[
group
]
||=
{
active_metrics:
0
,
metrics_missing_requirements:
0
}
group
=
groups
[
metrics
.
group
]
metrics
=
group
.
metrics
.
flat_map
(
&
:required_metrics
)
active_metrics
=
metrics
.
count
(
&
lookup
.
method
(
:has_key?
))
if
series
.
all?
(
&
lookup
.
method
(
:has_key?
))
groups
[
group
][
:active_metrics
]
+=
active_metrics
group
[
:active_metrics
]
+=
1
groups
[
group
][
:metrics_missing_requirements
]
+=
metrics
.
count
-
active_metrics
else
group
[
:metrics_missing_requirements
]
+=
1
end
group
end
end
groups
groups
end
end
def
active_series_lookup
(
metrics
)
def
active_series_lookup
(
metric
_group
s
)
timeframe_start
=
8
.
hours
.
ago
timeframe_start
=
8
.
hours
.
ago
timeframe_end
=
Time
.
now
timeframe_end
=
Time
.
now
series
=
metric
s
.
flat_map
{
|
metrics
,
series
|
series
}
.
uniq
series
=
metric
_groups
.
flat_map
(
&
:metrics
).
flat_map
(
&
:required_metrics
)
.
uniq
lookup
=
series
.
each_slice
(
MAX_QUERY_ITEMS
).
flat_map
do
|
batched_series
|
lookup
=
series
.
each_slice
(
MAX_QUERY_ITEMS
).
flat_map
do
|
batched_series
|
client_series
(
*
batched_series
,
start:
timeframe_start
,
stop:
timeframe_end
)
client_series
(
*
batched_series
,
start:
timeframe_start
,
stop:
timeframe_end
)
...
@@ -54,17 +51,27 @@ module Gitlab::Prometheus::Queries
...
@@ -54,17 +51,27 @@ module Gitlab::Prometheus::Queries
series_info
.
has_key?
(
'environment'
)
series_info
.
has_key?
(
'environment'
)
end
end
def
metrics_with_series
(
metric_groups
)
def
available_metrics
label_values
=
client_label_values
||
[]
@available_metrics
||=
client_label_values
||
[]
end
metrics
=
metric_groups
.
flat_map
do
|
group
|
def
filter_active_metrics
(
metric_group
)
group
.
metrics
.
map
do
|
metric
|
metric_group
.
metrics
.
select!
do
|
metric
|
matcher
=
Regexp
.
compile
(
metric
.
detect
)
metric
.
required_metrics
.
all?
(
&
available_metrics
.
method
(
:include?
))
[
metric
,
label_values
.
select
(
&
matcher
.
method
(
:match
))]
end
metric_group
end
end
def
groups_with_active_metrics
(
metric_groups
)
metric_groups
.
map
(
&
method
(
:filter_active_metrics
)).
select
{
|
group
|
group
.
metrics
.
any?
}
end
end
metrics
.
select
{
|
metric
,
labels
|
labels
&
.
any?
}
def
metrics_with_required_series
(
metric_groups
)
metric_groups
.
flat_map
do
|
group
|
group
.
metrics
.
select
do
|
metric
|
metric
.
required_metrics
.
all?
(
&
available_metrics
.
method
(
:include?
))
end
end
end
end
end
end
end
end
spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
0 → 100644
View file @
e74896df
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
,
lib:
true
do
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
let
(
:deployment
)
{
create
(
:deployment
,
environment:
environment
)
}
let
(
:client
)
{
double
(
'prometheus_client'
)
}
subject
{
described_class
.
new
(
client
)
}
around
do
|
example
|
time_without_subsecond_values
=
Time
.
local
(
2008
,
9
,
1
,
12
,
0
,
0
)
Timecop
.
freeze
(
time_without_subsecond_values
)
{
example
.
run
}
end
let
(
:metric_group_class
)
{
Gitlab
::
Prometheus
::
MetricGroup
}
let
(
:metric_class
)
{
Gitlab
::
Prometheus
::
Metric
}
let
(
:simple_metrics
)
do
[
metric_class
.
new
(
'title'
,
[
'metrica'
,
'metricb'
],
'1'
,
'y_label'
,
[{
:query_range
=>
'avg'
}])
]
end
let
(
:simple_metric_group
)
do
metric_group_class
.
new
(
'name'
,
1
,
simple_metrics
)
end
let
(
:xx
)
do
[{
'__name__'
:
'metrica'
,
'environment'
:
'mattermost'
},
{
'__name__'
:
'metricb'
,
'environment'
:
'mattermost'
}]
end
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
])
allow
(
client
).
to
receive
(
:label_values
).
and_return
([
'metrica'
,
'metricb'
])
allow
(
client
).
to
receive
(
:series
).
and_return
(
xx
)
end
it
"something something"
do
expect
(
subject
.
query
).
to
eq
(
"asf"
)
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