class StackifyRubyAPM::Spies::FaradaySpy

@api private

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/faraday.rb, line 8
def install
  Faraday::Connection.class_eval do
    alias_method 'run_request_without_apm', 'run_request'

    def run_request(method, url, body, headers, &block)
      result = nil
      return run_request_without_apm(method, url, body, headers, &block) unless StackifyRubyAPM.current_transaction

      begin
        uri = URI(build_url(url))

        # url is not available yet if it is set inside block
        # we need to build temporary request as of now
        # NOTE: this could have a side effect doing yeild twice
        unless uri.host
          tmp_request = build_request(method) do |req|
            yield(req) if block_given?
          end
          uri = URI(tmp_request.path)
        end

        host = uri.host
        method_upcase = method.to_s.upcase
        name = "#{method_upcase} #{host}"
        type = "ext.faraday.#{method_upcase}"

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

      # Creates new span from HTTP result
      #
      StackifyRubyAPM.span name, type, context: ctx do
        # Submits HTTP request
        #
        result = run_request_without_apm(method, url, body, headers) do |req|
          yield req if block_given?

          if StackifyRubyAPM.agent.config.prefix_enabled
            ctx.update_request_body(req.body || body || "")
            ctx.update_request_headers(req.headers || headers || Hash.new)
          end
        end

        begin
          status_code = result.status.to_s
          ctx.update_status(status_code)

          if StackifyRubyAPM.agent.config.prefix_enabled
            ctx.update_response_body(result.body || "")
            ctx.update_response_headers(result.headers || Hash.new)
          end
        rescue Exception => e
          StackifyRubyAPM.agent.error '[FaradaySpy] Error: getting status code or updating request/response context.'
          StackifyRubyAPM.agent.error "[FaradaySpy] #{e.inspect}"
        end
        return result
      end
    end
  end
end
run_request(method, url, body, headers) { |req| ... } click to toggle source
# File lib/stackify_apm/spies/faraday.rb, line 12
def run_request(method, url, body, headers, &block)
  result = nil
  return run_request_without_apm(method, url, body, headers, &block) unless StackifyRubyAPM.current_transaction

  begin
    uri = URI(build_url(url))

    # url is not available yet if it is set inside block
    # we need to build temporary request as of now
    # NOTE: this could have a side effect doing yeild twice
    unless uri.host
      tmp_request = build_request(method) do |req|
        yield(req) if block_given?
      end
      uri = URI(tmp_request.path)
    end

    host = uri.host
    method_upcase = method.to_s.upcase
    name = "#{method_upcase} #{host}"
    type = "ext.faraday.#{method_upcase}"

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

  # Creates new span from HTTP result
  #
  StackifyRubyAPM.span name, type, context: ctx do
    # Submits HTTP request
    #
    result = run_request_without_apm(method, url, body, headers) do |req|
      yield req if block_given?

      if StackifyRubyAPM.agent.config.prefix_enabled
        ctx.update_request_body(req.body || body || "")
        ctx.update_request_headers(req.headers || headers || Hash.new)
      end
    end

    begin
      status_code = result.status.to_s
      ctx.update_status(status_code)

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