Module: Apidepth::NetHTTPInstrumentation

Defined in:
lib/apidepth/net_http_instrumentation.rb

Instance Method Summary collapse

Instance Method Details

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apidepth/net_http_instrumentation.rb', line 5

def request(req, body = nil, &block)
  # Early exits — evaluated in order of cheapness:
  # 1. Recursion guard: we're inside our own collector flush
  # 2. SDK disabled entirely
  # 3. Host is on the customer's ignore list
  # 4. Sample rate: probabilistically skip events
  return super if Thread.current[:apidepth_skip]
  return super unless Apidepth.configuration.enabled
  return super if Apidepth.configuration.ignored_hosts.include?(address)
  return super unless sampled?

  # Snapshot connection state BEFORE calling super.
  # started? returns true if a keep-alive connection is already open.
  # cold_start events pay for DNS + SSL — that latency belongs to the
  # customer's infrastructure, not the vendor. Tag it so the collector
  # can exclude cold-start events from latency percentile calculations.
  cold_start = !started?

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    response    = super
    duration_ms = elapsed_ms(start)
    record_event(req, response, duration_ms, cold_start: cold_start)
    response
  rescue Net::OpenTimeout, Net::ReadTimeout => e
    # Timeouts are the leading indicator of vendor degradation — they
    # appear before the vendor acknowledges an incident. We record them
    # and always re-raise so the customer's error handling is unaffected.
    duration_ms = elapsed_ms(start)
    record_timeout(req, duration_ms, e.class.name, cold_start: cold_start)
    raise
  end
end