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
53f818fd
Commit
53f818fd
authored
Jan 16, 2018
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor method concern to use hashes
parent
e56b3d18
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
135 additions
and
51 deletions
+135
-51
lib/gitlab/metrics/concern.rb
lib/gitlab/metrics/concern.rb
+87
-51
lib/gitlab/metrics/concern/metric_options.rb
lib/gitlab/metrics/concern/metric_options.rb
+48
-0
No files found.
lib/gitlab/metrics/concern.rb
View file @
53f818fd
...
...
@@ -2,86 +2,122 @@ module Gitlab
module
Metrics
module
Concern
extend
ActiveSupport
::
Concern
included
do
@@_metrics_provider_mutex
||=
Mutex
.
new
@@_metrics_provider_cache
||=
{}
end
class_methods
do
private
def
metrics_provider
(
type
,
name
,
docstring
,
options
=
{})
@@_metrics_provider_mutex
||=
Mutex
.
new
def
define_metric
(
type
,
name
,
opts
=
{},
&
block
)
if
instance_methods
(
false
).
include?
(
name
)
raise
ArgumentError
,
"metrics method
#{
name
}
already exists"
end
options
[
:base_labels
]
||=
{}
args
=
[
name
.
inspect
,
%{"#{docstring}"}
,
options
[
:base_labels
].
inspect
]
define_method
(
name
)
do
# avoid unnecessary method call to speed up metric access
return
@@_metrics_provider_cache
[
name
]
if
@@_metrics_provider_cache
.
has_key?
(
name
)
fetch_metric
(
type
,
name
,
opts
,
&
block
)
end
end
def
fetch_metric
(
type
,
name
,
opts
=
{},
&
block
)
# avoid synchronization to speed up metrics access
return
@@_metrics_provider_cache
[
name
]
if
@@_metrics_provider_cache
.
has_key?
(
name
)
options
=
MetricOptions
.
new
(
opts
)
options
.
evaluate
(
&
block
)
@@_metrics_provider_mutex
.
synchronize
do
@@_metrics_provider_cache
[
name
]
||=
build_metric!
(
type
,
name
,
options
)
end
@@_metrics_provider_cache
[
name
]
end
def
build_metric!
(
type
,
name
,
options
)
unless
options
.
with_feature
.
nil?
||
Feature
.
get
(
options
.
with_feature
).
enabled?
return
NullMetric
.
new
end
case
type
when
:gauge
options
[
:multiprocess_mode
]
||=
:all
args
<<
options
[
:multiprocess_mode
].
inspect
Gitlab
::
Metrics
.
gauge
(
name
,
options
.
docs
,
options
.
base_labels
,
options
.
multiprocess_mode
)
when
:counter
Gitlab
::
Metrics
.
counter
(
name
,
options
.
docs
,
options
.
base_labels
)
when
:histogram
options
[
:buckets
]
||=
::
Prometheus
::
Client
::
Histogram
::
DEFAULT_BUCKETS
args
<<
options
[
:buckets
].
inspect
Gitlab
::
Metrics
.
histogram
(
name
,
options
.
docs
,
options
.
base_labels
,
options
.
buckets
)
when
:summary
raise
NotImplementedError
,
"summary metrics are not currently supported"
else
raise
ArgumentError
,
"uknown metric type
#{
type
}
"
end
end
metric_fetching_code
=
%{Gitlab::Metrics.#{type}(#{args.join(', ')})}
# optionally wrap in feature
if
options
[
:with_feature
].
is_a?
(
Symbol
)
metric_fetching_code
=
<<-
FETCH
.
strip_heredoc
if Feature.get(
#{
options
[
:with_feature
].
inspect
}
).enabled?
#{
metric_fetching_code
}
else
Gitlab::Metrics::NullMetric.new
end
FETCH
end
counter
:global
do
docstring
"Global counter"
multiprocess_mode
:all
buckets
[
0
,
1
]
end
method_code
,
line
=
<<-
METRIC
,
__LINE__
+
1
@@_metric_provider_cached_
#{
name
}
= nil
def
#{
name
}
return @@_metric_provider_cached_
#{
name
}
if @@_metric_provider_cached_
#{
name
}
# Fetch and/or initialize counter metric
# @param [Symbol] name
# @param [Hash] opts
def
fetch_counter
(
name
,
opts
=
{},
&
block
)
define_metric
(
:counter
,
name
,
opts
,
&
block
)
end
@@_metrics_provider_mutex.synchronize do
@@_metric_provider_cached_
#{
name
}
||=
#{
metric_fetching_code
}
end
end
# DFetch and/or initialize gauge metric
# @param [Symbol] name
# @param [Hash] opts
def
fetch_gauge
(
name
,
opts
=
{},
&
block
)
define_metric
(
:counter
,
name
,
opts
,
&
block
)
end
def reload_
#{
name
}
!
@@_metric_provider_cached_
#{
name
}
= nil
end
METRIC
# Fetch and/or initialize histogram metric
# @param [Symbol] name
# @param [Hash] opts
def
fetch_histogram
(
name
,
opts
=
{},
&
block
)
define_metric
(
:histogram
,
name
,
opts
,
&
block
)
end
instance_eval
(
method_code
,
__FILE__
,
line
)
module_eval
(
method_code
,
__FILE__
,
line
)
# Fetch and/or initialize summary metric
# @param [Symbol] name
# @param [Hash] opts
def
fetch_summary
(
name
,
opts
=
{},
&
block
)
define_metric
(
:summary
,
name
,
opts
,
&
block
)
end
# De
clare
a Counter
# De
fine metric accessor method for
a Counter
# @param [Symbol] name
# @param [String] docstring
# @param [Hash] options
def
counter
(
name
,
docstring
,
options
=
{})
metrics_provider
(
:counter
,
name
,
docstring
,
options
)
# @param [Hash] opts
def
define_counter
(
name
,
opts
=
{},
&
block
)
define_metric
(
:counter
,
name
,
opts
,
&
block
)
end
# De
clare
a Gauge
# De
fine metric accessor method for
a Gauge
# @param [Symbol] name
# @param [String] docstring
# @param [Hash] options
def
gauge
(
name
,
docstring
,
options
=
{})
metrics_provider
(
:counter
,
name
,
docstring
,
options
)
# @param [Hash] opts
def
define_gauge
(
name
,
opts
=
{},
&
block
)
define_metric
(
:counter
,
name
,
opts
,
&
block
)
end
# De
clare a Histogra
am
# De
fine metric accessor method for a Histogr
am
# @param [Symbol] name
# @param [String] docstring
# @param [Hash] options
def
histogram
(
name
,
docstring
,
options
=
{})
metrics_provider
(
:histogram
,
name
,
docstring
,
options
)
# @param [Hash] opts
def
define_histogram
(
name
,
opts
=
{},
&
block
)
define_metric
(
:histogram
,
name
,
opts
,
&
block
)
end
def
summary
(
*
args
)
raise
NotImplementedError
,
"summary metrics are not currently supported"
# Define metric accessor method for a Summary
# @param [Symbol] name
# @param [Hash] opts
def
define_summary
(
name
,
opts
=
{},
&
block
)
define_metric
(
:summary
,
name
,
opts
,
&
block
)
end
end
end
...
...
lib/gitlab/metrics/concern/metric_options.rb
0 → 100644
View file @
53f818fd
module
Gitlab
module
Metrics
module
Concern
class
MetricOptions
def
initialize
(
options
=
{})
@multiprocess_mode
=
options
[
:multiprocess_mode
]
||
:all
@buckets
=
options
[
:buckets
]
||
::
Prometheus
::
Client
::
Histogram
::
DEFAULT_BUCKETS
@base_labels
=
options
[
:base_labels
]
||
{}
end
def
docs
(
docs
=
nil
)
@docs
=
docs
unless
docs
.
nil?
@docs
end
def
multiprocess_mode
(
mode
)
@multiprocess_mode
=
mode
unless
@multiprocess_mode
.
nil?
@multiprocess_mode
end
def
buckets
(
buckets
)
@buckets
=
buckets
unless
@buckets
.
nil?
@buckets
end
def
base_labels
(
base_labels
)
@base_labels
=
base_labels
unless
@base_labels
.
nil?
@base_labels
end
def
with_feature
(
name
)
@feature_name
=
name
unless
@feature_name
.
nil?
@feature_name
end
def
evaluate
(
&
block
)
instance_eval
(
&
block
)
if
block_given?
self
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