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
61d7b7f1
Commit
61d7b7f1
authored
May 25, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finalize refactoring additional metrics query
parent
20614140
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
50 deletions
+49
-50
app/models/environment.rb
app/models/environment.rb
+3
-1
app/models/project_services/prometheus_service.rb
app/models/project_services/prometheus_service.rb
+1
-1
lib/gitlab/prometheus/metric_group.rb
lib/gitlab/prometheus/metric_group.rb
+4
-4
lib/gitlab/prometheus/queries/additional_metrics_query.rb
lib/gitlab/prometheus/queries/additional_metrics_query.rb
+39
-40
lib/gitlab/prometheus/queries/matched_metrics_query.rb
lib/gitlab/prometheus/queries/matched_metrics_query.rb
+2
-4
No files found.
app/models/environment.rb
View file @
61d7b7f1
...
...
@@ -158,7 +158,9 @@ class Environment < ActiveRecord::Base
end
def
additional_metrics
project
.
monitoring_service
.
reactive_query
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsQuery
,
self
.
id
)
if
has_additional_metrics?
if
has_additional_metrics?
project
.
monitoring_service
.
reactive_query
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsQuery
.
name
,
self
.
id
,
&
:itself
)
end
end
# An environment name is not necessarily suitable for use in URLs, DNS
...
...
app/models/project_services/prometheus_service.rb
View file @
61d7b7f1
...
...
@@ -73,7 +73,7 @@ class PrometheusService < MonitoringService
end
def
reactive_query
(
query_class
,
*
args
,
&
block
)
calculate
_reactive_cache
(
query_class
,
*
args
,
&
block
)
with
_reactive_cache
(
query_class
,
*
args
,
&
block
)
end
# Cache metrics for specific environment
...
...
lib/gitlab/prometheus/metric_group.rb
View file @
61d7b7f1
...
...
@@ -13,6 +13,10 @@ module Gitlab::Prometheus
load_groups_from_yaml
end
def
self
.
load_groups_from_yaml
additional_metrics_raw
.
map
(
&
method
(
:group_from_entry
))
end
def
self
.
group_from_entry
(
entry
)
missing_fields
=
[
:group
,
:priority
,
:metrics
].
select
{
|
key
|
!
entry
.
has_key?
(
key
)
}
raise
ParsingError
.
new
(
"entry missing required fields
#{
missing_fields
}
"
)
unless
missing_fields
.
empty?
...
...
@@ -22,10 +26,6 @@ module Gitlab::Prometheus
group
end
def
self
.
load_groups_from_yaml
additional_metrics_raw
.
map
(
&
method
(
:group_from_entry
))
end
def
self
.
additional_metrics_raw
@additional_metrics_raw
||=
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/additional_metrics.yml'
))
&
.
map
(
&
:deep_symbolize_keys
).
freeze
end
...
...
lib/gitlab/prometheus/queries/additional_metrics_query.rb
View file @
61d7b7f1
module
Gitlab::Prometheus::Queries
class
AdditionalMetricsQuery
<
BaseQuery
def
self
.
metrics
@metrics
||=
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/custom_metrics.yml'
)).
freeze
end
def
query
(
environment_id
)
environment
=
Environment
.
find_by
(
id:
environment_id
)
context
=
{
environment_slug:
environment
.
slug
,
environment_filter:
%{container_name!="POD",environment="#{environment.slug}"}
}
timeframe_start
=
8
.
hours
.
ago
.
to_f
timeframe_end
=
Time
.
now
.
to_f
query_processor
=
method
(
:process_query
).
curry
[
query_context
(
environment_id
)]
matched_metrics
.
map
do
|
group
|
group
[
:metrics
].
map!
do
|
metric
|
metric
[
:queries
].
map!
do
|
query
|
query
=
query
.
symbolize_keys
query
[
:result
]
=
if
query
.
has_key?
(
:query_range
)
client_query_range
(
query
[
:query_range
]
%
context
,
start:
timeframe_start
,
stop:
timeframe_end
)
else
client_query
(
query
[
:query
]
%
context
,
time:
timeframe_end
)
end
query
metrics
=
group
.
metrics
.
map
do
|
metric
|
{
title:
metric
.
title
,
weight:
metric
.
weight
,
queries:
metric
.
queries
.
map
(
&
query_processor
)
}
end
metric
{
group:
group
.
name
,
priority:
group
.
priority
,
metrics:
metrics
}
end
group
end
private
def
query_context
(
environment_id
)
environment
=
Environment
.
find_by
(
id:
environment_id
)
{
environment_slug:
environment
.
slug
,
environment_filter:
%{container_name!="POD",environment="#{environment.slug}"}
,
timeframe_start:
8
.
hours
.
ago
.
to_f
,
timeframe_end:
Time
.
now
.
to_f
}
end
def
process_query
(
group
,
query
)
result
=
if
query
.
has_key?
(
:query_range
)
client_query_range
(
query
[
:query_range
]
%
context
,
start:
timeframe_start
,
stop:
timeframe_end
)
def
process_query
(
context
,
query
)
query_with_result
=
query
.
dup
query_with_result
[
:result
]
=
if
query
.
has_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:
timeframe_end
)
end
contains_metrics
=
result
.
all?
do
|
item
|
item
&
.
[
](
:values
)
&
.
any?
||
item
&
.
[
](
:value
)
&
.
any?
client_query
(
query
[
:query
]
%
context
,
time:
context
[
:timeframe_end
])
end
query_with_result
end
def
process_result
(
query_result
)
...
...
@@ -54,17 +53,17 @@ module Gitlab::Prometheus::Queries
def
matched_metrics
label_values
=
client_label_values
||
[]
Gitlab
::
Prometheus
::
MetricGroup
.
all
result
=
Gitlab
::
Prometheus
::
MetricsSources
.
additional_metrics
.
map
do
|
group
|
group
[
:metrics
].
map!
(
&
:symbolize_keys
)
group
[
:metrics
].
select!
do
|
metric
|
matcher
=
Regexp
.
compile
(
metric
[
:detect
])
result
=
Gitlab
::
Prometheus
::
MetricGroup
.
all
.
map
do
|
group
|
group
.
metrics
.
select!
do
|
metric
|
matcher
=
Regexp
.
compile
(
metric
.
detect
)
label_values
.
any?
&
matcher
.
method
(
:match
)
end
group
end
result
.
select
{
|
group
|
!
group
[
:metrics
].
empty?
}
result
.
select
{
|
group
|
group
.
metrics
.
any?
}
end
end
end
lib/gitlab/prometheus/queries/matched_metrics_query.rb
View file @
61d7b7f1
...
...
@@ -2,10 +2,6 @@ module Gitlab::Prometheus::Queries
class
MatchedMetricsQuery
<
BaseQuery
MAX_QUERY_ITEMS
=
40
.
freeze
def
self
.
metrics
@metrics
||=
YAML
.
load_file
(
Rails
.
root
.
join
(
'config/additional_metrics.yml'
)).
map
(
&
:deep_symbolize_keys
)
end
def
query
groups_data
.
map
do
|
group
,
data
|
{
...
...
@@ -17,6 +13,8 @@ module Gitlab::Prometheus::Queries
end
end
private
def
groups_data
metrics_series
=
metrics_with_series
(
Gitlab
::
Prometheus
::
MetricGroup
.
all
)
lookup
=
active_series_lookup
(
metrics_series
)
...
...
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