Class: DebugBundle::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/debugbundle/client.rb

Constant Summary collapse

SCHEMA_VERSION =
'2026-03-01'
SDK_NAME =
'@debugbundle/sdk-ruby'
DEFAULT_SERVICE_NAME =
'ruby-service'
DEFAULT_ENVIRONMENT =
'development'
MAX_BUFFER_SIZE =
1_000
RETRY_AFTER_CAP_SECONDS =
300
DEFAULT_HEADER_ALLOWLIST =
%w[
  user-agent
  content-type
  accept
  x-request-id
  x-correlation-id
  x-debugbundle-trace-id
].freeze
BALANCED_IMMEDIATE_REQUEST_STATUSES =
[408, 423, 424, 425, 429].freeze
INVESTIGATIVE_IMMEDIATE_REQUEST_STATUSES =
(BALANCED_IMMEDIATE_REQUEST_STATUSES + [409]).freeze
BALANCED_ANOMALY_REQUEST_STATUSES =
[400, 401, 403, 404, 409, 410, 422].freeze
LOCAL_ENVIRONMENTS =
%w[development local test].freeze
REQUEST_TRIGGER_DIRECTIVES_KEY =
:__debugbundle_request_trigger_directives__
THREAD_HOOK_MUTEX =
Mutex.new
LOG_LEVEL_RANKS =
{
  debug: 10,
  info: 20,
  warning: 30,
  error: 40,
  fatal: 50,
  critical: 50
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport: nil, time_provider: nil, random_provider: nil, config_fetcher: nil, **options) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/debugbundle/client.rb', line 83

def initialize(transport: nil, time_provider: nil, random_provider: nil, config_fetcher: nil, **options)
  @config = Config.new(**options)
  @time_provider = time_provider || -> { Time.now.utc }
  @random_provider = random_provider || -> { rand }
  @redactor = Redaction::Redactor.new(
    sensitive_fields: Redaction::DEFAULT_SENSITIVE_FIELDS + config.redact_fields
  )
  @transport = transport || build_default_transport
  @config_fetcher = config_fetcher || build_default_config_fetcher(custom_transport: !transport.nil?)
  @context = {}
  @buffer = []
  @buffer_mutex = Mutex.new
  @flush_mutex = Mutex.new
  @probe_buffers = {}
  @suppression = Suppression::Tracker.new
  @last_event_at = nil
  @retry_at = nil
  @consecutive_failures = 0
  @at_exit_registered = false
  @thread_exception_registered = false
  @logger_bindings = {}
  @capture_semantic_logger = nil
  @next_remote_config_poll_at = nil
  @remote_config_etag = nil
  @remote_config = RemoteConfig::Snapshot.default
  @capture_policy = @remote_config.capture_policy

  refresh_remote_config!
  @capture_policy = RemoteConfig.minimal_capture_policy if @config_fetcher && @remote_config_etag.nil?
end

Class Attribute Details

.thread_exception_clientObject

Returns the value of attribute thread_exception_client.



42
43
44
# File 'lib/debugbundle/client.rb', line 42

def thread_exception_client
  @thread_exception_client
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



39
40
41
# File 'lib/debugbundle/client.rb', line 39

def config
  @config
end

#last_event_atObject (readonly)

Returns the value of attribute last_event_at.



39
40
41
# File 'lib/debugbundle/client.rb', line 39

def last_event_at
  @last_event_at
end

Class Method Details

.dispatch_thread_exception(error) ⇒ Object



44
45
46
# File 'lib/debugbundle/client.rb', line 44

def dispatch_thread_exception(error)
  thread_exception_client&.__send__(:capture_thread_exception, error)
end

.install_thread_exception_hook!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/debugbundle/client.rb', line 48

def install_thread_exception_hook!
  THREAD_HOOK_MUTEX.synchronize do
    return if @thread_exception_hook_installed

    interceptor = Module.new do
      define_method(:new) do |*args, &block|
        super(*args, &DebugBundle::Client.wrap_thread_block(block))
      end

      define_method(:start) do |*args, &block|
        super(*args, &DebugBundle::Client.wrap_thread_block(block))
      end

      define_method(:fork) do |*args, &block|
        super(*args, &DebugBundle::Client.wrap_thread_block(block))
      end
    end

    ::Thread.singleton_class.prepend(interceptor)
    @thread_exception_hook_installed = true
  end
end

.wrap_thread_block(block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/debugbundle/client.rb', line 71

def wrap_thread_block(block)
  return nil unless block

  proc do |*thread_args|
    block.call(*thread_args)
  rescue StandardError => e
    dispatch_thread_exception(e)
    raise
  end
end

Instance Method Details

#buffered_event_countObject



374
375
376
# File 'lib/debugbundle/client.rb', line 374

def buffered_event_count
  @buffer_mutex.synchronize { @buffer.length }
end

#capture_at_exitObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/debugbundle/client.rb', line 241

def capture_at_exit
  return false if @at_exit_registered

  @at_exit_registered = true
  client = self
  at_exit do
    error = $ERROR_INFO
    next unless error.is_a?(Exception)

    client.capture_exception(error, handled: false)
    client.flush
  end
  true
end

#capture_error(error, context: nil, handled: true) ⇒ Object



145
146
147
# File 'lib/debugbundle/client.rb', line 145

def capture_error(error, context: nil, handled: true)
  capture_exception(error, context: context, handled: handled)
end

#capture_exception(error, context: nil, handled: true) ⇒ Object



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
139
140
141
142
143
# File 'lib/debugbundle/client.rb', line 114

def capture_exception(error, context: nil, handled: true)
  return unless capture_enabled?

  poll_remote_config_if_due!

  merged_context = merge_context(context)
  payload = {
    'name' => error.class.name,
    'message' => error.message.to_s,
    'stack' => Array(error.backtrace).join("\n"),
    'handled' => handled,
    'request' => request_payload(merged_context['request']),
    'response' => response_payload(merged_context['response']),
    'runtime' => runtime_payload
  }

  causes = exception_causes(error)
  payload['causes'] = causes unless causes.empty?

  probe_data = probe_snapshot
  payload['probe_data'] = probe_data unless probe_data.empty?

  extra_context = merged_context.except('request', 'response', 'correlation')
  payload['context'] = extra_context unless extra_context.empty?

  suppression_key = [payload['name'], payload['message'], payload['stack']].join(':')
  return unless @suppression.should_capture(suppression_key, now: monotonic_now)

  enqueue_event(base_event('backend_exception', payload, merged_context))
end

#capture_exceptionsObject



234
235
236
237
238
239
# File 'lib/debugbundle/client.rb', line 234

def capture_exceptions
  at_exit_registered = capture_at_exit
  thread_registered = capture_thread_exceptions

  at_exit_registered || thread_registered
end

#capture_log(message, level: :warning, context: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/debugbundle/client.rb', line 149

def capture_log(message, level: :warning, context: nil)
  return unless capture_enabled?

  poll_remote_config_if_due!

  normalized_level = normalize_level(level || :warning)
  return unless level_enabled?(normalized_level)

  merged_context = merge_context(context)
  payload = {
    'level' => normalized_level.to_s,
    'message' => message.to_s,
    'attributes' => merged_context
  }
  enqueue_event(base_event('log_event', payload, merged_context))
end

#capture_logger(logger = ::Logger.new($stdout)) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/debugbundle/client.rb', line 256

def capture_logger(logger = ::Logger.new($stdout))
  binding_key = logger.object_id
  return logger if @logger_bindings.key?(binding_key)

  @logger_bindings[binding_key] = Logging.install_stdlib_logger(logger, client: self)
  logger
end

#capture_message(message, level: nil, context: nil) ⇒ Object



194
195
196
# File 'lib/debugbundle/client.rb', line 194

def capture_message(message, level: nil, context: nil)
  capture_log(message, level: level || :info, context: context)
end

#capture_request(request, response, context: nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/debugbundle/client.rb', line 166

def capture_request(request, response, context: nil)
  return unless capture_enabled?

  poll_remote_config_if_due!

  merged_context = merge_context(context)
  sanitized_request = request_payload(request)
  sanitized_response = response_payload(response)
  response_status = (sanitized_response['status_code'] || 0).to_i
  return unless capture_request_event?(response_status)

  payload = {
    'method' => sanitized_request['method'],
    'path' => sanitized_request['path'],
    'query' => sanitized_request['query'],
    'headers' => sanitized_request['headers'],
    'body' => sanitized_request['body'],
    'response_status' => response_status,
    'duration_ms' => extract_duration_ms(merged_context, sanitized_response),
    'route_template' => merged_context['route_template'],
    'controller' => merged_context['controller'],
    'action' => merged_context['action'],
    'response_headers' => sanitized_response['headers'],
    'response_body' => sanitized_response['body']
  }
  enqueue_event(base_event('request_event', payload, merged_context.merge('request' => sanitized_request)))
end

#capture_semantic_loggerObject



264
265
266
# File 'lib/debugbundle/client.rb', line 264

def capture_semantic_logger
  @capture_semantic_logger ||= Logging.install_semantic_logger(client: self)
end

#flushObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/debugbundle/client.rb', line 320

def flush
  # rubocop:disable Metrics/BlockLength
  @flush_mutex.synchronize do
    append_suppression_aggregates
    batch = buffered_batch
    return true if batch.empty?
    return false if @transport.nil?
    return false if rate_limited?

    result = Transport.coerce_result(
      @transport.call(
        project_token: config.project_token,
        service_name: service_name,
        events: batch.map(&:dup)
      )
    )

    case result.status_code
    when 200..299
      remove_buffered_events(batch)
      @retry_at = nil
      @consecutive_failures = 0
      @last_event_at = now
      true
    when 429
      @consecutive_failures += 1
      retry_after_seconds = (result.retry_after_seconds || 1).clamp(1, RETRY_AFTER_CAP_SECONDS)
      @retry_at = now + retry_after_seconds
      false
    when 400..499
      remove_buffered_events(batch)
      @retry_at = nil
      @consecutive_failures = 0
      false
    else
      @consecutive_failures += 1
      false
    end
  end
  # rubocop:enable Metrics/BlockLength
rescue StandardError
  @consecutive_failures += 1
  false
end

#probe(label, data = nil, heavy: false, &block) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/debugbundle/client.rb', line 203

def probe(label, data = nil, heavy: false, &block)
  return unless capture_enabled?

  poll_remote_config_if_due!
  return unless @remote_config.probes_enabled

  matching_directives = matching_probe_directives(label)
  if heavy
    return if matching_directives.empty?

    raw_value = block ? block.call : data
    emit_probe_events(label.to_s, @redactor.redact_value(raw_value), matching_directives)
    return
  end

  return if !@probe_buffers.key?(label) && @probe_buffers.size >= config.max_probe_labels

  raw_value = block ? block.call : data
  entry = {
    'label' => label.to_s,
    'data' => @redactor.redact_value(raw_value),
    'occurred_at' => now.iso8601
  }

  bucket = (@probe_buffers[label.to_s] ||= [])
  bucket << entry
  bucket.shift while bucket.length > config.max_probe_entries_per_label

  emit_probe_events(label.to_s, entry['data'], matching_directives)
end

#refresh_remote_config!Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/debugbundle/client.rb', line 282

def refresh_remote_config!
  return false unless capture_enabled?
  return false unless @config_fetcher

  response = @config_fetcher.call(@remote_config_etag)
  status_code = response.fetch(:status_code, 500)
  if status_code == 304
    schedule_next_remote_config_poll
    return true
  end
  unless status_code == 200
    schedule_next_remote_config_poll
    return false
  end

  snapshot = RemoteConfig.parse(response.fetch(:body, {}), config.probes_poll_interval)
  unless snapshot
    schedule_next_remote_config_poll
    return false
  end

  @remote_config = snapshot
  @capture_policy = snapshot.capture_policy
  @remote_config_etag = response[:etag]
  schedule_next_remote_config_poll
  true
rescue StandardError
  schedule_next_remote_config_poll
  false
end

#set_context(key, value) ⇒ Object



198
199
200
201
# File 'lib/debugbundle/client.rb', line 198

def set_context(key, value)
  @context[key.to_s] = @redactor.redact_value(value)
  value
end

#statusObject



365
366
367
368
369
370
371
372
# File 'lib/debugbundle/client.rb', line 365

def status
  return :disconnected unless config.enabled?
  return :degraded unless config.configured?
  return :disconnected if @consecutive_failures >= 3
  return :degraded if rate_limited?

  :healthy
end

#with_exception_capture(context: nil) ⇒ Object



313
314
315
316
317
318
# File 'lib/debugbundle/client.rb', line 313

def with_exception_capture(context: nil)
  yield
rescue StandardError => e
  capture_exception(e, context: context, handled: false)
  raise
end

#with_request_trigger(request) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/debugbundle/client.rb', line 268

def with_request_trigger(request)
  poll_remote_config_if_due! if capture_enabled?

  directives = TriggerToken.resolve_request_directives(
    request: request,
    trigger_token_key: @remote_config.trigger_token_key
  )
  previous = Thread.current[REQUEST_TRIGGER_DIRECTIVES_KEY]
  Thread.current[REQUEST_TRIGGER_DIRECTIVES_KEY] = directives
  yield
ensure
  Thread.current[REQUEST_TRIGGER_DIRECTIVES_KEY] = previous
end