Class: Datadog::OpenFeature::Hooks::SpanEnrichmentHook::SpanEnrichmentStateStore

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb

Overview

Maps each trace operation to its SpanEnrichmentState, keyed WEAKLY by object identity. Using ObjectSpace::WeakMap means an abandoned trace (root span never finishes) cannot pin its state: once the trace operation is unreachable the entry is collected. The state (the WeakMap value, which WeakMap would otherwise also collect once no strong ref remains) is kept alive for the trace's lifetime by the span_before_finish subscription closure, which is held by trace_op.events. So state lives exactly as long as the trace and dies with it.

All access is serialized by the owning hook's mutex; the WeakMap itself is not thread-safe under concurrent mutation.

Instance Method Summary collapse

Constructor Details

#initializeSpanEnrichmentStateStore

Returns a new instance of SpanEnrichmentStateStore.



22
23
24
# File 'lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb', line 22

def initialize
  @states = ObjectSpace::WeakMap.new
end

Instance Method Details

#[](trace_op) ⇒ Object



26
27
28
# File 'lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb', line 26

def [](trace_op)
  @states[trace_op]
end

#[]=(trace_op, state) ⇒ Object



30
31
32
# File 'lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb', line 30

def []=(trace_op, state)
  @states[trace_op] = state
end

#clear!Object



42
43
44
# File 'lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb', line 42

def clear!
  @states = ObjectSpace::WeakMap.new
end

#delete(trace_op) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/datadog/open_feature/hooks/span_enrichment_hook/span_enrichment_state_store.rb', line 34

def delete(trace_op)
  # `ObjectSpace::WeakMap#delete` was only added in Ruby 3.3; this gem
  # supports Ruby 2.5+, so overwrite the slot with nil instead so a stale
  # entry is never re-read after the root finishes. The slot itself is
  # reclaimed when the trace operation is collected.
  @states[trace_op] = nil
end