Class: Attio::Observability::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/observability.rb

Overview

Middleware for automatic instrumentation

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(app, instrumentation) ⇒ Middleware

Returns a new instance of Middleware.

Since:

  • 1.0.0



391
392
393
394
# File 'lib/attio/observability.rb', line 391

def initialize(app, instrumentation)
  @app = app
  @instrumentation = instrumentation
end

Instance Method Details

#call(env) ⇒ Object

Since:

  • 1.0.0



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/attio/observability.rb', line 396

def call(env)
  start_time = Time.now
  method = env[:method]
  path = env[:url].path

  begin
    response = @app.call(env)

    @instrumentation.record_api_call(
      method: method,
      path: path,
      duration: Time.now - start_time,
      status: response.status
    )

    response
  rescue StandardError => e
    @instrumentation.record_api_call(
      method: method,
      path: path,
      duration: Time.now - start_time,
      error: e
    )
    raise
  end
end