Class: OpenTelemetry::Instrumentation::Faraday::Middlewares::TracerMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb

Overview

TracerMiddleware propagates context and instruments Faraday requests by way of its middlware system

Constant Summary collapse

HTTP_METHODS_SYMBOL_TO_STRING =
{
  connect: 'CONNECT',
  delete: 'DELETE',
  get: 'GET',
  head: 'HEAD',
  options: 'OPTIONS',
  patch: 'PATCH',
  post: 'POST',
  put: 'PUT',
  trace: 'TRACE'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Versions prior to 1.0 do not define an accessor for app



63
64
65
# File 'lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb', line 63

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb', line 26

def call(env)
  http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
  config = Faraday::Instrumentation.instance.config

  attributes = span_creation_attributes(
    http_method: http_method, url: env.url, config: config
  )
  tracer.in_span(
    "HTTP #{http_method}", attributes: attributes, kind: config.fetch(:span_kind)
  ) do |span|
    OpenTelemetry.propagation.inject(env.request_headers)

    app.call(env).on_complete { |resp| trace_response(span, resp.status) }
  rescue ::Faraday::Error => e
    trace_response(span, e.response[:status]) if e.response

    raise
  end
end