2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
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-06-02 11:05:42 +05:30
|
|
|
PROXY_IVAR = :@__gitlab_instrumentation_proxy
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def self.configure
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# Returns the name of the series to use for storing method calls.
|
|
|
|
def self.series
|
2019-05-18 00:54:41 +05:30
|
|
|
@series ||= "#{::Gitlab::Metrics.series_prefix}method_calls"
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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-16 23:09:34 +05:30
|
|
|
# Instruments all public and private methods of a module.
|
2016-01-14 18:37:52 +05:30
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# mod - The module to instrument.
|
|
|
|
def self.instrument_methods(mod)
|
2016-06-16 23:09:34 +05:30
|
|
|
methods = mod.methods(false) + mod.private_methods(false)
|
|
|
|
methods.each do |name|
|
2016-01-14 18:37:52 +05:30
|
|
|
method = mod.method(name)
|
|
|
|
|
|
|
|
if method.owner == mod.singleton_class
|
|
|
|
if !block_given? || block_given? && yield(mod, method)
|
|
|
|
instrument_method(mod, name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
# Instruments all public and private instance methods of a module.
|
2016-01-14 18:37:52 +05:30
|
|
|
#
|
|
|
|
# See `instrument_methods` for more information.
|
|
|
|
#
|
|
|
|
# mod - The module to instrument.
|
|
|
|
def self.instrument_instance_methods(mod)
|
2016-06-16 23:09:34 +05:30
|
|
|
methods = mod.instance_methods(false) + mod.private_instance_methods(false)
|
|
|
|
methods.each do |name|
|
2016-01-14 18:37:52 +05:30
|
|
|
method = mod.instance_method(name)
|
|
|
|
|
|
|
|
if method.owner == mod
|
|
|
|
if !block_given? || block_given? && yield(mod, method)
|
|
|
|
instrument_instance_method(mod, name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# 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
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
# 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.
|
|
|
|
def self.instrument(type, mod, name)
|
2019-05-18 00:54:41 +05:30
|
|
|
return unless ::Gitlab::Metrics.enabled?
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
name = name.to_sym
|
2016-06-02 11:05:42 +05:30
|
|
|
target = type == :instance ? mod : mod.singleton_class
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
if type == :instance
|
|
|
|
target = mod
|
2018-03-17 18:26:18 +05:30
|
|
|
method_name = "##{name}"
|
2016-02-05 20:25:01 +05:30
|
|
|
method = mod.instance_method(name)
|
2016-01-14 18:37:52 +05:30
|
|
|
else
|
|
|
|
target = mod.singleton_class
|
2018-03-17 18:26:18 +05:30
|
|
|
method_name = ".#{name}"
|
2016-02-05 20:25:01 +05:30
|
|
|
method = mod.method(name)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
label = "#{mod.name}#{method_name}"
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
unless instrumented?(target)
|
|
|
|
target.instance_variable_set(PROXY_IVAR, Module.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
proxy_module = self.proxy_module(target)
|
|
|
|
|
2016-02-05 20:25:01 +05:30
|
|
|
# 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-08-17 22:00:37 +05:30
|
|
|
args_signature =
|
|
|
|
if method.arity == 0
|
|
|
|
''
|
|
|
|
else
|
|
|
|
'*args'
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
proxy_module.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
2016-02-05 20:25:01 +05:30
|
|
|
def #{name}(#{args_signature})
|
2016-06-22 15:30:34 +05:30
|
|
|
if trans = Gitlab::Metrics::Instrumentation.transaction
|
2018-03-17 18:26:18 +05:30
|
|
|
trans.method_call_for(#{label.to_sym.inspect}, #{mod.name.inspect}, "#{method_name}")
|
|
|
|
.measure { super }
|
2016-01-14 18:37:52 +05:30
|
|
|
else
|
2016-06-02 11:05:42 +05:30
|
|
|
super
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
EOF
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
target.prepend(proxy_module)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Small layer of indirection to make it easier to stub out the current
|
|
|
|
# transaction.
|
|
|
|
def self.transaction
|
|
|
|
Transaction.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|