Class: DebugBundle::Client
- Inherits:
-
Object
- Object
- DebugBundle::Client
- 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
- 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.
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 |
# File 'lib/debugbundle/client.rb', line 81 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 @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.
42 43 44 |
# File 'lib/debugbundle/client.rb', line 42 def thread_exception_client @thread_exception_client end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
39 40 41 |
# File 'lib/debugbundle/client.rb', line 39 def config @config end |
#last_event_at ⇒ Object (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 |
# File 'lib/debugbundle/client.rb', line 44 def dispatch_thread_exception(error) = thread_exception_client&.__send__(:capture_thread_exception, error) |
.install_thread_exception_hook! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/debugbundle/client.rb', line 46 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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/debugbundle/client.rb', line 69 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
367 |
# File 'lib/debugbundle/client.rb', line 367 def buffered_event_count = @buffer_mutex.synchronize { @buffer.length } |
#capture_at_exit ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/debugbundle/client.rb', line 234 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
142 |
# File 'lib/debugbundle/client.rb', line 142 def capture_error(error, context: nil, handled: true) = capture_exception(error, context: context, handled: handled) |
#capture_exception(error, context: nil, handled: true) ⇒ Object
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 139 140 |
# File 'lib/debugbundle/client.rb', line 112 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..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) probe_data = probe_snapshot payload['probe_data'] = probe_data unless probe_data.empty? extra_context = merged_context.except('request', 'response', 'correlation') extra_context['causes'] = causes unless causes.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, extra_context)) end |
#capture_exceptions ⇒ Object
227 228 229 230 231 232 |
# File 'lib/debugbundle/client.rb', line 227 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/debugbundle/client.rb', line 144 def capture_log(, 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' => .to_s, 'attributes' => merged_context } enqueue_event(base_event('log_event', payload, merged_context)) end |
#capture_logger(logger = ::Logger.new($stdout)) ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/debugbundle/client.rb', line 249 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
187 188 189 |
# File 'lib/debugbundle/client.rb', line 187 def (, level: nil, context: nil) capture_log(, level: level || :info, context: context) end |
#capture_request(request, response, context: nil) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/debugbundle/client.rb', line 161 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, sanitized_request) 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'] } enqueue_event(base_event('request_event', payload, merged_context.merge('request' => sanitized_request))) end |
#capture_semantic_logger ⇒ Object
257 258 259 |
# File 'lib/debugbundle/client.rb', line 257 def capture_semantic_logger @capture_semantic_logger ||= Logging.install_semantic_logger(client: self) end |
#flush ⇒ Object
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/debugbundle/client.rb', line 313 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/debugbundle/client.rb', line 196 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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/debugbundle/client.rb', line 275 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
191 192 193 194 |
# File 'lib/debugbundle/client.rb', line 191 def set_context(key, value) @context[key.to_s] = @redactor.redact_value(value) value end |
#status ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/debugbundle/client.rb', line 358 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
306 307 308 309 310 311 |
# File 'lib/debugbundle/client.rb', line 306 def with_exception_capture(context: nil) yield rescue StandardError => e capture_exception(e, context: context, handled: false) raise end |
#with_request_trigger(request) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/debugbundle/client.rb', line 261 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 |