Class: HubStep::Faraday::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/hubstep/faraday/middleware.rb

Overview

Faraday middlware for wrapping a request in a span.

tracer = HubStep::Tracer.new Faraday.new do |b|

b.request(:hubstep, tracer)
b.adapter(:typhoeus)

end

Instance Method Summary collapse

Constructor Details

#initialize(app, tracer) ⇒ Middleware

Returns a new instance of Middleware.



16
17
18
19
# File 'lib/hubstep/faraday/middleware.rb', line 16

def initialize(app, tracer)
  super(app)
  @tracer = tracer
end

Instance Method Details

#call(request_env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hubstep/faraday/middleware.rb', line 21

def call(request_env)
  # We pass `finish: false` so that the span won't have its end time
  # recorded until #on_complete runs (which could be after #call returns
  # if the request is asynchronous).
  @tracer.span("faraday", finish: false) do |span|
    begin
      span.configure { record_request(span, request_env) }
      @app.call(request_env).on_complete do |response_env|
        span.configure do
          record_response(span, response_env)
          span.finish if span.end_micros.nil?
        end
      end
    ensure
      span.configure { record_exception(span, $ERROR_INFO) }
    end
  end
end