Skip to content

Instantly share code, notes, and snippets.

@timonv
Last active August 29, 2015 14:19
Show Gist options
  • Save timonv/ce25357008f5e676637b to your computer and use it in GitHub Desktop.
Save timonv/ce25357008f5e676637b to your computer and use it in GitHub Desktop.
Easily measure methods with Appsignal
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