Class: Chronos::Integrations::FaradayMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/integrations/faraday.rb

Overview

Optional Faraday middleware for outbound timing and W3C propagation.

Examples:

builder.use Chronos::Integrations::FaradayMiddleware

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ FaradayMiddleware

Returns a new instance of FaradayMiddleware.



14
15
16
17
18
# File 'lib/chronos/integrations/faraday.rb', line 14

def initialize(app, options = {})
  @app = app
  @notifier = options[:notifier] || Chronos
  @clock = options[:clock] || proc { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
end

Instance Method Details

#call(environment) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chronos/integrations/faraday.rb', line 20

def call(environment)
  started_at = @clock.call
  inject_headers(environment)
  response = @app.call(environment)
  if response.respond_to?(:on_complete)
    response.on_complete { |completed| record(completed, started_at, nil) }
  else
    record(environment, started_at, nil)
  end
  response
rescue StandardError => error
  record(environment, started_at, error) if started_at
  raise
end