class StackifyRubyAPM::Spies::NetHTTPSpy

@api private

Public Instance Methods

install() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/stackify_apm/spies/net_http.rb, line 9
def install
  Net::HTTP.class_eval do
    alias_method 'request_without_apm', 'request'

    def request(req, body = nil, &block)
      result = nil
      return request_without_apm(req, body, &block) unless StackifyRubyAPM.current_transaction
      return request_without_apm(req, body, &block) if started? && req['span']

      begin
        # Data configuration
        #
        host, = req['host'] && req['host'].split(':')
        method = req.method

        host ||= address

        # For Ruby version 2.2.x, 2.3.x, 2.4.x we need to parse it and get the <scheme> & <host>/<path>
        updated_uri = req.uri.scheme + '://' + req.uri.host + req.uri.path if defined?(req.uri.host)

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

        # Builds span context
        #
        ctx = Span::Context.new(
          CATEGORY: 'Web External',
          SUBCATEGORY: 'Execute',
          URL: req.uri.nil? ? host : updated_uri,
          STATUS: '',
          METHOD: method
        )
      rescue Exception => e
        StackifyRubyAPM.agent.error "[NetHTTPSpy] Error: creating span context."
        StackifyRubyAPM.agent.error "[NetHTTPSpy] #{e.inspect}"
        return request_without_apm(req, body, &block)
      end

      # Creates new span from HTTP result
      #
      StackifyRubyAPM.span name, type, context: ctx do
        # Submits HTTP request
        #
        req['span'] = true
        result = request_without_apm(req, body, &block)
        begin
          status_code = result.code.to_i
          ctx.update_status(status_code)

          if StackifyRubyAPM.agent.config.prefix_enabled
            ctx.update_request_body(req.body || "")
            ctx.update_request_headers(req.each_header || Hash.new)
            ctx.update_response_body(result.body || "")
            ctx.update_response_headers(result.each_header || Hash.new)
          end
        rescue Exception => e
          StackifyRubyAPM.agent.error '[NetHTTPSpy] Error: getting status code or updating request/response context.'
          StackifyRubyAPM.agent.error "[NetHTTPSpy] #{e.inspect}"
        end
        return result
      end
    end
  end
end
request(req, body = nil, &block) click to toggle source
# File lib/stackify_apm/spies/net_http.rb, line 13
def request(req, body = nil, &block)
  result = nil
  return request_without_apm(req, body, &block) unless StackifyRubyAPM.current_transaction
  return request_without_apm(req, body, &block) if started? && req['span']

  begin
    # Data configuration
    #
    host, = req['host'] && req['host'].split(':')
    method = req.method

    host ||= address

    # For Ruby version 2.2.x, 2.3.x, 2.4.x we need to parse it and get the <scheme> & <host>/<path>
    updated_uri = req.uri.scheme + '://' + req.uri.host + req.uri.path if defined?(req.uri.host)

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

    # Builds span context
    #
    ctx = Span::Context.new(
      CATEGORY: 'Web External',
      SUBCATEGORY: 'Execute',
      URL: req.uri.nil? ? host : updated_uri,
      STATUS: '',
      METHOD: method
    )
  rescue Exception => e
    StackifyRubyAPM.agent.error "[NetHTTPSpy] Error: creating span context."
    StackifyRubyAPM.agent.error "[NetHTTPSpy] #{e.inspect}"
    return request_without_apm(req, body, &block)
  end

  # Creates new span from HTTP result
  #
  StackifyRubyAPM.span name, type, context: ctx do
    # Submits HTTP request
    #
    req['span'] = true
    result = request_without_apm(req, body, &block)
    begin
      status_code = result.code.to_i
      ctx.update_status(status_code)

      if StackifyRubyAPM.agent.config.prefix_enabled
        ctx.update_request_body(req.body || "")
        ctx.update_request_headers(req.each_header || Hash.new)
        ctx.update_response_body(result.body || "")
        ctx.update_response_headers(result.each_header || Hash.new)
      end
    rescue Exception => e
      StackifyRubyAPM.agent.error '[NetHTTPSpy] Error: getting status code or updating request/response context.'
      StackifyRubyAPM.agent.error "[NetHTTPSpy] #{e.inspect}"
    end
    return result
  end
end