Module: OpenTrace::HttpTracker

Defined in:
lib/opentrace/http_tracker.rb

Constant Summary collapse

MAX_BODY_CAPTURE_BYTES =

Max body size to capture (64KB) — avoids bloating memory with large payloads

65_536
VENDOR_PATTERNS =
{
  "stripe" => /api\.stripe\.com/i,
  "sendgrid" => /api\.sendgrid\.com/i,
  "twilio" => /api\.twilio\.com/i,
  "slack" => /slack\.com/i,
  "github" => /api\.github\.com/i,
  "aws" => /\.amazonaws\.com/i,
  "google" => /googleapis\.com/i,
  "mailgun" => /api\.mailgun\.net/i,
  "postmark" => /api\.postmarkapp\.com/i,
  "braintree" => /api\.braintreegateway\.com/i,
  "paypal" => /api\.paypal\.com/i,
  "shopify" => /\.myshopify\.com|\.shopify\.com/i,
  "intercom" => /api\.intercom\.io/i,
  "segment" => /api\.segment\.io/i,
  "sentry" => /sentry\.io/i,
  "datadog" => /api\.datadoghq\.com/i,
  "plaid" => /\.plaid\.com/i,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.infer_vendor(host) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/opentrace/http_tracker.rb', line 30

def self.infer_vendor(host)
  return nil unless host

  VENDOR_PATTERNS.each do |vendor, pattern|
    return vendor if pattern.match?(host)
  end
  nil
end

Instance Method Details

#request(req, body = nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/opentrace/http_tracker.rb', line 39

def request(req, body = nil, &block)
  # Guard 1: skip if disabled
  return super unless OpenTrace.enabled?

  # Guard 2: skip if this IS an OpenTrace dispatch call (prevent infinite recursion)
  return super if Fiber[:opentrace_http_tracking_disabled]

  # Inject trace context into outgoing request headers
  inject_trace_context(req) if OpenTrace.config.trace_propagation

  collector = Fiber[:opentrace_collector]
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  response = super

  duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000
  host = address
  port_str = (port == 443 || port == 80) ? "" : ":#{port}"
  scheme = use_ssl? ? "https" : "http"
  safe_path = req.path.to_s.split("?").first
  url = "#{scheme}://#{host}#{port_str}#{safe_path}"

  # Capture request body (if present and under size limit)
  req_body = nil
  if req.body && req.body.is_a?(String) && req.body.bytesize < MAX_BODY_CAPTURE_BYTES
    req_body = req.body
  end

  # Capture response body (if under size limit)
  resp_body = nil
  if response.body && response.body.is_a?(String) && response.body.bytesize < MAX_BODY_CAPTURE_BYTES
    resp_body = response.body
  end

  if collector
    collector.record_http(
      method: req.method,
      url: url,
      host: host,
      status: response.code.to_i,
      duration_ms: duration_ms
    )
  end

  buffer = Fiber[:opentrace_buffer]
  if buffer
    vendor = OpenTrace::HttpTracker.infer_vendor(host)
    buffer.record_http(
      method: req.method,
      url: url,
      host: host,
      vendor: vendor,
      status: response.code.to_i,
      duration_ms: duration_ms,
      request_headers: nil,  # skip headers for now to save memory
      request_body: req_body,
      response_headers: nil,
      response_body: resp_body,
      response_size: response.body&.bytesize,
      retry_attempt: 0,
      error_class: nil
    )
    buffer.record_timeline(type: :http, name: "#{req.method} #{host}", duration_ms: duration_ms)
  end

  response
rescue IOError, SystemCallError, OpenSSL::SSL::SSLError, Timeout::Error, Net::ProtocolError => e
  # Record the failed HTTP call, then re-raise
  duration_ms = start_time ? (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000 : 0

  if collector
    collector.record_http(
      method: req&.method,
      url: "#{address}#{req&.path}",
      host: address,
      status: 0,
      duration_ms: duration_ms,
      error: e.class.name
    )
  end

  buffer = Fiber[:opentrace_buffer]
  if buffer
    vendor = OpenTrace::HttpTracker.infer_vendor(address)
    buffer.record_http(
      method: req&.method,
      url: "#{address}#{req&.path}",
      host: address,
      vendor: vendor,
      status: 0,
      duration_ms: duration_ms,
      request_body: req_body,
      response_body: nil,
      response_size: nil,
      error_class: e.class.name
    )
  end

  raise # ALWAYS re-raise — never swallow app errors
end