Class: CloseYourIt::Scope

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScope

Returns a new instance of Scope.



48
49
50
# File 'lib/closeyourit/scope.rb', line 48

def initialize
  clear
end

Instance Attribute Details

Returns the value of attribute breadcrumbs.



46
47
48
# File 'lib/closeyourit/scope.rb', line 46

def breadcrumbs
  @breadcrumbs
end

#contextsObject (readonly)

Returns the value of attribute contexts.



46
47
48
# File 'lib/closeyourit/scope.rb', line 46

def contexts
  @contexts
end

#extraObject (readonly)

Returns the value of attribute extra.



46
47
48
# File 'lib/closeyourit/scope.rb', line 46

def extra
  @extra
end

#rack_envObject

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_idObject

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

#requestObject

Returns the value of attribute request.



45
46
47
# File 'lib/closeyourit/scope.rb', line 45

def request
  @request
end

#tagsObject (readonly)

Returns the value of attribute tags.



46
47
48
# File 'lib/closeyourit/scope.rb', line 46

def tags
  @tags
end

#trace_contextObject

Returns the value of attribute trace_context.



45
46
47
# File 'lib/closeyourit/scope.rb', line 45

def trace_context
  @trace_context
end

#trace_idObject

Returns the value of attribute trace_id.



45
46
47
# File 'lib/closeyourit/scope.rb', line 45

def trace_id
  @trace_id
end

#userObject (readonly)

Returns the value of attribute user.



46
47
48
# File 'lib/closeyourit/scope.rb', line 46

def user
  @user
end

Class Method Details

.currentObject

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 add_breadcrumb(breadcrumb)
  @breadcrumbs.add(breadcrumb)
end

#clearObject



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.max_breadcrumbs)
  @performance_profile = nil
end

#performance_profileObject

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 set_tags(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_hashObject

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" => breadcrumbs_payload
  }.reject { |_key, value| value.nil? }
end