Class: DebugBundle::Client
- Inherits:
-
Object
- Object
- DebugBundle::Client
- Includes:
- EventSupport
- Defined in:
- lib/debugbundle/client.rb,
lib/debugbundle/client_event_support.rb
Defined Under Namespace
Modules: EventSupport
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
- 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
-
.thread_exception_client ⇒ Object
Returns the value of attribute thread_exception_client.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#last_event_at ⇒ Object
readonly
Returns the value of attribute last_event_at.
Class Method Summary collapse
- .dispatch_thread_exception(error) ⇒ Object
- .install_thread_exception_hook! ⇒ Object
- .wrap_thread_block(block) ⇒ Object
Instance Method Summary collapse
- #buffered_event_count ⇒ Object
- #capture_at_exit ⇒ Object
- #capture_error(error, context: nil, handled: true) ⇒ Object
- #capture_exception(error, context: nil, handled: true) ⇒ Object
- #capture_exceptions ⇒ Object
- #capture_log(message, level: :warning, context: nil) ⇒ Object
- #capture_logger(logger = ::Logger.new($stdout)) ⇒ Object
- #capture_message(message, level: nil, context: nil) ⇒ Object
- #capture_request(request, response, context: nil) ⇒ Object
- #capture_semantic_logger ⇒ Object
- #flush ⇒ Object
-
#initialize(transport: nil, time_provider: nil, random_provider: nil, config_fetcher: nil, **options) ⇒ Client
constructor
A new instance of Client.
- #probe(label, data = nil, heavy: false, &block) ⇒ Object
- #refresh_remote_config! ⇒ Object
- #set_context(key, value) ⇒ Object
- #status ⇒ Object
- #with_exception_capture(context: nil) ⇒ Object
- #with_request_trigger(request) ⇒ Object
Constructor Details
#initialize(transport: nil, time_provider: nil, random_provider: nil, config_fetcher: nil, **options) ⇒ Client
Returns a new instance of Client.
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 |
# File 'lib/debugbundle/client.rb', line 82 def initialize(transport: nil, time_provider: nil, random_provider: nil, config_fetcher: nil, **) @config = Config.new(**) @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 @acknowledgement_state = nil @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_client ⇒ Object
Returns the value of attribute thread_exception_client.
43 44 45 |
# File 'lib/debugbundle/client.rb', line 43 def thread_exception_client @thread_exception_client end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
40 41 42 |
# File 'lib/debugbundle/client.rb', line 40 def config @config end |
#last_event_at ⇒ Object (readonly)
Returns the value of attribute last_event_at.
40 41 42 |
# File 'lib/debugbundle/client.rb', line 40 def last_event_at @last_event_at end |
Class Method Details
.dispatch_thread_exception(error) ⇒ Object
45 |
# File 'lib/debugbundle/client.rb', line 45 def dispatch_thread_exception(error) = thread_exception_client&.__send__(:capture_thread_exception, error) |
.install_thread_exception_hook! ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/debugbundle/client.rb', line 47 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
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/debugbundle/client.rb', line 70 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_count ⇒ Object
397 |
# File 'lib/debugbundle/client.rb', line 397 def buffered_event_count = @buffer_mutex.synchronize { @buffer.length } |
#capture_at_exit ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/debugbundle/client.rb', line 261 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.__send__( :capture_exception_internal, error, context: nil, handled: false, run_before_send: false ) client.flush end true end |
#capture_error(error, context: nil, handled: true) ⇒ Object
159 |
# File 'lib/debugbundle/client.rb', line 159 def capture_error(error, context: nil, handled: true) = capture_exception(error, context: context, handled: handled) |
#capture_exception(error, context: nil, handled: true) ⇒ Object
114 115 116 |
# File 'lib/debugbundle/client.rb', line 114 def capture_exception(error, context: nil, handled: true) capture_exception_internal(error, context: context, handled: handled, run_before_send: true) end |
#capture_exceptions ⇒ Object
254 255 256 257 258 259 |
# File 'lib/debugbundle/client.rb', line 254 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/debugbundle/client.rb', line 161 def capture_log(, level: :warning, context: nil) return unless capture_enabled? poll_remote_config_if_due! normalized_level = normalize_level(level || :warning) merged_context = merge_context(context) payload = { 'level' => normalized_level.to_s, 'message' => .to_s, 'attributes' => merged_context } event = apply_before_send(base_event('log_event', payload, merged_context)) return if event.nil? || !level_enabled?(normalized_level) enqueue_event(event) end |
#capture_logger(logger = ::Logger.new($stdout)) ⇒ Object
282 283 284 285 286 287 288 |
# File 'lib/debugbundle/client.rb', line 282 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
210 211 212 |
# File 'lib/debugbundle/client.rb', line 210 def (, level: nil, context: nil) capture_log(, level: level || :info, context: context) end |
#capture_request(request, response, context: nil) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/debugbundle/client.rb', line 180 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 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'], 'response_headers' => sanitized_response['headers'], 'response_body' => sanitized_response['body'] } event = apply_before_send( base_event('request_event', payload, merged_context.merge('request' => sanitized_request)) ) return if event.nil? || !capture_request_event?(response_status, sanitized_request) enqueue_event(event) end |
#capture_semantic_logger ⇒ Object
290 291 292 |
# File 'lib/debugbundle/client.rb', line 290 def capture_semantic_logger @capture_semantic_logger ||= Logging.install_semantic_logger(client: self) end |
#flush ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/debugbundle/client.rb', line 346 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 handle_successful_result(result, batch) 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
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/debugbundle/client.rb', line 219 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? resolved, raw_value = resolve_probe_value(data, block) return unless resolved emit_probe_events(label.to_s, normalize_probe_data(raw_value), matching_directives) return end return if !@probe_buffers.key?(label) && @probe_buffers.size >= config.max_probe_labels resolved, raw_value = resolve_probe_value(data, block) return unless resolved entry = { 'label' => label.to_s, 'data' => normalize_probe_data(raw_value), 'timestamp' => 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
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/debugbundle/client.rb', line 308 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
214 215 216 217 |
# File 'lib/debugbundle/client.rb', line 214 def set_context(key, value) @context[key.to_s] = @redactor.redact_value(value) value end |
#status ⇒ Object
387 388 389 390 391 392 393 394 395 |
# File 'lib/debugbundle/client.rb', line 387 def status return :disconnected unless config.enabled? return :degraded unless config.configured? return @acknowledgement_state if @acknowledgement_state return :disconnected if @consecutive_failures >= 3 return :degraded if rate_limited? :healthy end |
#with_exception_capture(context: nil) ⇒ Object
339 340 341 342 343 344 |
# File 'lib/debugbundle/client.rb', line 339 def with_exception_capture(context: nil) yield rescue StandardError => e capture_exception(e, context: context, handled: false) raise end |
#with_request_trigger(request) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/debugbundle/client.rb', line 294 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 |