Class: StackifyRubyAPM::Spies::FaradaySpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/faraday.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.



8
9
10
11
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
77
78
79
# 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