Class: OpenTelemetry::Instrumentation::Excon::Middlewares::TracerMiddleware

Inherits:
Excon::Middleware::Base
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb

Overview

Excon middleware for instrumentation

Constant Summary collapse

HTTP_METHODS_TO_UPPERCASE =
%w[connect delete get head options patch post put trace].each_with_object({}) do |method, hash|
  uppercase_method = method.upcase
  hash[method] = uppercase_method
  hash[method.to_sym] = uppercase_method
  hash[uppercase_method] = uppercase_method
end.freeze
HTTP_METHODS_TO_SPAN_NAMES =
HTTP_METHODS_TO_UPPERCASE.values.each_with_object({}) do |uppercase_method, hash|
  hash[uppercase_method] ||= "HTTP #{uppercase_method}"
end.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.around_default_stackObject

Returns a copy of the default stack with the trace middleware injected



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb', line 59

def self.around_default_stack
  ::Excon.defaults[:middlewares].dup.tap do |default_stack|
    # If the default stack contains a version of the trace middleware already...
    existing_trace_middleware = default_stack.find { |m| m <= TracerMiddleware }
    default_stack.delete(existing_trace_middleware) if existing_trace_middleware

    # Inject after the ResponseParser middleware
    response_middleware_index = default_stack.index(::Excon::Middleware::ResponseParser).to_i
    default_stack.insert(response_middleware_index + 1, self)
  end
end

Instance Method Details

#error_call(datum) ⇒ Object



53
54
55
56
# File 'lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb', line 53

def error_call(datum)
  handle_response(datum)
  @stack.error_call(datum)
end

#request_call(datum) ⇒ Object



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

def request_call(datum)
  begin
    unless datum.key?(:otel_span)
      http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]]
      attributes = span_creation_attributes(datum, http_method)
      tracer.start_span(
        HTTP_METHODS_TO_SPAN_NAMES[http_method],
        attributes: attributes,
        kind: :client
      ).tap do |span|
        datum[:otel_span] = span
        OpenTelemetry::Trace.with_span(span) do
          OpenTelemetry.propagation.inject(datum[:headers])
        end
      end
    end
  rescue StandardError => e
    OpenTelemetry.logger.debug(e.message)
  end

  @stack.request_call(datum)
end

#response_call(datum) ⇒ Object



47
48
49
50
51
# File 'lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb', line 47

def response_call(datum)
  @stack.response_call(datum).tap do |d|
    handle_response(d)
  end
end