Class: Rubino::API::Middleware::Observability

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/api/middleware/observability.rb

Overview

Outermost middleware. Wraps every request to:

- record http_requests_total{method,path,status} + http_request_duration_seconds
- emit one JSON log line (event="api.request") with method, path, status, duration_ms

Status comes from the response tuple after ErrorHandler has done its mapping; on a fully unhandled raise we still record status=500 and re-raise so Puma can render whatever it wants. The path metric label uses env (the matched pattern) when present, to keep Prometheus label cardinality bounded.

Instance Method Summary collapse

Constructor Details

#initialize(app, logger: nil) ⇒ Observability

Returns a new instance of Observability.



16
17
18
19
# File 'lib/rubino/api/middleware/observability.rb', line 16

def initialize(app, logger: nil)
  @app = app
  @logger = logger || Rubino.logger
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rubino/api/middleware/observability.rb', line 21

def call(env)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  status, headers, body = @app.call(env)
  observe(env, status, start)
  [status, headers, body]
rescue StandardError
  observe(env, 500, start)
  raise
end