Commit 5dbcb635 authored by Yorick Peterse's avatar Yorick Peterse

Methods for instrumenting multiple methods

The methods Instrumentation.instrument_methods and
Instrumentation.instrument_instance_methods can be used to instrument
all methods of a module at once.
parent ad69ba57
...@@ -27,6 +27,29 @@ module Gitlab ...@@ -27,6 +27,29 @@ module Gitlab
instrument(:instance, mod, name) instrument(:instance, mod, name)
end end
# Instruments all public methods of a module.
#
# mod - The module to instrument.
def self.instrument_methods(mod)
mod.public_methods(false).each do |name|
instrument_method(mod, name)
end
end
# Instruments all public instance methods of a module.
#
# mod - The module to instrument.
def self.instrument_instance_methods(mod)
mod.public_instance_methods(false).each do |name|
instrument_instance_method(mod, name)
end
end
# 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) def self.instrument(type, mod, name)
return unless Metrics.enabled? return unless Metrics.enabled?
......
...@@ -34,7 +34,7 @@ describe Gitlab::Metrics::Instrumentation do ...@@ -34,7 +34,7 @@ describe Gitlab::Metrics::Instrumentation do
end end
it 'tracks the call duration upon calling the method' do it 'tracks the call duration upon calling the method' do
allow(Gitlab::Metrics::Instrumentation).to receive(:transaction). allow(described_class).to receive(:transaction).
and_return(transaction) and_return(transaction)
expect(transaction).to receive(:add_metric). expect(transaction).to receive(:add_metric).
...@@ -51,7 +51,7 @@ describe Gitlab::Metrics::Instrumentation do ...@@ -51,7 +51,7 @@ describe Gitlab::Metrics::Instrumentation do
end end
it 'does not instrument the method' do it 'does not instrument the method' do
Gitlab::Metrics::Instrumentation.instrument_method(@dummy, :foo) described_class.instrument_method(@dummy, :foo)
expect(@dummy).to_not respond_to(:_original_foo) expect(@dummy).to_not respond_to(:_original_foo)
end end
...@@ -63,7 +63,7 @@ describe Gitlab::Metrics::Instrumentation do ...@@ -63,7 +63,7 @@ describe Gitlab::Metrics::Instrumentation do
before do before do
allow(Gitlab::Metrics).to receive(:enabled?).and_return(true) allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)
Gitlab::Metrics::Instrumentation. described_class.
instrument_instance_method(@dummy, :bar) instrument_instance_method(@dummy, :bar)
end end
...@@ -76,7 +76,7 @@ describe Gitlab::Metrics::Instrumentation do ...@@ -76,7 +76,7 @@ describe Gitlab::Metrics::Instrumentation do
end end
it 'tracks the call duration upon calling the method' do it 'tracks the call duration upon calling the method' do
allow(Gitlab::Metrics::Instrumentation).to receive(:transaction). allow(described_class).to receive(:transaction).
and_return(transaction) and_return(transaction)
expect(transaction).to receive(:add_metric). expect(transaction).to receive(:add_metric).
...@@ -93,11 +93,35 @@ describe Gitlab::Metrics::Instrumentation do ...@@ -93,11 +93,35 @@ describe Gitlab::Metrics::Instrumentation do
end end
it 'does not instrument the method' do it 'does not instrument the method' do
Gitlab::Metrics::Instrumentation. described_class.
instrument_instance_method(@dummy, :bar) instrument_instance_method(@dummy, :bar)
expect(@dummy.method_defined?(:_original_bar)).to eq(false) expect(@dummy.method_defined?(:_original_bar)).to eq(false)
end end
end end
end end
describe '.instrument_methods' do
before do
allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)
end
it 'instruments all public class methods' do
described_class.instrument_methods(@dummy)
expect(@dummy).to respond_to(:_original_foo)
end
end
describe '.instrument_instance_methods' do
before do
allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)
end
it 'instruments all public instance methods' do
described_class.instrument_instance_methods(@dummy)
expect(@dummy.method_defined?(:_original_bar)).to eq(true)
end
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