Class: EventTimeline::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/event_timeline/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/event_timeline/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/event_timeline/middleware.rb', line 9

def call(env)
  request = ActionDispatch::Request.new(env)
  correlation_id = request.request_id
  CurrentCorrelation.id = correlation_id
  Thread.current[:request_id] = correlation_id

  @app.call(env)
rescue Exception => e # rubocop:disable Lint/RescueException
  # Capture the exception before re-raising
  CallTracker.record_exception(e, correlation_id) if correlation_id
  raise
ensure
  # Flush buffered events to database
  CallTracker.flush_events(correlation_id) if correlation_id

  # Clean up thread-local state
  CallTracker.cleanup_thread_state(correlation_id) if correlation_id
  CurrentCorrelation.reset
  Thread.current[:request_id] = nil
end