class StackifyRubyAPM::Spies::HTTPRbSpy

@api private

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/httprb.rb, line 11
def install
  HTTP::Client.class_eval do
    alias_method 'perform_without_apm', 'perform'

    # Make HTTP request
    def perform(req, options)
      return perform_without_apm(req, options) unless StackifyRubyAPM.current_transaction

      begin
        method = req.verb.upcase
        uri = req.uri.to_s.strip
        name = "#{method} #{uri}"
        type = "ext.httprb.#{method}"

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

      StackifyRubyAPM.span name, type, context: ctx do
        res = perform_without_apm(req, options)

        begin
          ctx.update_status(res.code)

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

        res
      end
    end
  end
end
perform(req, options) click to toggle source

Make HTTP request

# File lib/stackify_apm/spies/httprb.rb, line 16
def perform(req, options)
  return perform_without_apm(req, options) unless StackifyRubyAPM.current_transaction

  begin
    method = req.verb.upcase
    uri = req.uri.to_s.strip
    name = "#{method} #{uri}"
    type = "ext.httprb.#{method}"

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

  StackifyRubyAPM.span name, type, context: ctx do
    res = perform_without_apm(req, options)

    begin
      ctx.update_status(res.code)

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

    res
  end
end