module OpenTelemetry::Instrumentation::HTTP::Patches::Client

Module to prepend to HTTP::Client for instrumentation

Public Instance Methods

perform(req, options) click to toggle source
Calls superclass method
# File lib/opentelemetry/instrumentation/http/patches/client.rb, line 13
def perform(req, options) # rubocop:disable Metrics/AbcSize
  uri = req.uri
  request_method = req.verb.to_s.upcase

  attributes = {
    'http.method' => request_method,
    'http.scheme' => uri.scheme,
    'http.target' => uri.path,
    'http.url' => "#{uri.scheme}://#{uri.host}",
    'peer.hostname' => uri.host,
    'peer.port' => uri.port
  }.merge(OpenTelemetry::Common::HTTP::ClientContext.attributes)

  tracer.in_span("HTTP #{request_method}", attributes: attributes, kind: :client) do |span|
    OpenTelemetry.propagation.inject(req.headers)
    super.tap do |response|
      annotate_span_with_response!(span, response)
    end
  end
end

Private Instance Methods

annotate_span_with_response!(span, response) click to toggle source
# File lib/opentelemetry/instrumentation/http/patches/client.rb, line 36
def annotate_span_with_response!(span, response)
  return unless response&.status

  status_code = response.status.to_i
  span.set_attribute('http.status_code', status_code)
  span.status = OpenTelemetry::Trace::Status.error unless (100..399).include?(status_code.to_i)
end
tracer() click to toggle source
# File lib/opentelemetry/instrumentation/http/patches/client.rb, line 44
def tracer
  HTTP::Instrumentation.instance.tracer
end