Permalink
Browse files

Creating an object to consolidate thread locals which hold the

instrumenters for the AS::Notifications module.
  • Loading branch information...
1 parent 5afaf4e commit b62c197a226d08c24f3ecf95b3ff3cfaae0a0c0b @wangjohn wangjohn committed Apr 8, 2013
Showing with 27 additions and 1 deletion.
  1. +27 −1 activesupport/lib/active_support/notifications.rb
@@ -177,7 +177,33 @@ def unsubscribe(args)
end
def instrumenter
- Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
+ InstrumentationRegistry.instrumenter_for(notifier)
+ end
+ end
+
+ # This class is a registry which holds all of the +Instrumenter+ objects
+ # in a particular thread local. To access the +Instrumenter+ object for a
+ # particular +notifier+, you can call the following method:
+ #
+ # InstrumentationRegistry.instrumenter_for(notifier)
+ #
+ # The instrumenters for multiple notifiers are held in a single instance of
+ # this class.
+ class InstrumentationRegistry # :nodoc:
+ class << self
+ delegate :instrumenter_for, to: :current
+
+ def current
+ Thread.current[:instrumentation_registry] ||= new
+ end
+ end
+
+ def initialize
+ @registry = {}
+ end
+
+ def instrumenter_for(notifier)
+ @registry[notifier] ||= Instrumenter.new(notifier)
end
end

0 comments on commit b62c197

Please sign in to comment.