Class: Binocs::Middleware::RequestRecorder

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestRecorder

Returns a new instance of RequestRecorder.



9
10
11
# File 'lib/binocs/middleware/request_recorder.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/binocs/middleware/request_recorder.rb', line 13

def call(env)
  return @app.call(env) unless Binocs.enabled?
  return @app.call(env) if ignored_path?(env["PATH_INFO"])

  request_id = SecureRandom.uuid
  Thread.current[:binocs_request_id] = request_id
  Thread.current[:binocs_logs] = []
  Thread.current[:binocs_start_time] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  Thread.current[:binocs_memory_before] = get_memory_usage

  request = ActionDispatch::Request.new(env)

  recorded_request = build_request_record(request, request_id)

  begin
    status, headers, response = @app.call(env)

    complete_request_record(recorded_request, status, headers, response, env)

    [status, headers, response]
  rescue Exception => e
    record_exception(recorded_request, e)
    raise
  ensure
    save_request_record(recorded_request)
    cleanup_thread_locals
  end
end