class StackifyRubyAPM::Spies::CurbMultiSpy

@api private

Public Class Methods

http(urls_with_config, _multi_options = {}, &blk) click to toggle source
# File lib/stackify_apm/spies/curb/multi.rb, line 14
def self.http(urls_with_config, _multi_options = {}, &blk)
  return http_without_apm(urls_with_config, _multi_options = {}, &blk) unless StackifyRubyAPM.current_transaction
  http_without_apm(urls_with_config, _multi_options = {}) do |c, code, method|
    begin
      status_code = code.zero? ? 404 : code
      method = method.upcase
      uri = c.url.to_s.strip
      name = "#{method} #{uri}"
      type = "ext.Curb.Multi.#{method}"

      ctx = Span::Context.new(
        CATEGORY: 'Web External',
        SUBCATEGORY: 'Execute',
        URL: uri,
        STATUS: status_code,
        METHOD: method
      )

      if StackifyRubyAPM.agent.config.prefix_enabled
        ctx.update_request_body(c.post_body || "")
        ctx.update_request_headers(c.headers || Hash.new)
        ctx.update_response_body(c.body || "")
        ctx.update_response_headers(c.proxy_headers || Hash.new)
      end
    rescue Exception => e
      StackifyRubyAPM.agent.error "[CurbMultiSpy] Error: creating span context."
      StackifyRubyAPM.agent.error "[CurbMultiSpy] #{e.inspect}"
      return blk.call(c, code, method)
    end

    # Creates new span from HTTP result
    StackifyRubyAPM.span name, type, context: ctx do
      blk.call(c, code, method)
    end
  end
end

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/curb/multi.rb, line 10
def install
  Curl::Multi.class_eval do
    singleton_class.send(:alias_method, :http_without_apm, :http)

    def self.http(urls_with_config, _multi_options = {}, &blk)
      return http_without_apm(urls_with_config, _multi_options = {}, &blk) unless StackifyRubyAPM.current_transaction
      http_without_apm(urls_with_config, _multi_options = {}) do |c, code, method|
        begin
          status_code = code.zero? ? 404 : code
          method = method.upcase
          uri = c.url.to_s.strip
          name = "#{method} #{uri}"
          type = "ext.Curb.Multi.#{method}"

          ctx = Span::Context.new(
            CATEGORY: 'Web External',
            SUBCATEGORY: 'Execute',
            URL: uri,
            STATUS: status_code,
            METHOD: method
          )

          if StackifyRubyAPM.agent.config.prefix_enabled
            ctx.update_request_body(c.post_body || "")
            ctx.update_request_headers(c.headers || Hash.new)
            ctx.update_response_body(c.body || "")
            ctx.update_response_headers(c.proxy_headers || Hash.new)
          end
        rescue Exception => e
          StackifyRubyAPM.agent.error "[CurbMultiSpy] Error: creating span context."
          StackifyRubyAPM.agent.error "[CurbMultiSpy] #{e.inspect}"
          return blk.call(c, code, method)
        end

        # Creates new span from HTTP result
        StackifyRubyAPM.span name, type, context: ctx do
          blk.call(c, code, method)
        end
      end
    end
  end
end