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
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
...
@@ -75,6 +75,11 @@ class PrometheusService < MonitoringService
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
end
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
# Cache metrics for specific environment
def
calculate_reactive_cache
(
query_class_name
,
*
args
)
def
calculate_reactive_cache
(
query_class_name
,
*
args
)
return
unless
active?
&&
project
&&
!
project
.
pending_delete?
return
unless
active?
&&
project
&&
!
project
.
pending_delete?
...
...
lib/gitlab/prometheus/queries/query_additional_metrics.rb
View file @
6df5bd8b
...
@@ -42,15 +42,17 @@ module Gitlab
...
@@ -42,15 +42,17 @@ module Gitlab
end
end
def
process_query
(
context
,
query
)
def
process_query
(
context
,
query
)
query
_with_result
=
query
.
dup
query
=
query
.
dup
result
=
result
=
if
query
.
key?
(
:query_range
)
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
else
client_query
(
query
[
:query
]
%
context
,
time:
context
[
:timeframe_end
])
query
[
:query
]
%=
context
client_query
(
query
[
:query
],
time:
context
[
:timeframe_end
])
end
end
query
_with_result
[
:result
]
=
result
&
.
map
(
&
:deep_symbolize_keys
)
query
[
:result
]
=
result
&
.
map
(
&
:deep_symbolize_keys
)
query
_with_result
query
end
end
def
available_metrics
def
available_metrics
...
@@ -69,15 +71,11 @@ module Gitlab
...
@@ -69,15 +71,11 @@ module Gitlab
end
end
def
common_query_context
(
environment
)
def
common_query_context
(
environment
)
variables
=
{
{
ci_environment_slug:
environment
.
slug
,
ci_environment_slug:
environment
.
slug
,
kube_namespace:
environment
.
project
.
kubernetes_service
.
actual_namespace
,
kube_namespace:
environment
.
project
.
kubernetes_service
&
.
actual_namespace
||
''
,
}.
flat_map
{
|
k
,
v
|
[[
k
,
v
],
[
k
.
upcase
,
v
]]
}.
to_h
macros
=
{
environment_filter:
%{container_name!="POD",environment="#{environment.slug}"}
environment_filter:
%{container_name!="POD",environment="#{environment.slug}"}
}
}
variables
.
merge
(
macros
)
end
end
end
end
end
end
...
...
spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb
View file @
6df5bd8b
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsDeploymentQuery
do
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
|
around
do
|
example
|
Timecop
.
freeze
(
Time
.
local
(
2008
,
9
,
1
,
12
,
0
,
0
))
{
example
.
run
}
Timecop
.
freeze
(
Time
.
local
(
2008
,
9
,
1
,
12
,
0
,
0
))
{
example
.
run
}
end
end
include_examples
'additional metrics query'
do
include_examples
'additional metrics query'
do
let
(
:deployment
)
{
create
(
:deployment
,
environment:
environment
)
}
let
(
:query_params
)
{
[
deployment
.
id
]
}
it
'queries using specific time'
do
it
'queries using specific time'
do
expect
(
client
).
to
receive
(
:query_range
).
with
(
anything
,
expect
(
client
).
to
receive
(
:query_range
).
with
(
anything
,
start:
(
deployment
.
created_at
-
30
.
minutes
).
to_f
,
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'
require
'spec_helper'
describe
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsEnvironmentQuery
do
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
|
around
do
|
example
|
Timecop
.
freeze
{
example
.
run
}
Timecop
.
freeze
{
example
.
run
}
end
end
include_examples
'additional metrics query'
do
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
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
(
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
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
...
@@ -10,12 +10,39 @@ RSpec.shared_examples 'additional metrics query' do
[{
'metric'
:
{},
'values'
:
[[
1488758662.506
,
'0.00002996364761904785'
],
[
1488758722.506
,
'0.00003090239047619091'
]]
}]
[{
'metric'
:
{},
'values'
:
[[
1488758662.506
,
'0.00002996364761904785'
],
[
1488758722.506
,
'0.00003090239047619091'
]]
}]
end
end
let
(
:client
)
{
double
(
'prometheus_client'
)
}
let
(
:environment
)
{
create
(
:environment
,
slug:
'environment-slug'
)
}
before
do
before
do
allow
(
client
).
to
receive
(
:label_values
).
and_return
(
metric_names
)
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
])])
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
([
simple_metric_group
(
metrics:
[
simple_metric
])])
end
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
context
'with one group where two metrics is found'
do
let
(
:query_result
)
{
described_class
.
new
(
client
).
query
(
*
query_params
)
}
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
])
end
end
...
@@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do
...
@@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do
end
end
context
'with two groups with one metric each'
do
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
])]
}
let
(
:metrics
)
{
[
simple_metric
(
queries:
[
simple_query
])]
}
before
do
before
do
allow
(
metric_group_class
).
to
receive
(
:all
).
and_return
(
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