Class: RorVsWild::Plugin::NetHttp

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

Constant Summary collapse

HTTP =
"http".freeze
HTTPS =
"https".freeze

Class Method Summary collapse

Class Method Details

.ignore(&block) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/rorvswild/plugin/net_http.rb', line 39

def self.ignore(&block)
  old_value = Thread.current[:rorvswild_ignore_net_http]
  Thread.current[:rorvswild_ignore_net_http] = true
  block.call
ensure
  Thread.current[:rorvswild_ignore_net_http] = old_value
end

.setupObject


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
# File 'lib/rorvswild/plugin/net_http.rb', line 7

def self.setup
  return if !defined?(Net::HTTP)
  return if Net::HTTP.method_defined?(:request_without_rorvswild)

  Net::HTTP.class_eval do
    alias_method :request_without_rorvswild, :request

    def request(req, body = nil, &block)
      if Thread.current[:rorvswild_ignore_net_http]
        request_without_rorvswild(req, body, &block)
      else
        request_with_rorvswild(req, body, &block)
      end
    end

    def request_with_rorvswild(req, body = nil, &block)
      return request_without_rorvswild(req, body, &block) if request_called_twice?
      scheme = use_ssl? ? HTTPS : HTTP
      url = "#{req.method} #{scheme}://#{address}#{req.path}"
      RorVsWild.agent.measure_section(url, kind: HTTP) do
        request_without_rorvswild(req, body, &block)
      end
    end

    def request_called_twice?
      # Net::HTTP#request calls itself when connection is not started.
      # This condition prevents from counting twice the request.
      (current_section = RorVsWild::Section.current) && current_section.kind == HTTP
    end
  end
end