Sha256: f65c696267f7bf82e902475bfbf70cbfc8faf4e00d03b837e351768164edc255

Contents?: true

Size: 1.61 KB

Versions: 14

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  # @api private
  module Spies
    # @api private
    class NetHTTPSpy
      KEY = :__elastic_apm_net_http_disabled

      # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      class << self
        def disabled=(disabled)
          Thread.current[KEY] = disabled
        end

        def disabled?
          Thread.current[KEY] ||= false
        end

        def disable_in
          self.disabled = true

          begin
            yield
          ensure
            self.disabled = false
          end
        end
      end

      def install
        Net::HTTP.class_eval do
          alias request_without_apm request

          def request(req, body = nil, &block)
            unless (transaction = ElasticAPM.current_transaction)
              return request_without_apm(req, body, &block)
            end
            if ElasticAPM::Spies::NetHTTPSpy.disabled?
              return request_without_apm(req, body, &block)
            end

            host, = req['host'] && req['host'].split(':')
            method = req.method

            host ||= address

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

            ElasticAPM.with_span name, type do |span|
              trace_context = span&.trace_context || transaction.trace_context
              req['Elastic-Apm-Traceparent'] = trace_context.to_header
              request_without_apm(req, body, &block)
            end
          end
        end
      end
      # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
    end

    register 'Net::HTTP', 'net/http', NetHTTPSpy.new
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
elastic-apm-2.10.1 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.10.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.9.1 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.9.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.8.1 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.8.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.7.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.6.1 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.6.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.5.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.4.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.3.1 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.3.0 lib/elastic_apm/spies/net_http.rb
elastic-apm-2.2.0 lib/elastic_apm/spies/net_http.rb