Class: Hoss::Spies::FaradaySpy Private

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

Constant Summary collapse

TYPE =

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

'ext'
SUBTYPE =

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

'faraday'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.without_net_httpObject

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.



28
29
30
31
32
33
34
# File 'lib/hoss/spies/faraday.rb', line 28

def self.without_net_http
  return yield unless defined?(NetHTTPSpy)

  Hoss::Spies::NetHTTPSpy.disable_in do
    yield
  end
end

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.

rubocop:disable Metrics/CyclomaticComplexity



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hoss/spies/faraday.rb', line 37

def install
  ::Faraday::Connection.class_eval do
    alias run_request_without_apm run_request

    def run_request(method, url, body, headers, &block)
      result = nil
      error_in_request = false
      error_in_block = false
      begin
        Hoss.with_event do |event|
          Hoss::Spies::FaradaySpy.without_net_http do
            uri = URI(build_url(url))
            begin
              result = run_request_without_apm(method, url, body, headers) do |req|
                if block_given?
                  yield req
                  begin
                    new_path = req.path
                    new_query = URI.encode_www_form(req.params)
                    if uri.path != new_path || uri.query != new_query
                      test_uri = uri
                      test_uri.query = nil
                      begin
                        test_uri.path = new_path
                      rescue Exception => e
                      end
                      # The original url can be set to path if Faraday.get used
                      if test_uri.to_s != uri.to_s
                        uri.path = new_path
                      end
                      uri.query = new_query
                    end
                  rescue
                    error_in_block = true
                    raise
                  end
                end
                begin
                  event.request.method = method.to_s.upcase
                  event.request.url = uri.to_s
                  event.request.received_at = DateTime.now.strftime('%Q').to_i
                  event.request.headers['host'] = uri.hostname
                  req.headers.each {|n,v| event.request.headers[n] = v}
                  event.request.url = uri.to_s
                  event.request.body = req.body
                rescue
                  error_in_block = true
                  raise
                end
              end
            rescue
              error_in_request = true
              event.error = Hoss::Event::Error.new(Hoss::Event::Error::ConnectionError)
              event.error.received_at = DateTime.now.strftime('%Q').to_i
              raise
            end
            if result
              event.response = Hoss::Event::Response.new
              event.response.received_at = DateTime.now.strftime('%Q').to_i
              event.response.status_code = result.status.to_i
              result.headers.each {|n,v| event.response.headers[n] = v}
              event.response.body = result.body
            end
            result
          end
        end
      rescue Exception => e
        raise if error_in_request && !error_in_block
        puts format('Hoss Error: %s %s', e.inspect, e.backtrace)
        return result unless result.nil?
        return run_request_without_apm(method, url, body, headers, &block)
      end
    end
  end
end