Sha256: a763d89a31beb0111284c6156e3dacaf4bbdc534454830e7347c66311735fe1b

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  # @api private
  module Spies
    # @api private
    class HTTPSpy
      # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      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

            name = "#{method} #{host}"
            type = "ext.http_rb.#{method}"

            ElasticAPM.with_span name, type do |span|
              id = span&.id || transaction.id

              req['Elastic-Apm-Traceparent'] =
                transaction.traceparent.to_header(span_id: id)

              perform_without_apm(req, options)
            end
          end
        end
      end
      # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
    end

    register 'HTTP', 'http', HTTPSpy.new
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elastic-apm-2.1.2 lib/elastic_apm/spies/http.rb
elastic-apm-2.1.1 lib/elastic_apm/spies/http.rb