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
254830c1
Commit
254830c1
authored
May 23, 2017
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move most of MetricsController logic to MetricsService
parent
466beeb3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
37 deletions
+47
-37
app/controllers/metrics_controller.rb
app/controllers/metrics_controller.rb
+6
-35
app/services/metrics_service.rb
app/services/metrics_service.rb
+38
-0
spec/controllers/metrics_controller_spec.rb
spec/controllers/metrics_controller_spec.rb
+3
-2
No files found.
app/controllers/metrics_controller.rb
View file @
254830c1
require
'prometheus/client/formats/text'
class
MetricsController
<
ActionController
::
Base
class
MetricsController
<
ActionController
::
Base
protect_from_forgery
with: :exception
protect_from_forgery
with: :exception
before_action
:validate_prometheus_metrics
include
RequiresHealthToken
include
RequiresHealthToken
before_action
:ensure_prometheus_metrics_are_enabled
CHECKS
=
[
Gitlab
::
HealthChecks
::
DbCheck
,
Gitlab
::
HealthChecks
::
RedisCheck
,
Gitlab
::
HealthChecks
::
FsShardsCheck
].
freeze
def
metrics
def
metrics
metrics_text
=
Prometheus
::
Client
::
Formats
::
Text
.
marshal_multiprocess
(
multiprocess_metrics_path
)
response
=
"
#{
metrics_service
.
health_metrics_text
}
\n
#{
metrics_service
.
prometheus_metrics_text
}
"
response
=
"
#{
health_metrics_text
}
\n
#{
metrics_text
}
"
render
text:
response
,
content_type:
'text/plain; version=0.0.4'
render
text:
response
,
content_type:
'text/plain; version=0.0.4'
end
end
private
private
def
ensure_prometheus_metrics_are_enabled
def
metrics_service
return
render_404
unless
Gitlab
::
Metrics
.
prometheus_metrics_enabled?
@metrics_service
||=
MetricsService
.
new
end
end
def
multiprocess_metrics_path
def
validate_prometheus_metrics
Rails
.
root
.
join
(
ENV
[
'prometheus_multiproc_dir'
])
render_404
unless
Gitlab
::
Metrics
.
prometheus_metrics_enabled?
end
def
health_metrics_text
results
=
CHECKS
.
flat_map
(
&
:metrics
)
types
=
results
.
map
(
&
:name
).
uniq
.
map
{
|
metric_name
|
"# TYPE
#{
metric_name
}
gauge"
}
metrics
=
results
.
map
(
&
method
(
:metric_to_prom_line
))
types
.
concat
(
metrics
).
join
(
"
\n
"
)
end
def
metric_to_prom_line
(
metric
)
labels
=
metric
.
labels
&
.
map
{
|
key
,
value
|
"
#{
key
}
=
\"
#{
value
}
\"
"
}
&
.
join
(
','
)
||
''
if
labels
.
empty?
"
#{
metric
.
name
}
#{
metric
.
value
}
"
else
"
#{
metric
.
name
}
{
#{
labels
}
}
#{
metric
.
value
}
"
end
end
end
end
end
app/services/metrics_service.rb
0 → 100644
View file @
254830c1
require
'prometheus/client/formats/text'
class
MetricsService
CHECKS
=
[
Gitlab
::
HealthChecks
::
DbCheck
,
Gitlab
::
HealthChecks
::
RedisCheck
,
Gitlab
::
HealthChecks
::
FsShardsCheck
].
freeze
def
prometheus_metrics_text
Prometheus
::
Client
::
Formats
::
Text
.
marshal_multiprocess
(
multiprocess_metrics_path
)
end
def
health_metrics_text
results
=
CHECKS
.
flat_map
(
&
:metrics
)
types
=
results
.
map
(
&
:name
).
uniq
.
map
{
|
metric_name
|
"# TYPE
#{
metric_name
}
gauge"
}
metrics
=
results
.
map
(
&
method
(
:metric_to_prom_line
))
types
.
concat
(
metrics
).
join
(
"
\n
"
)
end
private
def
multiprocess_metrics_path
Rails
.
root
.
join
(
ENV
[
'prometheus_multiproc_dir'
])
end
def
metric_to_prom_line
(
metric
)
labels
=
metric
.
labels
&
.
map
{
|
key
,
value
|
"
#{
key
}
=
\"
#{
value
}
\"
"
}
&
.
join
(
','
)
||
''
if
labels
.
empty?
"
#{
metric
.
name
}
#{
metric
.
value
}
"
else
"
#{
metric
.
name
}
{
#{
labels
}
}
#{
metric
.
value
}
"
end
end
end
spec/controllers/metrics_controller_spec.rb
View file @
254830c1
...
@@ -50,7 +50,9 @@ describe MetricsController do
...
@@ -50,7 +50,9 @@ describe MetricsController do
end
end
context
'prometheus metrics are disabled'
do
context
'prometheus metrics are disabled'
do
before
do
allow
(
Gitlab
::
Metrics
).
to
receive
(
:prometheus_metrics_enabled?
).
and_return
(
false
)
allow
(
Gitlab
::
Metrics
).
to
receive
(
:prometheus_metrics_enabled?
).
and_return
(
false
)
end
it
'returns proper response'
do
it
'returns proper response'
do
get
:metrics
get
:metrics
...
@@ -65,6 +67,5 @@ describe MetricsController do
...
@@ -65,6 +67,5 @@ describe MetricsController do
expect
(
response
.
status
).
to
eq
(
404
)
expect
(
response
.
status
).
to
eq
(
404
)
end
end
end
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