Class: Hoss::Spies::NetHTTPSpy Private

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

KEY =

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.

:__hoss_net_http_disabled

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable_inObject

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.



38
39
40
41
42
43
44
45
46
# File 'lib/hoss/spies/net_http.rb', line 38

def disable_in
  self.disabled = true

  begin
    yield
  ensure
    self.disabled = false
  end
end

.disabled=(disabled) ⇒ Object

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.



30
31
32
# File 'lib/hoss/spies/net_http.rb', line 30

def disabled=(disabled)
  Thread.current[KEY] = disabled
end

.disabled?Boolean

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.

Returns:

  • (Boolean)


34
35
36
# File 'lib/hoss/spies/net_http.rb', line 34

def disabled?
  Thread.current[KEY] ||= false
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



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
# File 'lib/hoss/spies/net_http.rb', line 50

def install
  Net::HTTP.class_eval do
    alias request_without_apm request

    def request(req, body = nil, &block)
      result = nil
      error_in_request = false
      begin
        if req['HOSS-SKIP-INSTRUMENTATION'] == 'true' || Hoss::Spies::NetHTTPSpy.disabled?
          return request_without_apm(req, body, &block)
        end

        host = req['host']&.split(':')&.first || address
        method = req.method.to_s.upcase
        path, query = req.path.split('?')

        url = use_ssl? ? +'https://' : +'http://'
        url << host
        url << ":#{port}" if port
        url << path
        url << "?#{query}" if query
        uri = URI(url)

        Hoss.with_event do |event|
          # Record request
          event.request.headers['host'] = uri.hostname
          req.each_header {|n,v| event.request.headers[n] = v}
          event.request.method = method
          event.request.url = uri.to_s
          event.request.body = req.body
          event.request.received_at = DateTime.now.strftime('%Q').to_i

          begin
            result = request_without_apm(req, body, &block)
          rescue
            error_in_request = true
            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.code.to_i
            result.each_header {|n,v| event.response.headers[n] = v}
            event.response.body = result.body
          end

          result
        end
      rescue Exception => e
        raise if error_in_request
        puts format('Hoss Error: %s %s', e.inspect, e.backtrace)
        return result unless result.nil?
        return request_without_apm(req, body, &block)
      end
    end
  end
end