Class: Fbe::Middleware::Trace
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Fbe::Middleware::Trace
- Defined in:
- lib/fbe/middleware/trace.rb
Overview
Faraday middleware that traces all API calls.
This middleware records all HTTP requests and responses in a trace array, capturing method, URL, status, and timing information for debugging and monitoring purposes.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Zerocracy
- License
-
MIT
Instance Method Summary collapse
-
#call(env) ⇒ Faraday::Response
Processes the HTTP request and records trace information.
-
#initialize(app, trace, ignores: []) ⇒ Trace
constructor
Initializes the trace middleware.
Constructor Details
#initialize(app, trace, ignores: []) ⇒ Trace
Initializes the trace middleware.
which will be ignored
36 37 38 39 40 |
# File 'lib/fbe/middleware/trace.rb', line 36 def initialize(app, trace, ignores: []) super(app) @trace = trace @ignores = ignores end |
Instance Method Details
#call(env) ⇒ Faraday::Response
Processes the HTTP request and records trace information.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fbe/middleware/trace.rb', line 46 def call(env) entry = { method: env.method, url: env.url.to_s, started_at: Time.now } @app.call(env).on_complete do |response_env| next if !@ignores.empty? && response_env[:http_cache_trace] && (response_env[:http_cache_trace] & @ignores).size.positive? finished = Time.now duration = finished - entry[:started_at] entry[:status] = response_env.status entry[:finished_at] = finished entry[:duration] = duration @trace << entry end end |