Class: OpenTracing::Instrumentation::Faraday::TraceMiddleware

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opentracing/instrumentation/faraday/trace_middleware.rb

Overview

TraceMiddleware inject tracing header into request and trace request

Usage with default config:

Faraday.new(url) do |connection|
  connection.use \
    OpenTracing::Instrumentation::Faraday::TraceMiddleware
end

Usage with config block:

Faraday.new(url) do |connection|
  connection.use \
    OpenTracing::Instrumentation::Faraday::TraceMiddleware do |c|
      # c is instance of Config
      c.tracer = tracer
    end
end

Defined Under Namespace

Classes: Config

Instance Method Summary collapse

Constructor Details

#initialize(app, config = Config.new) {|config| ... } ⇒ TraceMiddleware

Returns a new instance of TraceMiddleware.

Parameters:

  • config (Config) (defaults to: Config.new)

Yield Parameters:



82
83
84
85
86
87
88
89
# File 'lib/opentracing/instrumentation/faraday/trace_middleware.rb', line 82

def initialize(
  app,
  config = Config.new
)
  @app = app
  @config = config
  yield(config) if block_given?
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Wrap Faraday request to trace it with OpenTracing

Parameters:

  • env (Faraday::Env)

Returns:

  • (Faraday::Response)


94
95
96
97
98
99
100
101
102
103
# File 'lib/opentracing/instrumentation/faraday/trace_middleware.rb', line 94

def call(env)
  trace_request(env) do |span|
    inject_tracing(span, env) if inject
    response_logger&.log_request(span, env.request_headers)
    @app.call(env).on_complete do |response|
      set_response_tags(span, response)
      response_logger&.log_response(span, response.response_headers)
    end
  end
end