Class: Errsight::Hub
- Inherits:
-
Object
- Object
- Errsight::Hub
- Defined in:
- lib/errsight/hub.rb
Overview
Per-thread scope stack. The top of the stack is the “current” scope read by Errsight.log when building events.
Rails request lifecycle pushes a fresh scope in CaptureMiddleware so any set_user/set_tag/add_breadcrumb calls made during a request are isolated to that request and don’t leak to the next request handled by the same Puma thread. Same pattern applies to Sidekiq job middleware.
Class Method Summary collapse
- .current ⇒ Object
-
.reset_current! ⇒ Object
Test-only: drop the per-thread hub so each test starts with a clean scope stack regardless of which thread runs it.
Instance Method Summary collapse
- #current_scope ⇒ Object
-
#initialize ⇒ Hub
constructor
A new instance of Hub.
- #pop_scope ⇒ Object
-
#push_scope(scope = nil) ⇒ Object
Push either a fresh fork of the current scope (default) or a specific scope (used by Sidekiq server middleware to rehydrate scope from a job payload).
-
#with_scope(scope = nil) ⇒ Object
When called with no argument, forks a fresh dup of the current scope for the block (Rails request middleware, ad-hoc isolation).
Constructor Details
Class Method Details
.current ⇒ Object
10 11 12 |
# File 'lib/errsight/hub.rb', line 10 def self.current Thread.current[:errsight_hub] ||= new end |
.reset_current! ⇒ Object
Test-only: drop the per-thread hub so each test starts with a clean scope stack regardless of which thread runs it.
16 17 18 |
# File 'lib/errsight/hub.rb', line 16 def self.reset_current! Thread.current[:errsight_hub] = nil end |
Instance Method Details
#current_scope ⇒ Object
24 25 26 |
# File 'lib/errsight/hub.rb', line 24 def current_scope @stack.last end |
#pop_scope ⇒ Object
36 37 38 39 40 |
# File 'lib/errsight/hub.rb', line 36 def pop_scope # Always keep at least one scope on the stack so callers can never end # up with a nil current_scope after an over-eager pop. @stack.pop if @stack.size > 1 end |
#push_scope(scope = nil) ⇒ Object
Push either a fresh fork of the current scope (default) or a specific scope (used by Sidekiq server middleware to rehydrate scope from a job payload).
31 32 33 34 |
# File 'lib/errsight/hub.rb', line 31 def push_scope(scope = nil) @stack.push(scope || current_scope.dup) current_scope end |
#with_scope(scope = nil) ⇒ Object
When called with no argument, forks a fresh dup of the current scope for the block (Rails request middleware, ad-hoc isolation). When called with an explicit scope, pushes that one as-is — used by Sidekiq server middleware to install a job’s rehydrated scope.
46 47 48 49 50 51 |
# File 'lib/errsight/hub.rb', line 46 def with_scope(scope = nil) push_scope(scope) yield current_scope ensure pop_scope end |