Class: ActionDispatch::ServerTiming::Subscriber

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

Overview

:nodoc:

Constant Summary collapse

KEY =
:action_dispatch_server_timing_events

Instance Method Summary collapse

Methods included from Singleton

#duplicable?

Constructor Details

#initializeSubscriber

Returns a new instance of Subscriber.



13
14
15
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 13

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#call(event) ⇒ Object



17
18
19
20
21
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 17

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

#collect_eventsObject



23
24
25
26
27
28
29
30
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 23

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

#ensure_subscribedObject



32
33
34
35
36
37
38
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 32

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



40
41
42
43
44
45
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 40

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