Class: SimpleApm::NetHttp

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_apm/net_http.rb

Class Method Summary collapse

Class Method Details

.installObject



5
6
7
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
# File 'lib/simple_apm/net_http.rb', line 5

def install
  Net::HTTP.class_eval do
    alias origin_request_apm request unless method_defined?(:origin_request_apm)
    alias origin_do_start_apm do_start unless method_defined?(:origin_do_start_apm)

    def do_start
      Thread.current[:injection_net_http_request_start_time] = Time.now
      origin_do_start_apm
    end

    def request(req, body = nil, &block)
      url = if @port == '80'
              "http://#{@address}#{req.path}"
            elsif @port == '443'
              "https://#{@address}#{req.path}"
            else
              "http://#{@address}:#{@port}#{req.path}"
            end
      payload = {
          real_start_time: Thread.current[:injection_net_http_request_start_time],
          url: url, host: @address, path: req.path
      }
      if started?
        ActiveSupport::Notifications.instrument 'net_http.request', payload do
          @response = origin_request_apm(req, body, &block)
        end
      else
        # 去connect
        @response = origin_request_apm(req, body, &block)
      end
      @response
    end
  end
end