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
Jérome Perrin
gitlab-ce
Commits
6df5bd8b
Commit
6df5bd8b
authored
Jul 31, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Context handling and tests cleanup + simple test kube_namespace context test
parent
ce83e563
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
26 deletions
+49
-26
app/models/project_services/prometheus_service.rb
app/models/project_services/prometheus_service.rb
+5
-0
lib/gitlab/prometheus/queries/query_additional_metrics.rb
lib/gitlab/prometheus/queries/query_additional_metrics.rb
+9
-11
spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb
...theus/queries/additional_metrics_deployment_query_spec.rb
+3
-8
spec/lib/gitlab/prometheus/queries/additional_metrics_environment_query_spec.rb
...heus/queries/additional_metrics_environment_query_spec.rb
+3
-7
spec/support/prometheus/additional_metrics_shared_examples.rb
.../support/prometheus/additional_metrics_shared_examples.rb
+29
-0
No files found.
app/models/project_services/prometheus_service.rb
View file @
6df5bd8b
...
...
@@ -75,6 +75,11 @@ class PrometheusService < MonitoringService
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
end
def
with_reactive_cache
(
name
,
*
args
,
&
block
)
vals
=
args
.
map
(
&
:to_s
)
yield
calculate_reactive_cache
(
name
,
*
vals
)
end
# Cache metrics for specific environment
def
calculate_reactive_cache
(
query_class_name
,
*
args
)
return
unless
active?
&&
project
&&
!
project
.
pending_delete?
...
...
lib/gitlab/prometheus/queries/query_additional_metrics.rb
View file @
6df5bd8b
...
...
@@ -42,15 +42,17 @@ module Gitlab
end
def
process_query
(
context
,
query
)
query
_with_result
=
query
.
dup
query
=
query
.
dup
result
=
if
query
.
key?
(
:query_range
)
client_query_range
(
query
[
:query_range
]
%
context
,
start:
context
[
:timeframe_start
],
stop:
context
[
:timeframe_end
])
query
[
:query_range
]
%=
context
client_query_range
(
query
[
:query_range
],
start:
context
[
:timeframe_start
],
stop:
context
[
:timeframe_end
])
else
client_query
(
query
[
:query
]
%
context
,
time:
context
[
:timeframe_end
])
query
[
:query
]
%=
context
client_query
(
query
[
:query
],
time:
context
[
:timeframe_end
])
end
query
_with_result
[
:result
]
=
result
&
.
map
(
&
:deep_symbolize_keys
)
query
_with_result
query
[
:result
]
=
result
&
.
map
(
&
:deep_symbolize_keys
)
query
end
def
available_metrics
...
...
@@ -69,15 +71,11 @@ module Gitlab
end
def
common_query_context
(
environment
)
variables
=
{
{
ci_environment_slug:
environment
.
slug
,
kube_namespace:
environment
.
project
.
kubernetes_service
.
actual_namespace
,
}.
flat_map
{
|
k
,
v
|
[[
k
,
v
],
[
k
.
upcase
,
v
]]
}.
to_h
macros
=
{
kube_namespace:
environment
.
project
.
kubernetes_service
&
.
actual_namespace
||
''
,
environment_filter:
%{container_name!="POD",environment="#{environment.slug}"}
}
variables
.
merge
(
macros
)
end
end
end
...
...
spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb
View file @
6df5bd8b
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsDeploymentQuery
do
include
Prometheus
::
MetricBuilders
let
(
:client
)
{
double
(
'prometheus_client'
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
let
(
:deployment
)
{
create
(
:deployment
,
environment:
environment
)
}
subject
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
deployment
.
id
)
}
around
do
|
example
|
Timecop
.
freeze
(
Time
.
local
(
2008
,
9
,
1
,
12
,
0
,
0
))
{
example
.
run
}
end
include_examples
'additional metrics query'
do
let
(
:deployment
)
{
create
(
:deployment
,
environment:
environment
)
}
let
(
:query_params
)
{
[
deployment
.
id
]
}
it
'queries using specific time'
do
expect
(
client
).
to
receive
(
:query_range
).
with
(
anything
,
start:
(
deployment
.
created_at
-
30
.
minutes
).
to_f
,
...
...
spec/lib/gitlab/prometheus/queries/additional_metrics_environment_query_spec.rb
View file @
6df5bd8b
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsEnvironmentQuery
do
include
Prometheus
::
MetricBuilders
let
(
:client
)
{
double
(
'prometheus_client'
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
subject
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
environment
.
id
)
}
around
do
|
example
|
Timecop
.
freeze
{
example
.
run
}
end
include_examples
'additional metrics query'
do
let
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
environment
.
id
)
}
let
(
:query_params
)
{
[
environment
.
id
]
}
it
'queries using specific time'
do
expect
(
client
).
to
receive
(
:query_range
).
with
(
anything
,
start:
8
.
hours
.
ago
.
to_f
,
stop:
Time
.
now
.
to_f
)
expect
(
query_result
).
not_to
be_nil
...
...
spec/support/prometheus/additional_metrics_shared_examples.rb
View file @
6df5bd8b
...
...
@@ -10,12 +10,39 @@ RSpec.shared_examples 'additional metrics query' do
[{
'metric'
:
{},
'values'
:
[[
1488758662.506
,
'0.00002996364761904785'
],
[
1488758722.506
,
'0.00003090239047619091'
]]
}]
end
let
(
:client
)
{
double
(
'prometheus_client'
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
before
do
allow
(
client
).
to
receive
(
:label_values
).
and_return
(
metric_names
)
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
(
metrics:
[
simple_metric
])])
end
context
'metrics rendering'
do
subject!
{
described_class
.
new
(
client
)
}
before
do
end
describe
'project has kubernetes service'
do
let
(
:project
)
{
create
(
:kubernetes_project
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
,
project:
project
)
}
let
(
:kube_namespace
)
{
project
.
kubernetes_service
.
actual_namespace
}
it
'query context contains kube namespace'
do
expect
(
subject
).
to
receive
(
:query_metrics
).
with
(
hash_including
(
kube_namespace:
kube_namespace
)
)
subject
.
query
(
*
query_params
)
end
end
end
context
'with one group where two metrics is found'
do
let
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
*
query_params
)
}
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
])
end
...
...
@@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do
end
context
'with two groups with one metric each'
do
let
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
*
query_params
)
}
let
(
:metrics
)
{
[
simple_metric
(
queries:
[
simple_query
])]
}
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
(
[
...
...
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