Commit 641761f1 authored by Yorick Peterse's avatar Yorick Peterse

Only instrument methods defined directly

When using instrument_methods/instrument_instance_methods we only want
to instrument methods defined directly in a class, not those included
via mixins (e.g. whatever RSpec throws in during development).

In case an externally included method _has_ to be instrumented we can
still use the regular instrument_method/instrument_instance_method
methods.
parent f43f3b89
......@@ -36,7 +36,9 @@ module Gitlab
# mod - The module to instrument.
def self.instrument_methods(mod)
mod.public_methods(false).each do |name|
instrument_method(mod, name)
method = mod.method(name)
instrument_method(mod, name) if method.owner == mod.singleton_class
end
end
......@@ -45,7 +47,9 @@ module Gitlab
# mod - The module to instrument.
def self.instrument_instance_methods(mod)
mod.public_instance_methods(false).each do |name|
instrument_instance_method(mod, name)
method = mod.instance_method(name)
instrument_instance_method(mod, name) if method.owner == mod
end
end
......@@ -77,7 +81,7 @@ module Gitlab
if trans
start = Time.now
retval =
retval = __send__(#{alias_name.inspect}, *args, &block)
duration = (Time.now - start) * 1000.0
trans.add_metric(Gitlab::Metrics::Instrumentation::SERIES,
......
......@@ -119,6 +119,19 @@ describe Gitlab::Metrics::Instrumentation do
expect(@dummy).to respond_to(:_original_foo)
end
it 'only instruments methods directly defined in the module' do
mod = Module.new do
def kittens
end
end
@dummy.extend(mod)
described_class.instrument_methods(@dummy)
expect(@dummy).to_not respond_to(:_original_kittens)
end
end
describe '.instrument_instance_methods' do
......@@ -131,5 +144,18 @@ describe Gitlab::Metrics::Instrumentation do
expect(@dummy.method_defined?(:_original_bar)).to eq(true)
end
it 'only instruments methods directly defined in the module' do
mod = Module.new do
def kittens
end
end
@dummy.include(mod)
described_class.instrument_instance_methods(@dummy)
expect(@dummy.method_defined?(:_original_kittens)).to eq(false)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment