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.
- #breadcrumb_count ⇒ Object
- #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
- #captured_count ⇒ Object
- #clear_user ⇒ Object
- #dropped_count ⇒ 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 27 28 |
# 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 @captured_count = 0 @dropped_count = 0 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/allstak/modules/errors.rb', line 46 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 |
#breadcrumb_count ⇒ Object
177 178 179 180 181 182 |
# File 'lib/allstak/modules/errors.rb', line 177 def buffer = Thread.current[BREADCRUMB_TLS_KEY] buffer.respond_to?(:length) ? buffer.length : 0 rescue StandardError 0 end |
#capture_error(exception_class, message, stack_trace: nil, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) ⇒ Object
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/allstak/modules/errors.rb', line 128 def capture_error(exception_class, , stack_trace: nil, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) if @transport.disabled? @dropped_count += 1 return nil end begin @captured_count += 1 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. unless Sampling.sampled?(@config.sample_rate) @dropped_count += 1 return nil end payload = apply_before_send(payload) if payload.nil? @dropped_count += 1 return nil end 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
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 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/allstak/modules/errors.rb', line 62 def capture_exception(exc, level: "error", user: nil, request_context: nil, trace_id: nil, metadata: nil) if @transport.disabled? @dropped_count += 1 return nil end begin @captured_count += 1 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. unless Sampling.sampled?(@config.sample_rate) @dropped_count += 1 return nil end payload = apply_before_send(payload) if payload.nil? @dropped_count += 1 return nil end 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 |
#captured_count ⇒ Object
184 185 186 |
# File 'lib/allstak/modules/errors.rb', line 184 def captured_count @captured_count end |
#clear_user ⇒ Object
34 35 36 |
# File 'lib/allstak/modules/errors.rb', line 34 def clear_user @current_user = nil end |
#dropped_count ⇒ Object
188 189 190 |
# File 'lib/allstak/modules/errors.rb', line 188 def dropped_count @dropped_count end |
#set_user(id: nil, email: nil, ip: nil) ⇒ Object
30 31 32 |
# File 'lib/allstak/modules/errors.rb', line 30 def set_user(id: nil, email: nil, ip: nil) @current_user = Models::UserContext.new(id: id, email: email, ip: ip) end |