Class: Dontbugme::Middleware::Rack

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack



6
7
8
# File 'lib/dontbugme/middleware/rack.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
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
# File 'lib/dontbugme/middleware/rack.rb', line 10

def call(env)
  return @app.call(env) unless Dontbugme.config.recording?
  return @app.call(env) unless Dontbugme.config.should_record_request?(env)

  request = ::Rack::Request.new(env)
  path = request.path.to_s
  mount_path = Dontbugme.config.web_ui_mount_path.to_s.chomp('/')
  return @app.call(env) if mount_path.to_s != '' && (path == mount_path || path.start_with?("#{mount_path}/"))

  request_id = env['action_dispatch.request_id'] || request.get_header('HTTP_X_REQUEST_ID') || SecureRandom.uuid
  correlation_id = env['HTTP_X_CORRELATION_ID'] || Correlation.generate
  Correlation.current = correlation_id

  method = request.request_method
  identifier = "#{method} #{path}"

   = {
    request_id: request_id,
    correlation_id: correlation_id,
    method: method,
    path: path
  }

  Recorder.record(kind: :request, identifier: identifier, metadata: ) do
    @app.call(env)
  end
ensure
  Correlation.clear!
end