Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 80 lines (70 sloc) 2.422 kb
7b5225a @josevalim Abstract publishing, subscribing and instrumenting in Orchestra.
josevalim authored
1 require 'active_support/core_ext/module/delegation'
3c9a37c @josevalim Added Orchestra.
josevalim authored
2
3 module ActiveSupport
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
4 # Notifications provides an instrumentation API for Ruby. To instrument an
5 # action in Ruby you just need to do:
3c9a37c @josevalim Added Orchestra.
josevalim authored
6 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
7 # ActiveSupport::Notifications.instrument(:render, :extra => :information) do
3c9a37c @josevalim Added Orchestra.
josevalim authored
8 # render :text => "Foo"
9 # end
10 #
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
11 # You can consume those events and the information they provide by registering
f0523f7 @sikachu Rename Rails::Subscriber to Rails::LogSubscriber
sikachu authored
12 # a log subscriber. For instance, let's store all instrumented events in an array:
3c9a37c @josevalim Added Orchestra.
josevalim authored
13 #
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
14 # @events = []
15 #
c9487ed @wycats Change Event#thread_id to #transaction_id. Defaults to one "transaction"...
wycats authored
16 # ActiveSupport::Notifications.subscribe do |*args|
17 # @events << ActiveSupport::Notifications::Event.new(*args)
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
18 # end
3c9a37c @josevalim Added Orchestra.
josevalim authored
19 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
20 # ActiveSupport::Notifications.instrument(:render, :extra => :information) do
3c9a37c @josevalim Added Orchestra.
josevalim authored
21 # render :text => "Foo"
22 # end
23 #
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
24 # event = @events.first
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
25 # event.name # => :render
26 # event.duration # => 10 (in milliseconds)
27 # event.payload # => { :extra => :information }
3c9a37c @josevalim Added Orchestra.
josevalim authored
28 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
29 # When subscribing to Notifications, you can pass a pattern, to only consume
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
30 # events that match the pattern:
31 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
32 # ActiveSupport::Notifications.subscribe(/render/) do |event|
5d0f8ab @josevalim Orchestra listeners have their own queue.
josevalim authored
33 # @render_events << event
34 # end
35 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
36 # Notifications ships with a queue implementation that consumes and publish events
f0523f7 @sikachu Rename Rails::Subscriber to Rails::LogSubscriber
sikachu authored
37 # to log subscribers in a thread. You can use any queue implementation you want.
3c9a37c @josevalim Added Orchestra.
josevalim authored
38 #
2d7abe2 @josevalim Renamed Orchestra to Notifications once again [#3321 state:resolved]
josevalim authored
39 module Notifications
4f2a04c @jeremy Notifications: extract central Notifier, cordon off the internal Fanout ...
jeremy authored
40 autoload :Instrumenter, 'active_support/notifications/instrumenter'
41 autoload :Event, 'active_support/notifications/instrumenter'
42 autoload :Fanout, 'active_support/notifications/fanout'
a60bdd7 @josevalim Added queue abstraction to Orchestra.
josevalim authored
43
ba8d89c Performance optimizations to handle cases of instrumentors that are not ...
Carlhuda authored
44 @instrumenters = Hash.new { |h,k| h[k] = notifier.listening?(k) }
45
7b5225a @josevalim Abstract publishing, subscribing and instrumenting in Orchestra.
josevalim authored
46 class << self
3e02b37 @tenderlove just use an attr_accessor so we do not pay ||= on every notification cal...
tenderlove authored
47 attr_accessor :notifier
3d0579f @tenderlove speed up notification publishing by writing the delegate method
tenderlove authored
48
49 def publish(name, *args)
50 notifier.publish(name, *args)
51 end
ba8d89c Performance optimizations to handle cases of instrumentors that are not ...
Carlhuda authored
52
ff0d842 @josevalim Revert the previous three commits.
josevalim authored
53 def instrument(name, payload = {})
ba8d89c Performance optimizations to handle cases of instrumentors that are not ...
Carlhuda authored
54 if @instrumenters[name]
ff0d842 @josevalim Revert the previous three commits.
josevalim authored
55 instrumenter.instrument(name, payload) { yield payload if block_given? }
ba8d89c Performance optimizations to handle cases of instrumentors that are not ...
Carlhuda authored
56 else
ff0d842 @josevalim Revert the previous three commits.
josevalim authored
57 yield payload if block_given?
ba8d89c Performance optimizations to handle cases of instrumentors that are not ...
Carlhuda authored
58 end
59 end
60
61 def subscribe(*args, &block)
62 notifier.subscribe(*args, &block).tap do
63 @instrumenters.clear
64 end
65 end
66
67 def unsubscribe(*args)
68 notifier.unsubscribe(*args)
69 @instrumenters.clear
70 end
7b5225a @josevalim Abstract publishing, subscribing and instrumenting in Orchestra.
josevalim authored
71
74f6cce @josevalim instrumenter should be accessible from ActiveSupport::Notifications.
josevalim authored
72 def instrumenter
73 Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
74 end
7b5225a @josevalim Abstract publishing, subscribing and instrumenting in Orchestra.
josevalim authored
75 end
3e02b37 @tenderlove just use an attr_accessor so we do not pay ||= on every notification cal...
tenderlove authored
76
77 self.notifier = Fanout.new
3c9a37c @josevalim Added Orchestra.
josevalim authored
78 end
79 end
Something went wrong with that request. Please try again.