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.



41
42
43
# File 'lib/closeyourit/scope.rb', line 41

def initialize
  clear
end

Instance Attribute Details

Returns the value of attribute breadcrumbs.



39
40
41
# File 'lib/closeyourit/scope.rb', line 39

def breadcrumbs
  @breadcrumbs
end

#contextsObject (readonly)

Returns the value of attribute contexts.



39
40
41
# File 'lib/closeyourit/scope.rb', line 39

def contexts
  @contexts
end

#extraObject (readonly)

Returns the value of attribute extra.



39
40
41
# File 'lib/closeyourit/scope.rb', line 39

def extra
  @extra
end

#requestObject

Returns the value of attribute request.



38
39
40
# File 'lib/closeyourit/scope.rb', line 38

def request
  @request
end

#tagsObject (readonly)

Returns the value of attribute tags.



39
40
41
# File 'lib/closeyourit/scope.rb', line 39

def tags
  @tags
end

#trace_idObject

Returns the value of attribute trace_id.



38
39
40
# File 'lib/closeyourit/scope.rb', line 38

def trace_id
  @trace_id
end

#userObject (readonly)

Returns the value of attribute user.



39
40
41
# File 'lib/closeyourit/scope.rb', line 39

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

.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).



23
24
25
# File 'lib/closeyourit/scope.rb', line 23

def reset!
  store[STORAGE_KEY] = nil
end

Instance Method Details

#add_breadcrumb(breadcrumb) ⇒ Object



65
66
67
# File 'lib/closeyourit/scope.rb', line 65

def add_breadcrumb(breadcrumb)
  @breadcrumbs.add(breadcrumb)
end

#clearObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/closeyourit/scope.rb', line 75

def clear
  @user        = {}
  @tags        = {}
  @extra       = {}
  @contexts    = {}
  @request     = nil
  @trace_id    = 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.



71
72
73
# File 'lib/closeyourit/scope.rb', line 71

def performance_profile
  @performance_profile ||= Performance::RequestProfile.new
end

#set_context(key, attributes) ⇒ Object



57
58
59
# File 'lib/closeyourit/scope.rb', line 57

def set_context(key, attributes)
  @contexts[key.to_s] = stringify_keys(attributes)
end

#set_extra(key, value) ⇒ Object



61
62
63
# File 'lib/closeyourit/scope.rb', line 61

def set_extra(key, value)
  @extra[key.to_s] = value
end

#set_tag(key, value) ⇒ Object



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

def set_tag(key, value)
  @tags[key.to_s] = value
end

#set_tags(attributes) ⇒ Object



53
54
55
# File 'lib/closeyourit/scope.rb', line 53

def set_tags(attributes)
  attributes.each { |key, value| set_tag(key, value) }
end

#set_user(attributes) ⇒ Object



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

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.



90
91
92
93
94
95
96
97
98
99
# File 'lib/closeyourit/scope.rb', line 90

def to_event_hash
  {
    "user"        => serialize_user,
    "tags"        => scrub(presence(@tags)),
    "extra"       => scrub(presence(@extra)),
    "contexts"    => scrub(presence(@contexts)),
    "request"     => @request,
    "breadcrumbs" => breadcrumbs_payload
  }.reject { |_key, value| value.nil? }
end