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 middleware 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
HTTP_STATUS_SUCCESS_RANGE =

Constant for the HTTP status range

(100..399)

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



69
70
71
# File 'lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb', line 69

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb', line 29

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
  )

  OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
    tracer.in_span(
      "HTTP #{http_method}", attributes: attrs, 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
end