Class: PostHog::Internal::Context
- Inherits:
-
Object
- Object
- PostHog::Internal::Context
- Defined in:
- lib/posthog/internal/context.rb
Overview
Internal request/fiber-local context applied to capture calls. Uses Rails’ isolated execution state when available, otherwise falls back to thread-local storage in the core SDK.
This is intentionally not exposed as a public SDK API in Ruby yet. It exists to let framework integrations such as posthog-rails propagate request-scoped tracing headers to regular capture and exception events without making the server-side SDK globally stateful per user.
Constant Summary collapse
- STORAGE_KEY =
:posthog_context
Instance Attribute Summary collapse
-
#distinct_id ⇒ Object
readonly
Returns the value of attribute distinct_id.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Class Method Summary collapse
- .current ⇒ Object
- .current=(context) ⇒ Object
- .merge_properties(base, overrides) ⇒ Object
- .with_context(data = nil, fresh: false, **kwargs) ⇒ Object
Instance Method Summary collapse
-
#initialize(distinct_id: nil, session_id: nil, properties: {}) ⇒ Context
constructor
A new instance of Context.
Constructor Details
#initialize(distinct_id: nil, session_id: nil, properties: {}) ⇒ Context
Returns a new instance of Context.
18 19 20 21 22 23 |
# File 'lib/posthog/internal/context.rb', line 18 def initialize(distinct_id: nil, session_id: nil, properties: {}) @distinct_id = distinct_id @session_id = session_id @properties = properties ? properties.dup : {} apply_session_property! end |
Instance Attribute Details
#distinct_id ⇒ Object (readonly)
Returns the value of attribute distinct_id.
16 17 18 |
# File 'lib/posthog/internal/context.rb', line 16 def distinct_id @distinct_id end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
16 17 18 |
# File 'lib/posthog/internal/context.rb', line 16 def properties @properties end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
16 17 18 |
# File 'lib/posthog/internal/context.rb', line 16 def session_id @session_id end |
Class Method Details
.current ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/posthog/internal/context.rb', line 25 def self.current if defined?(ActiveSupport::IsolatedExecutionState) ActiveSupport::IsolatedExecutionState[STORAGE_KEY] else Thread.current[STORAGE_KEY] end end |
.current=(context) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/posthog/internal/context.rb', line 33 def self.current=(context) if defined?(ActiveSupport::IsolatedExecutionState) ActiveSupport::IsolatedExecutionState[STORAGE_KEY] = context else Thread.current[STORAGE_KEY] = context end end |
.merge_properties(base, overrides) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/posthog/internal/context.rb', line 77 def self.merge_properties(base, overrides) merged = (base || {}).dup (overrides || {}).each do |key, value| merged.delete(key.to_s) if key.is_a?(Symbol) merged.delete(key.to_sym) if key.is_a?(String) merged[key] = value end merged end |
.with_context(data = nil, fresh: false, **kwargs) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/posthog/internal/context.rb', line 41 def self.with_context(data = nil, fresh: false, **kwargs) previous_context = current raise ArgumentError, 'with_context requires a block' unless block_given? self.current = resolve(merge_data_and_kwargs(data, kwargs), previous_context, fresh: fresh) yield ensure self.current = previous_context end |