2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Module for instrumenting methods.
|
|
|
|
#
|
|
|
|
# This module allows instrumenting of methods without having to actually
|
|
|
|
# alter the target code (e.g. by including modules).
|
|
|
|
#
|
|
|
|
# Example usage:
|
|
|
|
#
|
|
|
|
# Gitlab::Metrics::Instrumentation.instrument_method(User, :by_login)
|
|
|
|
module Instrumentation
|
2016-04-15 12:31:01 -04:00
|
|
|
PROXY_IVAR = :@__gitlab_instrumentation_proxy
|
|
|
|
|
2015-12-10 10:16:48 -05:00
|
|
|
def self.configure
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
2016-07-28 09:08:57 -04:00
|
|
|
# Returns the name of the series to use for storing method calls.
|
|
|
|
def self.series
|
2019-02-14 13:05:35 -05:00
|
|
|
@series ||= "#{::Gitlab::Metrics.series_prefix}method_calls"
|
2016-07-28 09:08:57 -04:00
|
|
|
end
|
|
|
|
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
# Instruments a class method.
|
|
|
|
#
|
|
|
|
# mod - The module to instrument as a Module/Class.
|
|
|
|
# name - The name of the method to instrument.
|
|
|
|
def self.instrument_method(mod, name)
|
|
|
|
instrument(:class, mod, name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Instruments an instance method.
|
|
|
|
#
|
|
|
|
# mod - The module to instrument as a Module/Class.
|
|
|
|
# name - The name of the method to instrument.
|
|
|
|
def self.instrument_instance_method(mod, name)
|
|
|
|
instrument(:instance, mod, name)
|
|
|
|
end
|
|
|
|
|
2015-12-15 11:22:46 -05:00
|
|
|
# Recursively instruments all subclasses of the given root module.
|
|
|
|
#
|
|
|
|
# This can be used to for example instrument all ActiveRecord models (as
|
|
|
|
# these all inherit from ActiveRecord::Base).
|
|
|
|
#
|
|
|
|
# This method can optionally take a block to pass to `instrument_methods`
|
|
|
|
# and `instrument_instance_methods`.
|
|
|
|
#
|
|
|
|
# root - The root module for which to instrument subclasses. The root
|
|
|
|
# module itself is not instrumented.
|
|
|
|
def self.instrument_class_hierarchy(root, &block)
|
|
|
|
visit = root.subclasses
|
|
|
|
|
|
|
|
until visit.empty?
|
|
|
|
klass = visit.pop
|
|
|
|
|
|
|
|
instrument_methods(klass, &block)
|
|
|
|
instrument_instance_methods(klass, &block)
|
|
|
|
|
|
|
|
klass.subclasses.each { |c| visit << c }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-13 12:49:21 -04:00
|
|
|
# Instruments all public and private methods of a module.
|
2015-12-10 10:15:46 -05:00
|
|
|
#
|
2015-12-15 10:31:24 -05:00
|
|
|
# This method optionally takes a block that can be used to determine if a
|
|
|
|
# method should be instrumented or not. The block is passed the receiving
|
|
|
|
# module and an UnboundMethod. If the block returns a non truthy value the
|
|
|
|
# method is not instrumented.
|
|
|
|
#
|
2015-12-10 10:15:46 -05:00
|
|
|
# mod - The module to instrument.
|
|
|
|
def self.instrument_methods(mod)
|
2016-06-13 12:49:21 -04:00
|
|
|
methods = mod.methods(false) + mod.private_methods(false)
|
|
|
|
methods.each do |name|
|
2015-12-10 10:25:28 -05:00
|
|
|
method = mod.method(name)
|
|
|
|
|
2015-12-15 10:31:24 -05:00
|
|
|
if method.owner == mod.singleton_class
|
|
|
|
if !block_given? || block_given? && yield(mod, method)
|
|
|
|
instrument_method(mod, name)
|
|
|
|
end
|
|
|
|
end
|
2015-12-10 10:15:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-13 12:49:21 -04:00
|
|
|
# Instruments all public and private instance methods of a module.
|
2015-12-10 10:15:46 -05:00
|
|
|
#
|
2015-12-15 10:31:24 -05:00
|
|
|
# See `instrument_methods` for more information.
|
|
|
|
#
|
2015-12-10 10:15:46 -05:00
|
|
|
# mod - The module to instrument.
|
|
|
|
def self.instrument_instance_methods(mod)
|
2016-06-13 12:49:21 -04:00
|
|
|
methods = mod.instance_methods(false) + mod.private_instance_methods(false)
|
|
|
|
methods.each do |name|
|
2015-12-10 10:25:28 -05:00
|
|
|
method = mod.instance_method(name)
|
|
|
|
|
2015-12-15 10:31:24 -05:00
|
|
|
if method.owner == mod
|
|
|
|
if !block_given? || block_given? && yield(mod, method)
|
|
|
|
instrument_instance_method(mod, name)
|
|
|
|
end
|
|
|
|
end
|
2015-12-10 10:15:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 12:31:01 -04:00
|
|
|
# Returns true if a module is instrumented.
|
|
|
|
#
|
|
|
|
# mod - The module to check
|
|
|
|
def self.instrumented?(mod)
|
|
|
|
mod.instance_variable_defined?(PROXY_IVAR)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the proxy module (if any) of `mod`.
|
|
|
|
def self.proxy_module(mod)
|
|
|
|
mod.instance_variable_get(PROXY_IVAR)
|
|
|
|
end
|
|
|
|
|
2015-12-10 10:15:46 -05:00
|
|
|
# Instruments a method.
|
|
|
|
#
|
|
|
|
# type - The type (:class or :instance) of method to instrument.
|
|
|
|
# mod - The module containing the method.
|
|
|
|
# name - The name of the method to instrument.
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
def self.instrument(type, mod, name)
|
2019-02-14 13:05:35 -05:00
|
|
|
return unless ::Gitlab::Metrics.enabled?
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
|
2015-12-10 09:16:56 -05:00
|
|
|
if type == :instance
|
|
|
|
target = mod
|
2017-09-06 08:02:42 -04:00
|
|
|
method_name = "##{name}"
|
2016-01-25 15:26:49 -05:00
|
|
|
method = mod.instance_method(name)
|
2015-12-10 09:16:56 -05:00
|
|
|
else
|
|
|
|
target = mod.singleton_class
|
2017-09-06 08:02:42 -04:00
|
|
|
method_name = ".#{name}"
|
2016-01-25 15:26:49 -05:00
|
|
|
method = mod.method(name)
|
|
|
|
end
|
|
|
|
|
2017-09-06 08:02:42 -04:00
|
|
|
label = "#{mod.name}#{method_name}"
|
|
|
|
|
2016-04-15 12:31:01 -04:00
|
|
|
unless instrumented?(target)
|
|
|
|
target.instance_variable_set(PROXY_IVAR, Module.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
proxy_module = self.proxy_module(target)
|
|
|
|
|
2016-01-25 15:26:49 -05:00
|
|
|
# Some code out there (e.g. the "state_machine" Gem) checks the arity of
|
|
|
|
# a method to make sure it only passes arguments when the method expects
|
|
|
|
# any. If we were to always overwrite a method to take an `*args`
|
|
|
|
# signature this would break things. As a result we'll make sure the
|
|
|
|
# generated method _only_ accepts regular arguments if the underlying
|
|
|
|
# method also accepts them.
|
2017-02-22 10:10:32 -05:00
|
|
|
args_signature =
|
|
|
|
if method.arity == 0
|
|
|
|
''
|
|
|
|
else
|
|
|
|
'*args'
|
|
|
|
end
|
2015-12-10 09:16:56 -05:00
|
|
|
|
2020-08-21 11:10:03 -04:00
|
|
|
method_visibility = method_visibility_for(target, name)
|
|
|
|
|
2016-04-15 12:31:01 -04:00
|
|
|
proxy_module.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
2016-01-25 15:26:49 -05:00
|
|
|
def #{name}(#{args_signature})
|
2016-06-17 11:45:37 -04:00
|
|
|
if trans = Gitlab::Metrics::Instrumentation.transaction
|
2017-09-06 08:02:42 -04:00
|
|
|
trans.method_call_for(#{label.to_sym.inspect}, #{mod.name.inspect}, "#{method_name}")
|
2017-09-06 07:59:21 -04:00
|
|
|
.measure { super }
|
2015-12-10 09:16:56 -05:00
|
|
|
else
|
2016-04-15 12:31:01 -04:00
|
|
|
super
|
2015-12-10 09:16:56 -05:00
|
|
|
end
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
end
|
2020-08-21 11:10:03 -04:00
|
|
|
#{method_visibility} :#{name}
|
2015-12-10 07:38:45 -05:00
|
|
|
EOF
|
2016-04-15 12:31:01 -04:00
|
|
|
|
|
|
|
target.prepend(proxy_module)
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
end
|
2015-12-10 09:16:56 -05:00
|
|
|
|
2020-08-21 11:10:03 -04:00
|
|
|
def self.method_visibility_for(mod, name)
|
|
|
|
if mod.private_method_defined?(name)
|
|
|
|
:private
|
|
|
|
elsif mod.protected_method_defined?(name)
|
|
|
|
:protected
|
|
|
|
else
|
|
|
|
:public
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_class_method :method_visibility_for
|
|
|
|
|
2015-12-10 09:16:56 -05:00
|
|
|
# Small layer of indirection to make it easier to stub out the current
|
|
|
|
# transaction.
|
|
|
|
def self.transaction
|
|
|
|
Transaction.current
|
|
|
|
end
|
Storing of application metrics in InfluxDB
This adds the ability to write application metrics (e.g. SQL timings) to
InfluxDB. These metrics can in turn be visualized using Grafana, or
really anything else that can read from InfluxDB. These metrics can be
used to track application performance over time, between different Ruby
versions, different GitLab versions, etc.
== Transaction Metrics
Currently the following is tracked on a per transaction basis (a
transaction is a Rails request or a single Sidekiq job):
* Timings per query along with the raw (obfuscated) SQL and information
about what file the query originated from.
* Timings per view along with the path of the view and information about
what file triggered the rendering process.
* The duration of a request itself along with the controller/worker
class and method name.
* The duration of any instrumented method calls (more below).
== Sampled Metrics
Certain metrics can't be directly associated with a transaction. For
example, a process' total memory usage is unrelated to any running
transactions. While a transaction can result in the memory usage going
up there's no accurate way to determine what transaction is to blame,
this becomes especially problematic in multi-threaded environments.
To solve this problem there's a separate thread that takes samples at a
fixed interval. This thread (using the class Gitlab::Metrics::Sampler)
currently tracks the following:
* The process' total memory usage.
* The number of file descriptors opened by the process.
* The amount of Ruby objects (using ObjectSpace.count_objects).
* GC statistics such as timings, heap slots, etc.
The default/current interval is 15 seconds, any smaller interval might
put too much pressure on InfluxDB (especially when running dozens of
processes).
== Method Instrumentation
While currently not yet used methods can be instrumented to track how
long they take to run. Unlike the likes of New Relic this doesn't
require modifying the source code (e.g. including modules), it all
happens from the outside. For example, to track `User.by_login` we'd add
the following code somewhere in an initializer:
Gitlab::Metrics::Instrumentation.
instrument_method(User, :by_login)
to instead instrument an instance method:
Gitlab::Metrics::Instrumentation.
instrument_instance_method(User, :save)
Instrumentation for either all public model methods or a few crucial
ones will be added in the near future, I simply haven't gotten to doing
so just yet.
== Configuration
By default metrics are disabled. This means users don't have to bother
setting anything up if they don't want to. Metrics can be enabled by
editing one's gitlab.yml configuration file (see
config/gitlab.yml.example for example settings).
== Writing Data To InfluxDB
Because InfluxDB is still a fairly young product I expect the worse.
Data loss, unexpected reboots, the database not responding, you name it.
Because of this data is _not_ written to InfluxDB directly, instead it's
queued and processed by Sidekiq. This ensures that users won't notice
anything when InfluxDB is giving trouble.
The metrics worker can be started in a standalone manner as following:
bundle exec sidekiq -q metrics
The corresponding class is called MetricsWorker.
2015-12-09 10:45:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|