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)
return super unless OpenTrace.enabled?
return super if Fiber[:opentrace_http_tracking_disabled]
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}"
req_body = nil
if req.body && req.body.is_a?(String) && req.body.bytesize < MAX_BODY_CAPTURE_BYTES
req_body = req.body
end
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, 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
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 end
|