Last active
August 29, 2015 14:19
-
-
Save timonv/ce25357008f5e676637b to your computer and use it in GitHub Desktop.
Easily measure methods with Appsignal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def self.measure(method, category_name=nil) | |
alias_method "#{method}_unmeasured", method | |
define_method method do |*args| | |
cat_name = if category_name.respond_to?(:call) | |
# If proc eval in current context and pass args | |
self.instance_exec args, &category_name | |
elsif category_name && category_name.respond_to?(:to_s) | |
# Stringify that thing you gave me | |
category_name.to_s | |
else | |
# Try to be smart | |
args.first.try(:to_s) | |
end | |
ActiveSupport::Notifications.instrument( | |
"#{self.class.name}.#{method.to_s}", | |
:name => cat_name | |
) do | |
send "#{method}_unmeasured", *args | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment