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
# 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)
      begin
        Hoss.with_event do |event|
          Hoss::Spies::FaradaySpy.without_net_http do
            begin
              uri = URI(build_url(url))
              result = run_request_without_apm(method, url, body, headers) do |req|
                if block_given?
                  yield req
                  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
                end
                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
              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
            rescue Exception => e
              if e.respond_to?(:wrapped_exception) && e.wrapped_exception.is_a?(Net::OpenTimeout)
                event.error = Hoss::Event::Error.new(Hoss::Event::Error::ConnectionTimeout)
              else
                event.error = Hoss::Event::Error.new(Hoss::Event::Error::ConnectionError)
              end
              event.error.received_at = DateTime.now.strftime('%Q').to_i
              raise
            end
          end
        end
      rescue
        run_request_without_apm(method, url, body, headers, &block)
      end
    end
  end
end