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
# 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]

  # Trace context injection is bookkeeping — must never raise to the host.
  begin
    inject_trace_context(req) if OpenTrace.config.trace_propagation
  rescue StandardError
  end

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    response = super
  rescue IOError, SystemCallError, OpenSSL::SSL::SSLError, Timeout::Error, Net::ProtocolError => e
    # Real network error from the host's HTTP call. Record it, then re-raise
    # so the host app sees the error it's expecting to handle.
    record_http_failure(req, e, start_time) rescue nil
    raise
  end

  # Bookkeeping for a successful response. Any bug here (NoMethodError on
  # a streaming body, a payload builder regression, etc.) must NOT bubble
  # into host code — swallow and move on.
  record_http_success(req, response, start_time) rescue nil

  response
end