Class: CloseYourIt::Scope
- Inherits:
-
Object
- Object
- CloseYourIt::Scope
- Defined in:
- lib/closeyourit/scope.rb
Overview
Contesto per-richiesta (o per-job) isolato per execution-context (Fiber storage): user/tags/extra/contexts/request. Letto da ErrorEvent#to_h sul thread chiamante (sincrono) → il worker di invio non lo vede mai e lo scope non cola tra richieste.
Constant Summary collapse
- STORAGE_KEY =
:__closeyourit_scope
Instance Attribute Summary collapse
-
#breadcrumbs ⇒ Object
readonly
Returns the value of attribute breadcrumbs.
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#rack_env ⇒ Object
Returns the value of attribute rack_env.
-
#replay_session_id ⇒ Object
Returns the value of attribute replay_session_id.
-
#request ⇒ Object
Returns the value of attribute request.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#trace_context ⇒ Object
Returns the value of attribute trace_context.
-
#trace_id ⇒ Object
Returns the value of attribute trace_id.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.current ⇒ Object
Scope dell'execution-context corrente.
-
.current=(scope) ⇒ Object
Re-installa uno scope salvato in precedenza.
-
.reset! ⇒ Object
Azzera lo scope corrente — chiamato in
ensureda middleware e job (su Puma il thread è riusato: senza reset lo scope colerebbe nella richiesta successiva).
Instance Method Summary collapse
- #add_breadcrumb(breadcrumb) ⇒ Object
- #clear ⇒ Object
-
#initialize ⇒ Scope
constructor
A new instance of Scope.
-
#performance_profile ⇒ Object
Profilo di performance per-richiesta (query + HTTP esterne).
- #set_context(key, attributes) ⇒ Object
- #set_extra(key, value) ⇒ Object
- #set_tag(key, value) ⇒ Object
- #set_tags(attributes) ⇒ Object
- #set_user(attributes) ⇒ Object
-
#to_event_hash ⇒ Object
Sottoinsieme non vuoto in forma evento Sentry (user/tags/extra/contexts/request), fuso nel payload da ErrorEvent#to_h.
Constructor Details
#initialize ⇒ Scope
Returns a new instance of Scope.
48 49 50 |
# File 'lib/closeyourit/scope.rb', line 48 def initialize clear end |
Instance Attribute Details
#breadcrumbs ⇒ Object (readonly)
Returns the value of attribute breadcrumbs.
46 47 48 |
# File 'lib/closeyourit/scope.rb', line 46 def @breadcrumbs end |
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
46 47 48 |
# File 'lib/closeyourit/scope.rb', line 46 def contexts @contexts end |
#extra ⇒ Object (readonly)
Returns the value of attribute extra.
46 47 48 |
# File 'lib/closeyourit/scope.rb', line 46 def extra @extra end |
#rack_env ⇒ Object
Returns the value of attribute rack_env.
45 46 47 |
# File 'lib/closeyourit/scope.rb', line 45 def rack_env @rack_env end |
#replay_session_id ⇒ Object
Returns the value of attribute replay_session_id.
45 46 47 |
# File 'lib/closeyourit/scope.rb', line 45 def replay_session_id @replay_session_id end |
#request ⇒ Object
Returns the value of attribute request.
45 46 47 |
# File 'lib/closeyourit/scope.rb', line 45 def request @request end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
46 47 48 |
# File 'lib/closeyourit/scope.rb', line 46 def @tags end |
#trace_context ⇒ Object
Returns the value of attribute trace_context.
45 46 47 |
# File 'lib/closeyourit/scope.rb', line 45 def trace_context @trace_context end |
#trace_id ⇒ Object
Returns the value of attribute trace_id.
45 46 47 |
# File 'lib/closeyourit/scope.rb', line 45 def trace_id @trace_id end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
46 47 48 |
# File 'lib/closeyourit/scope.rb', line 46 def user @user end |
Class Method Details
.current ⇒ Object
Scope dell'execution-context corrente. Usa ActiveSupport::IsolatedExecutionState quando
presente (rispetta isolation_level: thread su Puma, fiber su Falcon), altrimenti
Thread.current (thread-local puro, NON ereditato dai thread figli → niente bleed).
17 18 19 |
# File 'lib/closeyourit/scope.rb', line 17 def current store[STORAGE_KEY] ||= new end |
.current=(scope) ⇒ Object
Re-installa uno scope salvato in precedenza. Serve a after_discard (ActiveJob 7.1+) per
riprendere lo scope arricchito durante il perform — tag/contesti/breadcrumb, incluse le query —
dopo che il reset di fine perform l'ha sganciato dallo storage (CYRB-19). Simmetrico a reset!.
24 25 26 |
# File 'lib/closeyourit/scope.rb', line 24 def current=(scope) store[STORAGE_KEY] = scope end |
.reset! ⇒ Object
Azzera lo scope corrente — chiamato in ensure da middleware e job (su Puma il
thread è riusato: senza reset lo scope colerebbe nella richiesta successiva).
30 31 32 |
# File 'lib/closeyourit/scope.rb', line 30 def reset! store[STORAGE_KEY] = nil end |
Instance Method Details
#add_breadcrumb(breadcrumb) ⇒ Object
72 73 74 |
# File 'lib/closeyourit/scope.rb', line 72 def () @breadcrumbs.add() end |
#clear ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/closeyourit/scope.rb', line 82 def clear @user = {} @tags = {} @extra = {} @contexts = {} @request = nil @rack_env = nil @trace_id = nil @replay_session_id = nil # Contesto di trace W3C della richiesta (CloseYourIt::TraceContext): popolato solo con la # propagazione opt-in attiva, consumato dal patch Net::HTTP per gli header d'uscita. @trace_context = nil @breadcrumbs = BreadcrumbBuffer.new(CloseYourIt.configuration.) @performance_profile = nil end |
#performance_profile ⇒ Object
Profilo di performance per-richiesta (query + HTTP esterne). Lazy: creato al primo accesso, azzerato da #clear a fine richiesta. Il verdetto lo calcola Subscribers::RequestPerformance.
78 79 80 |
# File 'lib/closeyourit/scope.rb', line 78 def performance_profile @performance_profile ||= Performance::RequestProfile.new end |
#set_context(key, attributes) ⇒ Object
64 65 66 |
# File 'lib/closeyourit/scope.rb', line 64 def set_context(key, attributes) @contexts[key.to_s] = stringify_keys(attributes) end |
#set_extra(key, value) ⇒ Object
68 69 70 |
# File 'lib/closeyourit/scope.rb', line 68 def set_extra(key, value) @extra[key.to_s] = value end |
#set_tag(key, value) ⇒ Object
56 57 58 |
# File 'lib/closeyourit/scope.rb', line 56 def set_tag(key, value) @tags[key.to_s] = value end |
#set_tags(attributes) ⇒ Object
60 61 62 |
# File 'lib/closeyourit/scope.rb', line 60 def (attributes) attributes.each { |key, value| set_tag(key, value) } end |
#set_user(attributes) ⇒ Object
52 53 54 |
# File 'lib/closeyourit/scope.rb', line 52 def set_user(attributes) @user.merge!(stringify_keys(attributes)) end |
#to_event_hash ⇒ Object
Sottoinsieme non vuoto in forma evento Sentry (user/tags/extra/contexts/request), fuso nel payload da ErrorEvent#to_h. tags/extra/contexts passano dallo Scrubber (denylist ricorsiva per chiave): il backend NON li ri-scruba (Errors::Ingest::Normalize li conserva verbatim), quindi questa è l'unica rete di sicurezza contro le chiavi sensibili lì — R2.
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/closeyourit/scope.rb', line 102 def to_event_hash { "user" => serialize_user, "tags" => scrub(presence(@tags)), "extra" => scrub(presence(@extra)), "contexts" => contexts_payload, "request" => request_payload, "breadcrumbs" => }.reject { |_key, value| value.nil? } end |