Sha256: 73368c4f41391b7f62581faa25ffc5607c5c409c85ca999e117264efa02f7a83

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  # @api private
  module Spies
    # @api private
    class FaradaySpy
      # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      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
                   else
                     URI(url).host
                   end

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

            ElasticAPM.with_span name, type do |span|
              ElasticAPM::Spies::NetHTTPSpy.disable_in 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
      # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
    end

    register 'Faraday', 'faraday', FaradaySpy.new
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
elastic-apm-2.4.0 lib/elastic_apm/spies/faraday.rb
elastic-apm-2.3.1 lib/elastic_apm/spies/faraday.rb
elastic-apm-2.3.0 lib/elastic_apm/spies/faraday.rb
elastic-apm-2.2.0 lib/elastic_apm/spies/faraday.rb