class ElasticAPM::Spies::FaradaySpy

@api private

Public Class Methods

without_net_http() { || ... } click to toggle source
# File lib/elastic_apm/spies/faraday.rb, line 7
def self.without_net_http
  return yield unless defined?(NetHTTPSpy)

  ElasticAPM::Spies::NetHTTPSpy.disable_in do
    yield
  end
end

Public Instance Methods

install() click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize rubocop:disable Metrics/BlockLength, Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity

# File lib/elastic_apm/spies/faraday.rb, line 18
def install
  ::Faraday::Connection.class_eval do
    alias run_request_without_apm run_request

    def run_request(method, url, body, headers, &block)
      unless (transaction = ElasticAPM.current_transaction)
        return run_request_without_apm(method, url, body, headers, &block)
      end

      host = if url_prefix.is_a?(URI) && url_prefix.host
               url_prefix.host
             elsif url.nil?
               tmp_request = build_request(method) do |req|
                 yield(req) if block_given?
               end
               URI(tmp_request.path).host
             else
               URI(url).host
             end

      name = "#{method.upcase} #{host}"
      type = "ext.faraday.#{method}"

      ElasticAPM.with_span name, type do |span|
        ElasticAPM::Spies::FaradaySpy.without_net_http do
          trace_context = span&.trace_context || transaction.trace_context

          run_request_without_apm(method, url, body, headers) do |req|
            req['Elastic-Apm-Traceparent'] = trace_context.to_header

            yield req if block_given?
          end
        end
      end
    end
  end
end
run_request(method, url, body, headers) { |req| ... } click to toggle source
# File lib/elastic_apm/spies/faraday.rb, line 22
def run_request(method, url, body, headers, &block)
  unless (transaction = ElasticAPM.current_transaction)
    return run_request_without_apm(method, url, body, headers, &block)
  end

  host = if url_prefix.is_a?(URI) && url_prefix.host
           url_prefix.host
         elsif url.nil?
           tmp_request = build_request(method) do |req|
             yield(req) if block_given?
           end
           URI(tmp_request.path).host
         else
           URI(url).host
         end

  name = "#{method.upcase} #{host}"
  type = "ext.faraday.#{method}"

  ElasticAPM.with_span name, type do |span|
    ElasticAPM::Spies::FaradaySpy.without_net_http do
      trace_context = span&.trace_context || transaction.trace_context

      run_request_without_apm(method, url, body, headers) do |req|
        req['Elastic-Apm-Traceparent'] = trace_context.to_header

        yield req if block_given?
      end
    end
  end
end