Class: ActionDispatch::ServerTiming::Subscriber

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/action_dispatch/middleware/server_timing.rb

Overview

:nodoc:

Constant Summary collapse

KEY =
:action_dispatch_server_timing_events

Instance Method Summary collapse

Constructor Details

#initializeSubscriber

Returns a new instance of Subscriber.



11
12
13
# File 'lib/action_dispatch/middleware/server_timing.rb', line 11

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#call(event) ⇒ Object



15
16
17
18
19
# File 'lib/action_dispatch/middleware/server_timing.rb', line 15

def call(event)
  if events = ActiveSupport::IsolatedExecutionState[KEY]
    events << event
  end
end

#collect_eventsObject



21
22
23
24
25
26
27
28
# File 'lib/action_dispatch/middleware/server_timing.rb', line 21

def collect_events
  events = []
  ActiveSupport::IsolatedExecutionState[KEY] = events
  yield
  events
ensure
  ActiveSupport::IsolatedExecutionState.delete(KEY)
end

#ensure_subscribedObject



30
31
32
33
34
35
36
# File 'lib/action_dispatch/middleware/server_timing.rb', line 30

def ensure_subscribed
  @mutex.synchronize do
    # Subscribe to all events, except those beginning with "!"
    # Ideally we would be more selective of what is being measured
    @subscriber ||= ActiveSupport::Notifications.subscribe(/\A[^!]/, self)
  end
end

#unsubscribeObject



38
39
40
41
42
43
# File 'lib/action_dispatch/middleware/server_timing.rb', line 38

def unsubscribe
  @mutex.synchronize do
    ActiveSupport::Notifications.unsubscribe @subscriber
    @subscriber = nil
  end
end