Class: StackifyRubyAPM::Spies::HTTPartySpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/httparty.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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