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
Boxiang Sun
gitlab-ce
Commits
a7e12052
Commit
a7e12052
authored
Jun 07, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use explicit instance of prometheus service and add access methods to it
parent
969b8124
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
136 additions
and
33 deletions
+136
-33
app/controllers/projects/deployments_controller.rb
app/controllers/projects/deployments_controller.rb
+1
-1
app/controllers/projects/prometheus_controller.rb
app/controllers/projects/prometheus_controller.rb
+5
-9
app/models/deployment.rb
app/models/deployment.rb
+7
-6
app/models/environment.rb
app/models/environment.rb
+9
-5
app/models/project_services/prometheus_service.rb
app/models/project_services/prometheus_service.rb
+10
-2
spec/controllers/projects/deployments_controller_spec.rb
spec/controllers/projects/deployments_controller_spec.rb
+62
-0
spec/controllers/projects/prometheus_controller_spec.rb
spec/controllers/projects/prometheus_controller_spec.rb
+6
-7
spec/models/deployment_spec.rb
spec/models/deployment_spec.rb
+29
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+7
-3
No files found.
app/controllers/projects/deployments_controller.rb
View file @
a7e12052
...
...
@@ -23,7 +23,7 @@ class Projects::DeploymentsController < Projects::ApplicationController
end
def
additional_metrics
return
render_404
unless
deployment
.
has_additional_metrics
?
return
render_404
unless
deployment
.
prometheus_service
.
present
?
metrics
=
deployment
.
additional_metrics
...
...
app/controllers/projects/prometheus_controller.rb
View file @
a7e12052
...
...
@@ -3,10 +3,10 @@ class Projects::PrometheusController < Projects::ApplicationController
before_action
:require_prometheus_metrics!
def
active_metrics
matched_metrics
=
prometheus_service
.
reactive_query
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
||
{}
respond_to
do
|
format
|
format
.
json
do
matched_metrics
=
prometheus_service
.
matched_metrics
||
{}
if
matched_metrics
.
any?
render
json:
matched_metrics
else
...
...
@@ -18,19 +18,15 @@ class Projects::PrometheusController < Projects::ApplicationController
private
rescue_from
(
ActionController
::
UnknownFormat
)
do
|
e
|
rescue_from
(
ActionController
::
UnknownFormat
)
do
render_404
end
def
prometheus_service
project
.
monitoring_service
end
def
has_prometheus_metrics?
prometheus_service
&
.
respond_to?
(
:reactive_query
)
@prometheus_service
||=
project
.
monitoring_services
.
reorder
(
nil
).
find_by
(
active:
true
,
type:
PrometheusService
.
name
)
end
def
require_prometheus_metrics!
render_404
unless
has_prometheus_metrics
?
render_404
unless
prometheus_service
.
present
?
end
end
app/models/deployment.rb
View file @
a7e12052
...
...
@@ -103,10 +103,6 @@ class Deployment < ActiveRecord::Base
project
.
monitoring_service
.
present?
end
def
has_additional_metrics?
has_metrics?
&&
project
.
monitoring_service
&
.
respond_to?
(
:reactive_query
)
end
def
metrics
return
{}
unless
has_metrics?
...
...
@@ -114,11 +110,16 @@ class Deployment < ActiveRecord::Base
end
def
additional_metrics
return
{}
unless
has_additional_metrics?
metrics
=
project
.
monitoring_service
.
reactive_query
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsDeploymentQuery
.
name
,
id
,
&
:itself
)
return
{}
unless
prometheus_service
.
present?
metrics
=
prometheus_service
.
additional_deployment_metrics
(
self
)
metrics
&
.
merge
(
deployment_time:
created_at
.
to_i
)
||
{}
end
def
prometheus_service
@prometheus_service
||=
project
.
monitoring_services
.
reorder
(
nil
).
find_by
(
active:
true
,
type:
PrometheusService
.
name
)
end
private
def
ref_path
...
...
app/models/environment.rb
View file @
a7e12052
...
...
@@ -149,20 +149,24 @@ class Environment < ActiveRecord::Base
project
.
monitoring_service
.
present?
&&
available?
&&
last_deployment
.
present?
end
def
has_additional_metrics?
has_metrics?
&&
project
.
monitoring_service
&
.
respond_to?
(
:reactive_query
)
end
def
metrics
project
.
monitoring_service
.
environment_metrics
(
self
)
if
has_metrics?
end
def
has_additional_metrics?
prometheus_service
.
present?
&&
available?
&&
last_deployment
.
present?
end
def
additional_metrics
if
has_additional_metrics?
pro
ject
.
monitoring_service
.
reactive_query
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsEnvironmentQuery
.
name
,
self
.
id
,
&
:it
self
)
pro
metheus_service
.
additional_environment_metrics
(
self
)
end
end
def
prometheus_service
@prometheus_service
||=
project
.
monitoring_services
.
reorder
(
nil
).
find_by
(
active:
true
,
type:
PrometheusService
.
name
)
end
# An environment name is not necessarily suitable for use in URLs, DNS
# or other third-party contexts, so provide a slugified version. A slug has
# the following properties:
...
...
app/models/project_services/prometheus_service.rb
View file @
a7e12052
...
...
@@ -62,8 +62,16 @@ class PrometheusService < MonitoringService
metrics
&
.
merge
(
deployment_time:
created_at
.
to_i
)
||
{}
end
def
reactive_query
(
query_class
,
*
args
,
&
block
)
with_reactive_cache
(
query_class
,
*
args
,
&
block
)
def
additional_environment_metrics
(
environment
)
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsEnvironmentQuery
.
name
,
environment
.
id
,
&
:itself
)
end
def
additional_deployment_metrics
(
deployment
)
with_reactive_cache
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsDeploymentQuery
.
name
,
deployment
.
id
,
&
:itself
)
end
def
matched_metrics
additional_deployment_metrics
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
,
&
:itself
)
end
# Cache metrics for specific environment
...
...
spec/controllers/projects/deployments_controller_spec.rb
View file @
a7e12052
...
...
@@ -108,6 +108,68 @@ describe Projects::DeploymentsController do
end
end
describe
'GET #additional_metrics'
do
let
(
:deployment
)
{
create
(
:deployment
,
project:
project
,
environment:
environment
)
}
before
do
allow
(
controller
).
to
receive
(
:deployment
).
and_return
(
deployment
)
end
context
'when metrics are disabled'
do
before
do
allow
(
deployment
).
to
receive
(
:has_metrics?
).
and_return
false
end
it
'responds with not found'
do
get
:metrics
,
deployment_params
(
id:
deployment
.
id
)
expect
(
response
).
to
be_not_found
end
end
context
'when metrics are enabled'
do
let
(
:prometheus_service
)
{
double
(
'prometheus_service'
)
}
before
do
allow
(
deployment
).
to
receive
(
:prometheus_service
).
and_return
(
prometheus_service
)
end
context
'when environment has no metrics'
do
before
do
expect
(
deployment
).
to
receive
(
:additional_metrics
).
and_return
({})
end
it
'returns a empty response 204 response'
do
get
:additional_metrics
,
deployment_params
(
id:
deployment
.
id
)
expect
(
response
).
to
have_http_status
(
204
)
expect
(
response
.
body
).
to
eq
(
''
)
end
end
context
'when environment has some metrics'
do
let
(
:empty_metrics
)
do
{
success:
true
,
metrics:
{},
last_update:
42
}
end
before
do
expect
(
deployment
).
to
receive
(
:additional_metrics
).
and_return
(
empty_metrics
)
end
it
'returns a metrics JSON document'
do
get
:additional_metrics
,
deployment_params
(
id:
deployment
.
id
)
expect
(
response
).
to
be_ok
expect
(
json_response
[
'success'
]).
to
be
(
true
)
expect
(
json_response
[
'metrics'
]).
to
eq
({})
expect
(
json_response
[
'last_update'
]).
to
eq
(
42
)
end
end
end
end
def
deployment_params
(
opts
=
{})
opts
.
reverse_merge
(
namespace_id:
project
.
namespace
,
project_id:
project
,
...
...
spec/controllers/projects/prometheus_controller_spec.rb
View file @
a7e12052
...
...
@@ -8,7 +8,7 @@ describe Projects::PrometheusController do
before
do
allow
(
controller
).
to
receive
(
:project
).
and_return
(
project
)
allow
(
project
).
to
receive
(
:monitoring
_service
).
and_return
(
prometheus_service
)
allow
(
controller
).
to
receive
(
:prometheus
_service
).
and_return
(
prometheus_service
)
project
.
add_master
(
user
)
sign_in
(
user
)
...
...
@@ -16,11 +16,11 @@ describe Projects::PrometheusController do
describe
'GET #active_metrics'
do
context
'when prometheus metrics are enabled'
do
context
'when data is not present'
do
before
do
allow
(
prometheus_service
).
to
receive
(
:reactive_query
)
allow
(
prometheus_service
).
to
receive
(
:matched_metrics
).
and_return
({}
)
end
context
'when data is not present'
do
it
'returns no content response'
do
get
:active_metrics
,
project_params
(
format: :json
)
...
...
@@ -32,8 +32,7 @@ describe Projects::PrometheusController do
let
(
:sample_response
)
{
{
some_data:
1
}
}
before
do
allow
(
prometheus_service
).
to
receive
(
:reactive_query
).
with
(
Gitlab
::
Prometheus
::
Queries
::
MatchedMetricsQuery
.
name
)
.
and_return
(
sample_response
)
allow
(
prometheus_service
).
to
receive
(
:matched_metrics
).
and_return
(
sample_response
)
end
it
'returns no content response'
do
...
...
spec/models/deployment_spec.rb
View file @
a7e12052
...
...
@@ -77,6 +77,35 @@ describe Deployment, models: true do
end
end
describe
'#additional_metrics'
do
let
(
:deployment
)
{
create
(
:deployment
)
}
subject
{
deployment
.
additional_metrics
}
context
'metrics are disabled'
do
it
{
is_expected
.
to
eq
({})
}
end
context
'metrics are enabled'
do
let
(
:simple_metrics
)
do
{
success:
true
,
metrics:
{},
last_update:
42
}
end
let
(
:prometheus_service
)
{
double
(
'prometheus_service'
)
}
before
do
allow
(
deployment
).
to
receive
(
:prometheus_service
).
and_return
(
prometheus_service
)
allow
(
prometheus_service
).
to
receive
(
:additional_deployment_metrics
).
and_return
(
simple_metrics
)
end
it
{
is_expected
.
to
eq
(
simple_metrics
.
merge
({
deployment_time:
deployment
.
created_at
.
to_i
}))
}
end
end
describe
'#stop_action'
do
let
(
:build
)
{
create
(
:ci_build
)
}
...
...
spec/models/environment_spec.rb
View file @
a7e12052
...
...
@@ -452,8 +452,8 @@ describe Environment, models: true do
end
it
'returns the additional metrics from the deployment service'
do
expect
(
project
.
monitoring_service
).
to
receive
(
:reactive_query
)
.
with
(
Gitlab
::
Prometheus
::
Queries
::
AdditionalMetricsEnvironmentQuery
.
name
,
environment
.
id
)
expect
(
environment
.
prometheus_service
).
to
receive
(
:additional_environment_metrics
)
.
with
(
environment
)
.
and_return
(
:fake_metrics
)
is_expected
.
to
eq
(
:fake_metrics
)
...
...
@@ -470,7 +470,11 @@ describe Environment, models: true do
end
describe
'#has_additional_metrics??'
do
subject
{
environment
.
has_metrics?
}
subject
{
environment
.
has_additional_metrics?
}
before
do
end
context
'when the enviroment is available'
do
context
'with a deployment service'
do
...
...
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