Class: Faraday::Request::Instrumentation

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/request/instrumentation.rb

Overview

Middleware for instrumenting Requests.

Constant Summary collapse

Options =

Options class used in Request::Instrumentation class.

Faraday::Options.new(:name, :instrumenter) do
  remove_method :name
  # @return [String]
  def name
    self[:name] ||= 'request.faraday'
  end

  remove_method :instrumenter
  # @return [Class]
  def instrumenter
    self[:instrumenter] ||= ActiveSupport::Notifications
  end
end

Constants inherited from Middleware

Middleware::DEFAULT_OPTIONS, Middleware::LOCK

Instance Attribute Summary

Attributes inherited from Middleware

#app, #options

Instance Method Summary collapse

Methods inherited from Middleware

#close, default_options, default_options=

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware, #registered_middleware, #unregister_middleware

Constructor Details

#initialize(app, options = nil) ⇒ Instrumentation

Instruments requests using Active Support.

Measures time spent only for synchronous requests.

Examples:

Using ActiveSupport::Notifications to measure time spent

for Faraday requests.
ActiveSupport::Notifications
  .subscribe('request.faraday') do |name, starts, ends, _, env|
  url = env[:url]
  http_method = env[:method].to_s.upcase
  duration = ends - starts
  $stderr.puts '[%s] %s %s (%.3f s)' %
    [url.host, http_method, url.request_uri, duration]
end

Parameters:

  • app (#call)
  • options (nil, Hash) (defaults to: nil)

    Options hash

Options Hash (options):

  • :name (String) — default: 'request.faraday'

    Name of the instrumenter

  • :instrumenter (Class) — default: ActiveSupport::Notifications

    Active Support instrumenter class.



42
43
44
45
46
# File 'lib/faraday/request/instrumentation.rb', line 42

def initialize(app, options = nil)
  super(app)
  @name, @instrumenter = Options.from(options)
                                .values_at(:name, :instrumenter)
end

Instance Method Details

#call(env) ⇒ Object

Parameters:



49
50
51
52
53
# File 'lib/faraday/request/instrumentation.rb', line 49

def call(env)
  @instrumenter.instrument(@name, env) do
    @app.call(env)
  end
end