Module: LogBrew::HttpClientTracing
- Defined in:
- lib/logbrew/http_client_tracing.rb
Defined Under Namespace
Classes: FaradayHeaderSnapshot, NetHttpHeaderSnapshot
Class Method Summary
collapse
Class Method Details
.active_operation? ⇒ Boolean
227
228
229
|
# File 'lib/logbrew/http_client_tracing.rb', line 227
def active_operation?
!Thread.current[ACTIVE_OPERATION_KEY].nil?
end
|
.monotonic_time ⇒ Object
272
273
274
|
# File 'lib/logbrew/http_client_tracing.rb', line 272
def monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
|
.normalize_host(host) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/logbrew/http_client_tracing.rb', line 240
def normalize_host(host)
value = host.to_s.strip.downcase.sub(/\.+\z/, "")
return nil if value.empty? || value.bytesize > 253
return nil if value.match?(/\A[0-9.]+\z/)
return nil unless value.match?(/\A[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?\z/)
begin
IPAddr.new(value)
nil
rescue IPAddr::InvalidAddressError
value
end
end
|
.normalize_method(method) ⇒ Object
235
236
237
238
|
# File 'lib/logbrew/http_client_tracing.rb', line 235
def normalize_method(method)
value = method.to_s.strip.upcase
value.empty? ? "HTTP" : value[0, 32]
end
|
.normalize_status_code(status_code) ⇒ Object
254
255
256
257
|
# File 'lib/logbrew/http_client_tracing.rb', line 254
def normalize_status_code(status_code)
value = status_code.to_i
value.positive? && value <= 999 ? value : nil
end
|
.prepare(client:, source:, on_capture_error: nil) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/logbrew/http_client_tracing.rb', line 190
def prepare(client:, source:, on_capture_error: nil)
parent = Trace.current
return nil unless parent
return nil if suppressed? || active_operation?
return nil unless SOURCES.include?(source)
method, host, = yield
operation = HttpClientTraceOperation.new(
client: client,
parent: parent,
source: source,
method: method,
host: host,
on_capture_error: on_capture_error
)
[operation, ]
rescue StandardError => error
report_capture_error(on_capture_error, error)
nil
end
|
.read_status(response, method, operation) ⇒ Object
259
260
261
262
263
264
|
# File 'lib/logbrew/http_client_tracing.rb', line 259
def read_status(response, method, operation)
response.public_send(method)
rescue StandardError => error
operation.capture_error(error)
nil
end
|
.report_capture_error(callback, error) ⇒ Object
276
277
278
279
280
|
# File 'lib/logbrew/http_client_tracing.rb', line 276
def report_capture_error(callback, error)
callback.call(error) if callback.respond_to?(:call)
rescue StandardError
nil
end
|
266
267
268
269
270
|
# File 'lib/logbrew/http_client_tracing.rb', line 266
def (, operation)
.reset
rescue StandardError => error
operation.capture_error(error)
end
|
.suppress ⇒ Object
219
220
221
222
223
224
225
|
# File 'lib/logbrew/http_client_tracing.rb', line 219
def suppress
previous = Thread.current[SUPPRESSION_KEY]
Thread.current[SUPPRESSION_KEY] = true
yield
ensure
Thread.current[SUPPRESSION_KEY] = previous
end
|
.suppressed? ⇒ Boolean
231
232
233
|
# File 'lib/logbrew/http_client_tracing.rb', line 231
def suppressed?
Thread.current[SUPPRESSION_KEY] == true
end
|
.with_operation(operation) ⇒ Object
211
212
213
214
215
216
217
|
# File 'lib/logbrew/http_client_tracing.rb', line 211
def with_operation(operation)
previous = Thread.current[ACTIVE_OPERATION_KEY]
Thread.current[ACTIVE_OPERATION_KEY] = operation
Trace.with_context(operation.context) { yield }
ensure
Thread.current[ACTIVE_OPERATION_KEY] = previous
end
|
.wrap_net_http(http, client:, on_capture_error: nil) ⇒ Object
184
185
186
187
188
|
# File 'lib/logbrew/http_client_tracing.rb', line 184
def wrap_net_http(http, client:, on_capture_error: nil)
return http if http.is_a?(NetHttpTracingClient)
NetHttpTracingClient.new(http, client: client, on_capture_error: on_capture_error)
end
|