Module: MetrixWire::Instrument::NetHttp

Defined in:
lib/metrixwire/instrument/net_http.rb

Overview

Patches Net::HTTP#request so outbound HTTP calls become http_call spans with the response status code. Covers Net::HTTP directly and libraries built on it. Skips requests to the MetrixWire ingest endpoint itself so we never trace our own telemetry.

Defined Under Namespace

Modules: Patch

Class Method Summary collapse

Class Method Details

.default_port?(scheme, port) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/metrixwire/instrument/net_http.rb', line 62

def default_port?(scheme, port)
  (scheme == "https" && port == 443) || (scheme == "http" && port == 80)
rescue StandardError
  true
end

.describe(http, req) ⇒ Object

Build "GET https://host/path" from the connection + request objects.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/metrixwire/instrument/net_http.rb', line 48

def describe(http, req)
  method = req.respond_to?(:method) ? req.method : "GET"
  scheme = http.respond_to?(:use_ssl?) && http.use_ssl? ? "https" : "http"
  host = http.respond_to?(:address) ? http.address : nil
  port = http.respond_to?(:port) ? http.port : nil
  return nil if host.nil?

  path = req.respond_to?(:path) ? req.path.to_s.split("?").first : "/"
  hostport = default_port?(scheme, port) ? host : "#{host}:#{port}"
  "#{method} #{scheme}://#{hostport}#{path}"
rescue StandardError
  nil
end

.installObject



14
15
16
17
18
19
20
21
# File 'lib/metrixwire/instrument/net_http.rb', line 14

def install
  return if ::Net::HTTP.instance_variable_get(:@__metrixwire_patched)

  ::Net::HTTP.prepend(Patch)
  ::Net::HTTP.instance_variable_set(:@__metrixwire_patched, true)
rescue StandardError
  nil
end

.own_endpoint?(desc) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/metrixwire/instrument/net_http.rb', line 68

def own_endpoint?(desc)
  ep = MetrixWire.endpoint
  return false if ep.nil? || ep.empty?

  host = URI.parse(ep).host
  host && desc.include?("//#{host}") || (host && desc.include?("//#{host}:"))
rescue StandardError
  false
end