Class: Skylight::Core::Probes::NetHTTP::Probe Private

Inherits:
Object
  • Object
show all
Defined in:
lib/skylight/core/probes/net_http.rb

Overview

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.

Probe for instrumenting Net::HTTP requests. Works by monkeypatching the default Net::HTTP#request method.

Constant Summary collapse

DISABLED_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.

:__skylight_net_http_disabled

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disableObject

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.



10
11
12
13
14
15
16
# File 'lib/skylight/core/probes/net_http.rb', line 10

def self.disable
  state_was = Thread.current[DISABLED_KEY]
  Thread.current[DISABLED_KEY] = true
  yield
ensure
  Thread.current[DISABLED_KEY] = state_was
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)


18
19
20
# File 'lib/skylight/core/probes/net_http.rb', line 18

def self.disabled?
  !!Thread.current[DISABLED_KEY]
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.



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
55
56
57
58
59
# File 'lib/skylight/core/probes/net_http.rb', line 22

def install
  Net::HTTP.class_eval do
    alias_method :request_without_sk, :request

    def request(req, body = nil, &block)
      if !started? || Probes::NetHTTP::Probe.disabled?
        return request_without_sk(req, body, &block)
      end

      method = req.method

      # req['host'] also includes special handling for default ports
      host, port = req["host"] ? req["host"].split(":") : nil

      # If we're connected with a persistent socket
      host ||= address
      port ||= port

      path   = req.path
      scheme = use_ssl? ? "https" : "http"

      # Contained in the path
      query  = nil

      opts = Formatters::HTTP.build_opts(method, scheme, host, port, path, query)

      Skylight::Core::Fanout.instrument(opts) do |spans|
        spans.each do |(instrumentable, span)|
          # TODO: Should we make something more generic?
          if (header = instrumentable.correlation_header)
            req[header] = instrumentable.span_correlation_header(span)
          end
        end
        request_without_sk(req, body, &block)
      end
    end
  end
end