class ElasticAPM::Spies::NetHTTPSpy

@api private

Constants

KEY

Public Class Methods

disable_in() { || ... } click to toggle source
# File lib/elastic_apm/spies/net_http.rb, line 20
def disable_in
  self.disabled = true

  begin
    yield
  ensure
    self.disabled = false
  end
end
disabled=(disabled) click to toggle source
# File lib/elastic_apm/spies/net_http.rb, line 12
def disabled=(disabled)
  Thread.current[KEY] = disabled
end
disabled?() click to toggle source
# File lib/elastic_apm/spies/net_http.rb, line 16
def disabled?
  Thread.current[KEY] ||= false
end

Public Instance Methods

install() click to toggle source
# File lib/elastic_apm/spies/net_http.rb, line 31
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
request(req, body = nil, &block) click to toggle source
# File lib/elastic_apm/spies/net_http.rb, line 35
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