Skip to content

Commit

Permalink
Merge pull request #163 from appsignal/instrument-method-blocks
Browse files Browse the repository at this point in the history
Support blocks arguments on Method instrumentation
  • Loading branch information
thijsc committed Sep 22, 2016
2 parents 56525d1 + d04c3db commit 74e7f2b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/appsignal/integrations/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ class Object
def self.appsignal_instrument_class_method(method_name, options = {})
singleton_class.send \
:alias_method, "appsignal_uninstrumented_#{method_name}", method_name
singleton_class.send(:define_method, method_name) do |*args|
singleton_class.send(:define_method, method_name) do |*args, &block|
name = options.fetch(:name) do
"#{method_name}.class_method.#{appsignal_reverse_class_name}.other"
end
Appsignal.instrument name do
send "appsignal_uninstrumented_#{method_name}", *args
send "appsignal_uninstrumented_#{method_name}", *args, &block
end
end
end

def self.appsignal_instrument_method(method_name, options = {})
alias_method "appsignal_uninstrumented_#{method_name}", method_name
define_method method_name do |*args|
define_method method_name do |*args, &block|
name = options.fetch(:name) do
"#{method_name}.#{appsignal_reverse_class_name}.other"
end
Appsignal.instrument name do
send "appsignal_uninstrumented_#{method_name}", *args
send "appsignal_uninstrumented_#{method_name}", *args, &block
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/appsignal/integrations/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def foo
expect(instance.foo).to eq(1)
end
end

context "with a method given a block" do
let(:klass) do
Class.new do
def foo
yield
end
appsignal_instrument_method :foo
end
end

it "should yield the block" do
expect(instance.foo { 42 }).to eq(42)
end
end
end

context "when not active" do
Expand Down Expand Up @@ -194,6 +209,21 @@ def self.bar
expect(klass.bar).to eq(2)
end
end

context "with a method given a block" do
let(:klass) do
Class.new do
def self.bar
yield
end
appsignal_instrument_class_method :bar
end
end

it "should yield the block" do
expect(klass.bar { 42 }).to eq(42)
end
end
end

context "when not active" do
Expand Down

0 comments on commit 74e7f2b

Please sign in to comment.