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
7d716cc8
Commit
7d716cc8
authored
Jan 19, 2018
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert InfluxDB to concern. Fix uninitialized metrics when metrics code is inherited.
parent
ea6196d4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
157 additions
and
150 deletions
+157
-150
lib/gitlab/metrics.rb
lib/gitlab/metrics.rb
+1
-1
lib/gitlab/metrics/concern.rb
lib/gitlab/metrics/concern.rb
+5
-4
lib/gitlab/metrics/influx_db.rb
lib/gitlab/metrics/influx_db.rb
+145
-141
lib/gitlab/metrics/prometheus.rb
lib/gitlab/metrics/prometheus.rb
+1
-2
spec/lib/gitlab/metrics/subscribers/rails_cache_spec.rb
spec/lib/gitlab/metrics/subscribers/rails_cache_spec.rb
+5
-2
No files found.
lib/gitlab/metrics.rb
View file @
7d716cc8
module
Gitlab
module
Gitlab
module
Metrics
module
Metrics
extend
Gitlab
::
Metrics
::
InfluxDb
include
Gitlab
::
Metrics
::
InfluxDb
include
Gitlab
::
Metrics
::
Prometheus
include
Gitlab
::
Metrics
::
Prometheus
def
self
.
enabled?
def
self
.
enabled?
...
...
lib/gitlab/metrics/concern.rb
View file @
7d716cc8
...
@@ -6,7 +6,7 @@ module Gitlab
...
@@ -6,7 +6,7 @@ module Gitlab
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
included
do
included
do
@_metric_provider_mutex
=
Mutex
.
new
@
@
_metric_provider_mutex
=
Mutex
.
new
@_metrics_provider_cache
=
{}
@_metrics_provider_cache
=
{}
end
end
...
@@ -23,12 +23,12 @@ module Gitlab
...
@@ -23,12 +23,12 @@ module Gitlab
end
end
define_singleton_method
(
name
)
do
define_singleton_method
(
name
)
do
@_metrics_provider_cache
[
name
]
||
init_metric
(
type
,
name
,
opts
,
&
block
)
@_metrics_provider_cache
&
.
[
](
name
)
||
init_metric
(
type
,
name
,
opts
,
&
block
)
end
end
end
end
def
fetch_metric
(
type
,
name
,
opts
=
{},
&
block
)
def
fetch_metric
(
type
,
name
,
opts
=
{},
&
block
)
@_metrics_provider_cache
[
name
]
||
init_metric
(
type
,
name
,
opts
,
&
block
)
@_metrics_provider_cache
&
.
[
](
name
)
||
init_metric
(
type
,
name
,
opts
,
&
block
)
end
end
def
init_metric
(
type
,
name
,
opts
=
{},
&
block
)
def
init_metric
(
type
,
name
,
opts
=
{},
&
block
)
...
@@ -43,7 +43,8 @@ module Gitlab
...
@@ -43,7 +43,8 @@ module Gitlab
end
end
def
synchronized_cache_fill
(
key
)
def
synchronized_cache_fill
(
key
)
@_metric_provider_mutex
.
synchronize
do
@@_metric_provider_mutex
.
synchronize
do
@_metrics_provider_cache
||=
{}
@_metrics_provider_cache
[
key
]
||=
yield
@_metrics_provider_cache
[
key
]
||=
yield
end
end
end
end
...
...
lib/gitlab/metrics/influx_db.rb
View file @
7d716cc8
module
Gitlab
module
Gitlab
module
Metrics
module
Metrics
module
InfluxDb
module
InfluxDb
extend
ActiveSupport
::
Concern
include
Gitlab
::
Metrics
::
Concern
include
Gitlab
::
Metrics
::
Concern
include
Gitlab
::
CurrentSettings
extend
self
MUTEX
=
Mutex
.
new
private_constant
:MUTEX
def
influx_metrics_enabled?
settings
[
:enabled
]
||
false
end
# Prometheus histogram buckets used for arbitrary code measurements
EXECUTION_MEASUREMENT_BUCKETS
=
[
0.001
,
0.01
,
0.1
,
1
].
freeze
EXECUTION_MEASUREMENT_BUCKETS
=
[
0.001
,
0.01
,
0.1
,
1
].
freeze
RAILS_ROOT
=
Rails
.
root
.
to_s
RAILS_ROOT
=
Rails
.
root
.
to_s
METRICS_ROOT
=
Rails
.
root
.
join
(
'lib'
,
'gitlab'
,
'metrics'
).
to_s
METRICS_ROOT
=
Rails
.
root
.
join
(
'lib'
,
'gitlab'
,
'metrics'
).
to_s
PATH_REGEX
=
/^
#{
RAILS_ROOT
}
\/?/
PATH_REGEX
=
/^
#{
RAILS_ROOT
}
\/?/
def
settings
MUTEX
=
Mutex
.
new
@settings
||=
{
private_constant
:MUTEX
enabled:
current_application_settings
[
:metrics_enabled
],
pool_size:
current_application_settings
[
:metrics_pool_size
],
timeout:
current_application_settings
[
:metrics_timeout
],
method_call_threshold:
current_application_settings
[
:metrics_method_call_threshold
],
host:
current_application_settings
[
:metrics_host
],
port:
current_application_settings
[
:metrics_port
],
sample_interval:
current_application_settings
[
:metrics_sample_interval
]
||
15
,
packet_size:
current_application_settings
[
:metrics_packet_size
]
||
1
}
end
def
mri?
class_methods
do
RUBY_ENGINE
==
'ruby'
include
Gitlab
::
CurrentSettings
end
def
method_call_threshold
def
influx_metrics_enabled?
# This is memoized since this method is called for every instrumented
settings
[
:enabled
]
||
false
# method. Loading data from an external cache on every method call slows
end
# things down too much.
# in milliseconds
@method_call_threshold
||=
settings
[
:method_call_threshold
]
end
def
submit_metrics
(
metrics
)
# Prometheus histogram buckets used for arbitrary code measurements
prepared
=
prepare_metrics
(
metrics
)
def
settings
@settings
||=
{
enabled:
current_application_settings
[
:metrics_enabled
],
pool_size:
current_application_settings
[
:metrics_pool_size
],
timeout:
current_application_settings
[
:metrics_timeout
],
method_call_threshold:
current_application_settings
[
:metrics_method_call_threshold
],
host:
current_application_settings
[
:metrics_host
],
port:
current_application_settings
[
:metrics_port
],
sample_interval:
current_application_settings
[
:metrics_sample_interval
]
||
15
,
packet_size:
current_application_settings
[
:metrics_packet_size
]
||
1
}
end
def
mri?
RUBY_ENGINE
==
'ruby'
end
def
method_call_threshold
# This is memoized since this method is called for every instrumented
# method. Loading data from an external cache on every method call slows
# things down too much.
# in milliseconds
@method_call_threshold
||=
settings
[
:method_call_threshold
]
end
pool
&
.
with
do
|
connection
|
def
submit_metrics
(
metrics
)
prepared
.
each_slice
(
settings
[
:packet_size
])
do
|
slice
|
prepared
=
prepare_metrics
(
metrics
)
begin
connection
.
write_points
(
slice
)
pool
&
.
with
do
|
connection
|
rescue
StandardError
prepared
.
each_slice
(
settings
[
:packet_size
])
do
|
slice
|
begin
connection
.
write_points
(
slice
)
rescue
StandardError
end
end
end
end
end
rescue
Errno
::
EADDRNOTAVAIL
,
SocketError
=>
ex
Gitlab
::
EnvironmentLogger
.
error
(
'Cannot resolve InfluxDB address. GitLab Performance Monitoring will not work.'
)
Gitlab
::
EnvironmentLogger
.
error
(
ex
)
end
end
rescue
Errno
::
EADDRNOTAVAIL
,
SocketError
=>
ex
Gitlab
::
EnvironmentLogger
.
error
(
'Cannot resolve InfluxDB address. GitLab Performance Monitoring will not work.'
)
Gitlab
::
EnvironmentLogger
.
error
(
ex
)
end
def
prepare_metrics
(
metrics
)
def
prepare_metrics
(
metrics
)
metrics
.
map
do
|
hash
|
metrics
.
map
do
|
hash
|
new_hash
=
hash
.
symbolize_keys
new_hash
=
hash
.
symbolize_keys
new_hash
[
:tags
].
each
do
|
key
,
value
|
new_hash
[
:tags
].
each
do
|
key
,
value
|
if
value
.
blank?
if
value
.
blank?
new_hash
[
:tags
].
delete
(
key
)
new_hash
[
:tags
].
delete
(
key
)
else
else
new_hash
[
:tags
][
key
]
=
escape_value
(
value
)
new_hash
[
:tags
][
key
]
=
escape_value
(
value
)
end
end
end
end
new_hash
new_hash
end
end
end
end
def
escape_value
(
value
)
def
escape_value
(
value
)
value
.
to_s
.
gsub
(
'='
,
'\\='
)
value
.
to_s
.
gsub
(
'='
,
'\\='
)
end
# Measures the execution time of a block.
#
# Example:
#
# Gitlab::Metrics.measure(:find_by_username_duration) do
# User.find_by_username(some_username)
# end
#
# name - The name of the field to store the execution time in.
#
# Returns the value yielded by the supplied block.
def
measure
(
name
)
trans
=
current_transaction
return
yield
unless
trans
real_start
=
Time
.
now
.
to_f
cpu_start
=
System
.
cpu_time
retval
=
yield
cpu_stop
=
System
.
cpu_time
real_stop
=
Time
.
now
.
to_f
real_time
=
(
real_stop
-
real_start
)
cpu_time
=
cpu_stop
-
cpu_start
real_duration_seconds
=
fetch_histogram
(
"gitlab_
#{
name
}
_real_duration_seconds"
.
to_sym
)
do
docstring
"Measure
#{
name
}
"
base_labels
Transaction
::
BASE_LABELS
buckets
EXECUTION_MEASUREMENT_BUCKETS
end
end
real_duration_seconds
.
observe
(
trans
.
labels
,
real_time
)
# Measures the execution time of a block.
#
# Example:
#
# Gitlab::Metrics.measure(:find_by_username_duration) do
# User.find_by_username(some_username)
# end
#
# name - The name of the field to store the execution time in.
#
# Returns the value yielded by the supplied block.
def
measure
(
name
)
trans
=
current_transaction
return
yield
unless
trans
real_start
=
Time
.
now
.
to_f
cpu_start
=
System
.
cpu_time
retval
=
yield
cpu_stop
=
System
.
cpu_time
real_stop
=
Time
.
now
.
to_f
real_time
=
(
real_stop
-
real_start
)
cpu_time
=
cpu_stop
-
cpu_start
real_duration_seconds
=
fetch_histogram
(
"gitlab_
#{
name
}
_real_duration_seconds"
.
to_sym
)
do
docstring
"Measure
#{
name
}
"
base_labels
Transaction
::
BASE_LABELS
buckets
EXECUTION_MEASUREMENT_BUCKETS
end
cpu_duration_seconds
=
fetch_histogram
(
"gitlab_
#{
name
}
_cpu_duration_seconds"
.
to_sym
)
do
real_duration_seconds
.
observe
(
trans
.
labels
,
real_time
)
docstring
"Measure
#{
name
}
"
base_labels
Transaction
::
BASE_LABELS
buckets
EXECUTION_MEASUREMENT_BUCKETS
# with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
end
cpu_duration_seconds
.
observe
(
trans
.
labels
,
cpu_time
)
# InfluxDB stores the _real_time and _cpu_time time values as milliseconds
cpu_duration_seconds
=
fetch_histogram
(
"gitlab_
#{
name
}
_cpu_duration_seconds"
.
to_sym
)
do
trans
.
increment
(
"
#{
name
}
_real_time"
,
real_time
.
in_milliseconds
,
false
)
docstring
"Measure
#{
name
}
"
trans
.
increment
(
"
#{
name
}
_cpu_time"
,
cpu_time
.
in_milliseconds
,
false
)
base_labels
Transaction
::
BASE_LABELS
trans
.
increment
(
"
#{
name
}
_call_count"
,
1
,
false
)
buckets
EXECUTION_MEASUREMENT_BUCKETS
# with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
end
cpu_duration_seconds
.
observe
(
trans
.
labels
,
cpu_time
)
retval
# InfluxDB stores the _real_time and _cpu_time time values as milliseconds
end
trans
.
increment
(
"
#{
name
}
_real_time"
,
real_time
.
in_milliseconds
,
false
)
trans
.
increment
(
"
#{
name
}
_cpu_time"
,
cpu_time
.
in_milliseconds
,
false
)
trans
.
increment
(
"
#{
name
}
_call_count"
,
1
,
false
)
# Sets the action of the current transaction (if any)
retval
#
end
# action - The name of the action.
def
action
=
(
action
)
trans
=
current_transaction
trans
&
.
action
=
action
# Sets the action of the current transaction (if any)
end
#
# action - The name of the action.
def
action
=
(
action
)
trans
=
current_transaction
# Tracks an event.
trans
&
.
action
=
action
#
end
# See `Gitlab::Metrics::Transaction#add_event` for more details.
def
add_event
(
*
args
)
trans
=
current_transaction
trans
&
.
add_event
(
*
args
)
# Tracks an event.
end
#
# See `Gitlab::Metrics::Transaction#add_event` for more details.
def
add_event
(
*
args
)
trans
=
current_transaction
# Returns the prefix to use for the name of a series.
trans
&
.
add_event
(
*
args
)
def
series_prefix
end
@series_prefix
||=
Sidekiq
.
server?
?
'sidekiq_'
:
'rails_'
end
# Allow access from other metrics related middlewares
# Returns the prefix to use for the name of a series.
def
current_transaction
def
series_prefix
Transaction
.
current
@series_prefix
||=
Sidekiq
.
server?
?
'sidekiq_'
:
'rails_'
end
end
# Allow access from other metrics related middlewares
def
current_transaction
Transaction
.
current
end
# When enabled this should be set before being used as the usual pattern
# When enabled this should be set before being used as the usual pattern
# "@foo ||= bar" is _not_ thread-safe.
# "@foo ||= bar" is _not_ thread-safe.
# rubocop:disable Gitlab/ModuleWithInstanceVariables
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def
pool
def
pool
if
influx_metrics_enabled?
if
influx_metrics_enabled?
if
@pool
.
nil?
if
@pool
.
nil?
MUTEX
.
synchronize
do
MUTEX
.
synchronize
do
@pool
||=
ConnectionPool
.
new
(
size:
settings
[
:pool_size
],
timeout:
settings
[
:timeout
])
do
@pool
||=
ConnectionPool
.
new
(
size:
settings
[
:pool_size
],
timeout:
settings
[
:timeout
])
do
host
=
settings
[
:host
]
host
=
settings
[
:host
]
port
=
settings
[
:port
]
port
=
settings
[
:port
]
InfluxDB
::
Client
InfluxDB
::
Client
.
new
(
udp:
{
host:
host
,
port:
port
})
.
new
(
udp:
{
host:
host
,
port:
port
})
end
end
end
end
end
end
@pool
@pool
end
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
end
end
end
end
end
end
lib/gitlab/metrics/prometheus.rb
View file @
7d716cc8
...
@@ -4,13 +4,12 @@ module Gitlab
...
@@ -4,13 +4,12 @@ module Gitlab
module
Metrics
module
Metrics
module
Prometheus
module
Prometheus
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
include
Gitlab
::
Metrics
::
Concern
include
Gitlab
::
CurrentSettings
REGISTRY_MUTEX
=
Mutex
.
new
REGISTRY_MUTEX
=
Mutex
.
new
PROVIDER_MUTEX
=
Mutex
.
new
PROVIDER_MUTEX
=
Mutex
.
new
class_methods
do
class_methods
do
include
Gitlab
::
CurrentSettings
include
Gitlab
::
Utils
::
StrongMemoize
include
Gitlab
::
Utils
::
StrongMemoize
def
metrics_folder_present?
def
metrics_folder_present?
...
...
spec/lib/gitlab/metrics/subscribers/rails_cache_spec.rb
View file @
7d716cc8
...
@@ -144,7 +144,10 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
...
@@ -144,7 +144,10 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
end
end
context
'with a transaction'
do
context
'with a transaction'
do
let
(
:metric_cache_misses_total
)
{
double
(
'metric_cache_misses_total'
,
increment:
nil
)
}
before
do
before
do
allow
(
subscriber
).
to
receive
(
:metric_cache_misses_total
).
and_return
(
metric_cache_misses_total
)
allow
(
subscriber
).
to
receive
(
:current_transaction
)
allow
(
subscriber
).
to
receive
(
:current_transaction
)
.
and_return
(
transaction
)
.
and_return
(
transaction
)
end
end
...
@@ -157,9 +160,9 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
...
@@ -157,9 +160,9 @@ describe Gitlab::Metrics::Subscribers::RailsCache do
end
end
it
'increments the cache_read_miss total'
do
it
'increments the cache_read_miss total'
do
expect
(
subscriber
.
send
(
:metric_cache_misses_total
)).
to
receive
(
:increment
).
with
({})
subscriber
.
cache_generate
(
event
)
subscriber
.
cache_generate
(
event
)
expect
(
metric_cache_misses_total
).
to
have_received
(
:increment
).
with
({})
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