class StackifyRubyAPM::Spies::HTTPartySpy

@api private

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/httparty.rb, line 12
def install
  # puts HTTParty::ClassMethods.instance_methods(false).inspect
  HTTParty::ClassMethods.module_eval do
    alias_method 'perform_request_without_apm', 'perform_request'

    # singleton_class.send(:alias_method, :perform_request_without_apm, :perform_request)

    private :perform_request

    def perform_request(http_method, path, options, &block)
      req = nil
      return perform_request_without_apm(http_method, path, options, &block) unless StackifyRubyAPM.current_transaction

      begin
        # Data configuration
        #
        method = http_method.to_s.gsub('Net::HTTP::', '').upcase
        uri = path.strip
        name = "#{method} #{uri}"
        type = "ext.HTTParty.#{method}"
        # Submits HTTP request
        #
        # req = perform_request_without_apm(http_method, path, options, &block)
        # Builds span context
        #
        # status_code = req.code

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

      # Creates new span from HTTP result
      #
      # class_info = { 'classname' => 'httparty', 'hostname' => URI.parse(uri).host }
      StackifyRubyAPM.span name, type, context: ctx do
        res = perform_request_without_apm(http_method, path, options, &block)

        begin
          status_code = res.code
          ctx.update_status(status_code)

          if StackifyRubyAPM.agent.config.prefix_enabled
            ctx.update_request_body(options[:body] || "")
            ctx.update_request_headers(options[:headers] || Hash.new)
            ctx.update_response_body(res.body || "")
            ctx.update_response_headers(res.each_header || Hash.new)
          end
        rescue Exception => e
          StackifyRubyAPM.agent.error '[HTTPartySpy] Error: getting status code or updating request/response context.'
          StackifyRubyAPM.agent.error "[HTTPartySpy] #{e.inspect}"
        end
        res
      end
    end
  end
end
perform_request(http_method, path, options, &block) click to toggle source
# File lib/stackify_apm/spies/httparty.rb, line 21
def perform_request(http_method, path, options, &block)
  req = nil
  return perform_request_without_apm(http_method, path, options, &block) unless StackifyRubyAPM.current_transaction

  begin
    # Data configuration
    #
    method = http_method.to_s.gsub('Net::HTTP::', '').upcase
    uri = path.strip
    name = "#{method} #{uri}"
    type = "ext.HTTParty.#{method}"
    # Submits HTTP request
    #
    # req = perform_request_without_apm(http_method, path, options, &block)
    # Builds span context
    #
    # status_code = req.code

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

  # Creates new span from HTTP result
  #
  # class_info = { 'classname' => 'httparty', 'hostname' => URI.parse(uri).host }
  StackifyRubyAPM.span name, type, context: ctx do
    res = perform_request_without_apm(http_method, path, options, &block)

    begin
      status_code = res.code
      ctx.update_status(status_code)

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