Class: Faraday::Request::Instrumentation

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

Overview

Middleware for instrumenting Requests.

Defined Under Namespace

Classes: Options

Instance Attribute Summary

Attributes inherited from Middleware

#app, #options

Attributes included from DependencyLoader

#load_error

Instance Method Summary collapse

Methods inherited from Middleware

#close

Methods included from DependencyLoader

#dependency, #inherited, #loaded?, #new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_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.


40
41
42
43
44
# File 'lib/faraday/request/instrumentation.rb', line 40

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

Instance Method Details

#call(env) ⇒ Object

Parameters:


47
48
49
50
51
# File 'lib/faraday/request/instrumentation.rb', line 47

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