Class: Salopulse::Instrumentation::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/salopulse/instrumentation/rack_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, client = Salopulse::Client.instance) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



7
8
9
10
# File 'lib/salopulse/instrumentation/rack_middleware.rb', line 7

def initialize(app, client = Salopulse::Client.instance)
  @app = app
  @client = client
end

Instance Method Details

#call(env) ⇒ Object



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
39
40
41
42
43
44
# File 'lib/salopulse/instrumentation/rack_middleware.rb', line 12

def call(env)
  return @app.call(env) if @client.disabled?

  endpoint = derive_endpoint(env)
  http_method = env["REQUEST_METHOD"]
  Salopulse::RequestContext.start(endpoint: endpoint, http_method: http_method)

  status = 500
  begin
    status, headers, body = @app.call(env)
    [status, headers, body]
  rescue Exception => e # rubocop:disable Lint/RescueException
    @client.capture_exception(e, environment_data: env_snapshot(env))
    raise
  ensure
    begin
      snapshot = env_snapshot(env)
      @client.capture_performance(
        endpoint: endpoint,
        http_method: http_method,
        duration_ms: Salopulse::RequestContext.elapsed_ms,
        status_code: status,
        ip: snapshot["ip"],
        request_headers: snapshot["headers"],
        request_params: snapshot["params"]
      )
      @client.flush_request_scope_events
    rescue StandardError
      # never let SDK errors propagate
    end
    Salopulse::RequestContext.clear
  end
end