Skip to content

Instantly share code, notes, and snippets.

@oestrich
Created November 14, 2012 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oestrich/4072523 to your computer and use it in GitHub Desktop.
Save oestrich/4072523 to your computer and use it in GitHub Desktop.
Instrument every method in a class for metrics
ActiveSupport::Notifications.subscribe(/my_class$/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
Rails.logger.warn "%7.2fms %s" % [event.duration, event.name]
end
module Notifications
extend ActiveSupport::Concern
module ClassMethods
def method_added(method_name)
@methods ||= []
return if @methods.include?(method_name) || method_name =~ /_old$/
@methods << method_name
class_eval %{alias #{method_name}_old #{method_name}}
define_method(method_name) do |*args|
ActiveSupport::Notifications.instrument("#{method_name}.#{self.class.name.underscore}") do
send("#{method_name}_old", *args)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment