class Skylight::Core::Probes::NetHTTP::Probe

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

Constants

DISABLED_KEY

Public Class Methods

disable() { || ... } click to toggle source
# 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?() click to toggle source
# File lib/skylight/core/probes/net_http.rb, line 18
def self.disabled?
  !!Thread.current[DISABLED_KEY]
end

Public Instance Methods

install() click to toggle source
# 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
request(req, body = nil, &block) click to toggle source
# File lib/skylight/core/probes/net_http.rb, line 26
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