class StackifyRubyAPM::Spies::HTTPClientSpy

@api private

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/httpclient.rb, line 11
def install
  HTTPClient.class_eval do
    alias_method 'request_without_apm', 'request'

    def request(method, uri, *args, &block)
      req = nil
      return request_without_apm(method, uri, *args, &block) unless StackifyRubyAPM.current_transaction

      begin
        # Data configuration
        #
        method = method.upcase
        uri = uri.strip
        name = "#{method} #{uri}"
        type = "ext.httpclient.#{method}"

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

      # Creates new span from HTTP result
      #
      StackifyRubyAPM.span name, type, context: ctx do
        # Submits HTTP request
        #
        res = request_without_apm(method, uri, *args, &block)

        begin
          ctx.update_status(res.status_code)

          if StackifyRubyAPM.agent.config.prefix_enabled
            options = args && args[0] || Hash.new
            ctx.update_request_body(options[:body] || "")
            ctx.update_request_headers(options[:header] || Hash.new)
            ctx.update_response_body(res.body || "")
            ctx.update_response_headers(res.headers || Hash.new)
          end
        rescue Exception => e
          StackifyRubyAPM.agent.error '[HTTPClientSpy] Error: getting status code or updating request/response context.'
          StackifyRubyAPM.agent.error "[HTTPClientSpy] #{e.inspect}"
        end

        res
      end
    end
  end
end
request(method, uri, *args, &block) click to toggle source
# File lib/stackify_apm/spies/httpclient.rb, line 15
def request(method, uri, *args, &block)
  req = nil
  return request_without_apm(method, uri, *args, &block) unless StackifyRubyAPM.current_transaction

  begin
    # Data configuration
    #
    method = method.upcase
    uri = uri.strip
    name = "#{method} #{uri}"
    type = "ext.httpclient.#{method}"

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

  # Creates new span from HTTP result
  #
  StackifyRubyAPM.span name, type, context: ctx do
    # Submits HTTP request
    #
    res = request_without_apm(method, uri, *args, &block)

    begin
      ctx.update_status(res.status_code)

      if StackifyRubyAPM.agent.config.prefix_enabled
        options = args && args[0] || Hash.new
        ctx.update_request_body(options[:body] || "")
        ctx.update_request_headers(options[:header] || Hash.new)
        ctx.update_response_body(res.body || "")
        ctx.update_response_headers(res.headers || Hash.new)
      end
    rescue Exception => e
      StackifyRubyAPM.agent.error '[HTTPClientSpy] Error: getting status code or updating request/response context.'
      StackifyRubyAPM.agent.error "[HTTPClientSpy] #{e.inspect}"
    end

    res
  end
end