Created
November 14, 2012 14:45
-
-
Save oestrich/4072523 to your computer and use it in GitHub Desktop.
Instrument every method in a class for metrics
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
ActiveSupport::Notifications.subscribe(/my_class$/) do |*args| | |
event = ActiveSupport::Notifications::Event.new(*args) | |
Rails.logger.warn "%7.2fms %s" % [event.duration, event.name] | |
end |
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
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