Module: AllStak::Integrations::NetHTTP::Patch

Defined in:
lib/allstak/integrations/net_http.rb

Instance Method Summary collapse

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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/allstak/integrations/net_http.rb', line 22

def request(req, body = nil, &block)
  return super unless AllStak.initialized?

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  host = (req["Host"] || address).to_s
  path = begin
    req.path.to_s
  rescue
    "/"
  end
  method = req.method.to_s.upcase
  status = 0
  resp_size = 0
  req_size = req.body.to_s.bytesize rescue 0
  error_fp = nil

  client = AllStak.client
  # Short-circuit: do NOT instrument our own ingest calls
  return super if host.include?("ingest") || host_matches_allstak?(host)

  begin
    response = super
    status = response.code.to_i
    resp_size = response.body.to_s.bytesize rescue 0
    response
  rescue => e
    error_fp = e.class.name
    raise
  ensure
    begin
      duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).to_i
      client.http.record(
        direction: "outbound",
        method: method,
        host: host,
        path: path,
        status_code: status,
        duration_ms: duration,
        request_size: req_size,
        response_size: resp_size,
        trace_id: client.tracing.current_trace_id,
        error_fingerprint: error_fp
      )
    rescue
      # never raise into host
    end
  end
end