Class: StackifyRubyAPM::Spies::NetHTTPSpy Private

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

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.

rubocop:disable Metrics/CyclomaticComplexity



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

def install
  Net::HTTP.class_eval do
    alias_method 'request_without_apm', 'request'

    def request(req, body = nil, &block)
      result = nil
      return request_without_apm(req, body, &block) unless StackifyRubyAPM.current_transaction
      return request_without_apm(req, body, &block) if started? && req['span']

      # Data configuration
      #
      host, = req['host'] && req['host'].split(':')
      method = req.method

      host ||= address

      # For Ruby version 2.2.x, 2.3.x, 2.4.x we need to parse it and get the <scheme> & <host>/<path>
      updated_uri = req.uri.scheme + '://' + req.uri.host + req.uri.path if defined?(req.uri.host)

      name = "#{method} #{host}"
      type = "ext.net_http.#{method}"

      # Builds span context
      #
      ctx = Span::Context.new(
        CATEGORY: 'Web External',
        SUBCATEGORY: 'Execute',
        URL: req.uri.nil? ? host : updated_uri,
        STATUS: '',
        METHOD: method
      )

      # Creates new span from HTTP result
      #
      StackifyRubyAPM.span name, type, context: ctx do
        # Submits HTTP request
        #
        req['span'] = true
        result = request_without_apm(req, body, &block)
        status_code = result.code.to_i
        ctx.update_status(status_code)
        return result
      end
    end
  end
end