class ElasticAPM::Spies::HTTPSpy

@api private

def install

def install
  ::HTTP::Client.class_eval do
    alias perform_without_apm perform
    def perform(req, options)
      unless (transaction = ElasticAPM.current_transaction)
        return perform_without_apm(req, options)
      end
      method = req.verb.to_s.upcase
      host = req.uri.host
      destination =
        ElasticAPM::Span::Context::Destination.from_uri(req.uri)
      context = ElasticAPM::Span::Context.new(
        http: { url: req.uri, method: method },
        destination: destination
      )
      name = "#{method} #{host}"
      ElasticAPM.with_span(
        name,
        TYPE,
        subtype: SUBTYPE,
        action: method,
        context: context
      ) do |span|
        trace_context = span&.trace_context || transaction.trace_context
        trace_context.apply_headers { |key, value| req[key] = value }
        result = perform_without_apm(req, options)
        if (http = span&.context&.http)
          http.status_code = result.status.to_s
        end
        result
      end
    end
  end
end

def perform(req, options)

def perform(req, options)
  unless (transaction = ElasticAPM.current_transaction)
    return perform_without_apm(req, options)
  end
  method = req.verb.to_s.upcase
  host = req.uri.host
  destination =
    ElasticAPM::Span::Context::Destination.from_uri(req.uri)
  context = ElasticAPM::Span::Context.new(
    http: { url: req.uri, method: method },
    destination: destination
  )
  name = "#{method} #{host}"
  ElasticAPM.with_span(
    name,
    TYPE,
    subtype: SUBTYPE,
    action: method,
    context: context
  ) do |span|
    trace_context = span&.trace_context || transaction.trace_context
    trace_context.apply_headers { |key, value| req[key] = value }
    result = perform_without_apm(req, options)
    if (http = span&.context&.http)
      http.status_code = result.status.to_s
    end
    result
  end
end