Class: RailsObservatory::EventCollector

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rails_observatory/event_collector.rb

Constant Summary collapse

COLLECTOR_LIST_KEY =
:ro_collector

Instance Method Summary collapse

Constructor Details

#initializeEventCollector

Returns a new instance of EventCollector.



7
8
9
# File 'lib/rails_observatory/event_collector.rb', line 7

def initialize
  @subscriber ||= ActiveSupport::Notifications.subscribe(/\A[^!]/, self)
end

Instance Method Details

#call(event) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_observatory/event_collector.rb', line 11

def call(event)
  return if ActiveSupport::IsolatedExecutionState[COLLECTOR_LIST_KEY].blank?

  ActiveSupport::IsolatedExecutionState[COLLECTOR_LIST_KEY].each do |key|
    if (events = ActiveSupport::IsolatedExecutionState[key])
      events << event
    end
  end

end

#collect_eventsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_observatory/event_collector.rb', line 27

def collect_events
  events = []
  key = generate_collector_key
  ActiveSupport::IsolatedExecutionState[COLLECTOR_LIST_KEY] ||= []
  ActiveSupport::IsolatedExecutionState[COLLECTOR_LIST_KEY] << key
  ActiveSupport::IsolatedExecutionState[key] = events
  yield
  events
rescue Exception => e
  e.instance_variable_set(:@_trace_events, events)
  raise
ensure
  ActiveSupport::IsolatedExecutionState[COLLECTOR_LIST_KEY].delete(key)
  ActiveSupport::IsolatedExecutionState.delete(key)
end

#generate_collector_keyObject



22
23
24
# File 'lib/rails_observatory/event_collector.rb', line 22

def generate_collector_key
  "collector:#{Object.new.object_id}"
end