Skip to content
This repository
Browse code

Make it possible to have IDs per request

  • Loading branch information...
commit 03d3824d965f38d3c595330d697fb16dc9efdb9a 1 parent 654b33a
Yehuda Katz authored October 28, 2009
20  activesupport/lib/active_support/notifications.rb
@@ -45,7 +45,7 @@ module Notifications
45 45
     mattr_accessor :queue
46 46
 
47 47
     class << self
48  
-      delegate :instrument, :to => :instrumenter
  48
+      delegate :instrument, :transaction_id, :generate_id, :to => :instrumenter
49 49
 
50 50
       def instrumenter
51 51
         Thread.current[:notifications_instrumeter] ||= Instrumenter.new(publisher)
@@ -63,7 +63,18 @@ def subscribe(pattern=nil, &block)
63 63
     class Instrumenter
64 64
       def initialize(publisher)
65 65
         @publisher = publisher
66  
-        @id        = SecureRandom.hex(10)
  66
+        @id        = random_id
  67
+      end
  68
+
  69
+      def transaction
  70
+        @id, old_id = random_id, @id
  71
+        yield
  72
+      ensure
  73
+        @id = old_id
  74
+      end
  75
+
  76
+      def transaction_id
  77
+        @id
67 78
       end
68 79
 
69 80
       def instrument(name, payload={})
@@ -72,6 +83,11 @@ def instrument(name, payload={})
72 83
       ensure
73 84
         @publisher.publish(name, time, Time.now, result, @id, payload)
74 85
       end
  86
+
  87
+    private
  88
+      def random_id
  89
+        SecureRandom.hex(10)
  90
+      end
75 91
     end
76 92
 
77 93
     class Publisher

0 notes on commit 03d3824

Please sign in to comment.
Something went wrong with that request. Please try again.