Class: AllStak::Modules::Errors
- Inherits:
-
Object
- Object
- AllStak::Modules::Errors
- Defined in:
- lib/allstak/modules/errors.rb
Overview
Captures exceptions and sends them to AllStak.
Constant Summary collapse
- PATH =
"/ingest/v1/errors".freeze
- MAX_BREADCRUMBS =
50- BREADCRUMB_TLS_KEY =
Thread-local key for the per-thread breadcrumb ring buffer. Each request/job runs on its own thread, so a per-thread buffer keeps one thread’s breadcrumb trail from bleeding into another concurrent request’s captured exception.
:allstak_breadcrumbs
Instance Method Summary collapse
-
#add_breadcrumb(type:, message:, level: "info", data: nil, auto: false) ⇒ Object
Append a breadcrumb to the current thread’s ring buffer.
- #capture_error(exception_class, message, stack_trace: nil, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) ⇒ Object
- #capture_exception(exc, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) ⇒ Object
- #clear_user ⇒ Object
-
#initialize(transport, config, logger, session_id_provider: nil) ⇒ Errors
constructor
A new instance of Errors.
- #set_user(id: nil, email: nil, ip: nil) ⇒ Object
Constructor Details
#initialize(transport, config, logger, session_id_provider: nil) ⇒ Errors
Returns a new instance of Errors.
18 19 20 21 22 23 24 25 26 |
# File 'lib/allstak/modules/errors.rb', line 18 def initialize(transport, config, logger, session_id_provider: nil) @transport = transport @config = config @logger = logger @current_user = nil # Optional callable returning the active release-health session id, so # the backend's error consumer can mark the session errored/crashed. @session_id_provider = session_id_provider end |
Instance Method Details
#add_breadcrumb(type:, message:, level: "info", data: nil, auto: false) ⇒ Object
Append a breadcrumb to the current thread’s ring buffer.
‘auto: true` marks the crumb as produced by an auto-instrumentation layer (Rack/Net::HTTP/ActiveRecord/log bridge); those are suppressed when `config.enable_auto_breadcrumbs` is false. Manual breadcrumbs (`auto: false`, the default) are always recorded so existing callers (e.g. the Sidekiq middleware) keep working unchanged. Fail-open: a malformed crumb never raises into the host.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/allstak/modules/errors.rb', line 44 def (type:, message:, level: "info", data: nil, auto: false) return if auto && ! buffer = buffer.shift if buffer.length >= MAX_BREADCRUMBS buffer << { timestamp: Time.now.utc.iso8601(6), type: type, message: , level: level, data: data }.compact nil rescue StandardError nil end |
#capture_error(exception_class, message, stack_trace: nil, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) ⇒ Object
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 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/allstak/modules/errors.rb', line 116 def capture_error(exception_class, , stack_trace: nil, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) return nil if @transport.disabled? begin payload = { exceptionClass: exception_class, message: , stackTrace: stack_trace, level: level, environment: @config.environment, release: @config.release, traceId: trace_id, sessionId: current_session_id, user: (user || @current_user)&.to_h, requestContext: request_context&.to_h, metadata: @config..merge( || {}) }.compact payload.delete(:user) if payload[:user]&.empty? payload.delete(:requestContext) if payload[:requestContext]&.empty? # Sampling first, then pre-hook scrub, before_send, and final # transport scrub. return nil unless Sampling.sampled?(@config.sample_rate) payload = apply_before_send(payload) return nil if payload.nil? status, _ = @transport.post(PATH, payload) status == 202 ? exception_class : nil rescue Transport::AllStakAuthError nil rescue Transport::AllStakTransportError => e @transport.persist_failed(PATH, payload) if defined?(payload) && payload @logger.debug("[AllStak] capture_error transport error (spooled): #{e.}") nil rescue => e @logger.debug("[AllStak] capture_error swallowed: #{e.class}: #{e.}") nil end end |
#capture_exception(exc, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) ⇒ Object
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 |
# File 'lib/allstak/modules/errors.rb', line 60 def capture_exception(exc, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) return nil if @transport.disabled? begin crumbs = payload = { exceptionClass: exc.class.name, message: exc..to_s.empty? ? exc.class.name : exc..to_s, stackTrace: extract_frames(exc), level: level, environment: @config.environment, release: @config.release, # Phase 3 — v2 ingest contract: top-level identity + frames. sdkName: @config.sdk_name, sdkVersion: @config.sdk_version, platform: @config.platform, dist: @config.dist, frames: extract_structured_frames(exc), traceId: trace_id, # Release-health: top-level session id so the backend error # consumer can mark the session errored/crashed server-side. sessionId: current_session_id, user: (user || @current_user)&.to_h, requestContext: request_context&.to_h, metadata: @config..merge( || {}), breadcrumbs: crumbs }.compact payload.delete(:user) if payload[:user]&.empty? payload.delete(:requestContext) if payload[:requestContext]&.empty? # Sampling first, then pre-hook scrub, before_send, and final # transport scrub. Hooks never see raw secrets and cannot reintroduce # values that escape the wire-path sanitizer. return nil unless Sampling.sampled?(@config.sample_rate) payload = apply_before_send(payload) return nil if payload.nil? status, body = @transport.post(PATH, payload) return nil unless status == 202 parsed = JSON.parse(body) rescue nil parsed&.dig("data", "id") rescue Transport::AllStakAuthError nil rescue Transport::AllStakTransportError => e # Retries exhausted / network outage: persist the (scrubbed) error for # replay on the next init instead of dropping. `payload` is in scope # only after it is built; guard so a pre-build failure still no-ops. @transport.persist_failed(PATH, payload) if defined?(payload) && payload @logger.debug("[AllStak] capture_exception transport error (spooled): #{e.}") nil rescue => e @logger.debug("[AllStak] capture_exception swallowed: #{e.class}: #{e.}") nil end end |
#clear_user ⇒ Object
32 33 34 |
# File 'lib/allstak/modules/errors.rb', line 32 def clear_user @current_user = nil end |
#set_user(id: nil, email: nil, ip: nil) ⇒ Object
28 29 30 |
# File 'lib/allstak/modules/errors.rb', line 28 def set_user(id: nil, email: nil, ip: nil) @current_user = Models::UserContext.new(id: id, email: email, ip: ip) end |