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
5ceec83a
Commit
5ceec83a
authored
Mar 05, 2018
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemente Prometheus:AdapterService
parent
273e4142
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
30 deletions
+84
-30
app/controllers/projects/prometheus/metrics_controller.rb
app/controllers/projects/prometheus/metrics_controller.rb
+4
-2
app/models/concerns/prometheus_adapter_locator.rb
app/models/concerns/prometheus_adapter_locator.rb
+0
-26
app/models/environment.rb
app/models/environment.rb
+4
-2
app/services/prometheus/adapter_service.rb
app/services/prometheus/adapter_service.rb
+36
-0
spec/services/prometheus/adapter_service_spec.rb
spec/services/prometheus/adapter_service_spec.rb
+40
-0
No files found.
app/controllers/projects/prometheus/metrics_controller.rb
View file @
5ceec83a
module
Projects
module
Projects
module
Prometheus
module
Prometheus
class
MetricsController
<
Projects
::
ApplicationController
class
MetricsController
<
Projects
::
ApplicationController
include
PrometheusAdapterLocator
before_action
:authorize_admin_project!
before_action
:authorize_admin_project!
before_action
:require_prometheus_metrics!
before_action
:require_prometheus_metrics!
...
@@ -22,6 +20,10 @@ module Projects
...
@@ -22,6 +20,10 @@ module Projects
private
private
def
prometheus_adapter
@prometheus_adapter
||=
Prometheus
::
AdapterService
.
new
(
project
).
prometheus_adapter
end
def
require_prometheus_metrics!
def
require_prometheus_metrics!
render_404
unless
prometheus_adapter
.
can_query?
render_404
unless
prometheus_adapter
.
can_query?
end
end
...
...
app/models/concerns/prometheus_adapter_locator.rb
deleted
100644 → 0
View file @
273e4142
module
PrometheusAdapterLocator
def
deployment_platform
project
.
deployment_platform
end
def
prometheus_adapter
@prometheus_adapter
||=
if
service_prometheus_adapter
.
can_query?
service_prometheus_adapter
else
cluster_prometheus_adapter
end
end
def
service_prometheus_adapter
project
.
find_or_initialize_service
(
'prometheus'
)
end
def
cluster_prometheus_adapter
return
unless
deployment_platform
.
respond_to?
(
:cluster
)
cluster
=
deployment_platform
.
cluster
return
unless
cluster
.
application_prometheus
&
.
installed?
cluster
.
application_prometheus
end
end
app/models/environment.rb
View file @
5ceec83a
class
Environment
<
ActiveRecord
::
Base
class
Environment
<
ActiveRecord
::
Base
include
PrometheusAdapterLocator
# Used to generate random suffixes for the slug
# Used to generate random suffixes for the slug
LETTERS
=
'a'
..
'z'
LETTERS
=
'a'
..
'z'
NUMBERS
=
'0'
..
'9'
NUMBERS
=
'0'
..
'9'
...
@@ -159,6 +157,10 @@ class Environment < ActiveRecord::Base
...
@@ -159,6 +157,10 @@ class Environment < ActiveRecord::Base
prometheus_adapter
.
query
(
:additional_metrics_environment
,
self
)
if
has_metrics?
prometheus_adapter
.
query
(
:additional_metrics_environment
,
self
)
if
has_metrics?
end
end
def
prometheus_adapter
@prometheus_adapter
||=
Prometheus
::
AdapterService
.
new
(
project
,
deployment_platform
).
prometheus_adapter
end
def
slug
def
slug
super
.
presence
||
generate_slug
super
.
presence
||
generate_slug
end
end
...
...
app/services/prometheus/adapter_service.rb
0 → 100644
View file @
5ceec83a
module
Prometheus
class
AdapterService
def
initialize
(
project
,
deployment_platform
=
nil
)
@project
=
project
@deployment_platform
=
if
deployment_platform
deployment_platform
else
project
.
deployment_platform
end
end
attr_reader
:deployment_platform
,
:project
def
prometheus_adapter
@prometheus_adapter
||=
if
service_prometheus_adapter
.
can_query?
service_prometheus_adapter
else
cluster_prometheus_adapter
end
end
def
service_prometheus_adapter
project
.
find_or_initialize_service
(
'prometheus'
)
end
def
cluster_prometheus_adapter
return
unless
deployment_platform
.
respond_to?
(
:cluster
)
cluster
=
deployment_platform
.
cluster
return
unless
cluster
.
application_prometheus
&
.
installed?
cluster
.
application_prometheus
end
end
end
spec/services/prometheus/adapter_service_spec.rb
0 → 100644
View file @
5ceec83a
require
'spec_helper'
describe
Prometheus
::
AdapterService
do
let
(
:project
)
{
create
(
:project
)
}
subject
{
described_class
.
new
(
project
)
}
describe
'#prometheus_adapter'
do
let
(
:cluster
)
{
create
(
:cluster
,
:provided_by_user
,
environment_scope:
'*'
,
projects:
[
project
])
}
context
'prometheus service can execute queries'
do
let
(
:prometheus_service
)
{
double
(
:prometheus_service
,
can_query?:
true
)
}
before
do
allow
(
project
).
to
receive
(
:find_or_initialize_service
).
with
(
'prometheus'
).
and_return
prometheus_service
end
it
'return prometheus service as prometheus adapter'
do
expect
(
subject
.
prometheus_adapter
).
to
eq
(
prometheus_service
)
end
end
context
"prometheus service can't execute queries"
do
let
(
:prometheus_service
)
{
double
(
:prometheus_service
,
can_query?:
false
)
}
context
'with cluster with prometheus installed'
do
let!
(
:prometheus
)
{
create
(
:clusters_applications_prometheus
,
:installed
,
cluster:
cluster
)
}
it
'returns application handling all environments'
do
expect
(
subject
.
prometheus_adapter
).
to
eq
(
prometheus
)
end
end
context
'with cluster without prometheus installed'
do
it
'returns nil'
do
expect
(
subject
.
prometheus_adapter
).
to
be_nil
end
end
end
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