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



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
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
# File 'lib/allstak/integrations/net_http.rb', line 24

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)
  trace_id = client.tracing.current_trace_id
  span_id = client.tracing.current_span_id
  request_id = SecureRandom.hex(16)
  AllStak::Propagation.apply_request_headers(req, trace_id: trace_id, request_id: request_id, span_id: span_id, sampled: client.tracing.current_trace_sampled?)

  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: trace_id,
        request_id: request_id,
        span_id: span_id,
        error_fingerprint: error_fp
      )

      # Outbound-call breadcrumb so it lands on the trail of any
      # exception captured later in the same thread. Auto-gated.
      client.errors.add_breadcrumb(
        type: "http",
        message: "#{method} #{host}#{path} #{status}",
        level: (error_fp || status >= 500) ? "error" : "info",
        data: {
          "direction"  => "outbound",
          "method"     => method,
          "host"       => host,
          "path"       => path,
          "status"     => status,
          "durationMs" => duration,
          "error"      => error_fp
        }.reject { |_, v| v.nil? },
        auto: true
      )
    rescue
      # never raise into host
    end
  end
end