Class: OpenTelemetry::Context
- Inherits:
-
Object
- Object
- OpenTelemetry::Context
- Defined in:
- lib/opentelemetry/context.rb,
lib/opentelemetry/context/key.rb,
lib/opentelemetry/context/propagation.rb,
lib/opentelemetry/context/propagation/propagator.rb,
lib/opentelemetry/context/propagation/propagation.rb,
lib/opentelemetry/context/propagation/noop_injector.rb,
lib/opentelemetry/context/propagation/default_getter.rb,
lib/opentelemetry/context/propagation/default_setter.rb,
lib/opentelemetry/context/propagation/noop_extractor.rb,
lib/opentelemetry/context/propagation/composite_propagator.rb
Overview
Manages context on a per-fiber basis
Defined Under Namespace
Modules: Propagation Classes: Key
Constant Summary collapse
- KEY =
:__opentelemetry_context__
- EMPTY_ENTRIES =
{}.freeze
- ROOT =
empty.freeze
Class Method Summary collapse
- .clear ⇒ Object
-
.create_key(name) ⇒ Context::Key
Returns a key used to index a value in a Context.
-
.current ⇒ Context
Returns current context, which is never nil.
-
.current=(ctx) ⇒ Object
Sets the current context.
- .empty ⇒ Object
-
.value(key) ⇒ Object
Returns the value associated with key in the current context.
-
.with_current(ctx) {|context| ... } ⇒ Object
Executes a block with ctx as the current context.
- .with_value(key, value) {|context, value| ... } ⇒ Object
- .with_values(values) {|context, values| ... } ⇒ Object
Instance Method Summary collapse
- #attach ⇒ Object private
- #detach(ctx_to_attach = nil) ⇒ Object private
-
#initialize(parent, entries) ⇒ Context
constructor
A new instance of Context.
-
#set_value(key, value) ⇒ Context
Returns a new Context where entries contains the newly added key and value.
-
#set_values(values) ⇒ Context
Returns a new Context with the current context's entries merged with the new entries.
-
#value(key) ⇒ Object
(also: #[])
Returns the corresponding value (or nil) for key.
Constructor Details
#initialize(parent, entries) ⇒ Context
Returns a new instance of Context.
100 101 102 103 |
# File 'lib/opentelemetry/context.rb', line 100 def initialize(parent, entries) @parent = parent @entries = entries.freeze end |
Class Method Details
.clear ⇒ Object
91 92 93 |
# File 'lib/opentelemetry/context.rb', line 91 def clear self.current = ROOT end |
.create_key(name) ⇒ Context::Key
Returns a key used to index a value in a Context
21 22 23 |
# File 'lib/opentelemetry/context.rb', line 21 def create_key(name) Key.new(name) end |
.current ⇒ Context
Returns current context, which is never nil
28 29 30 |
# File 'lib/opentelemetry/context.rb', line 28 def current Thread.current[KEY] ||= ROOT end |
.current=(ctx) ⇒ Object
Sets the current context
35 36 37 |
# File 'lib/opentelemetry/context.rb', line 35 def current=(ctx) Thread.current[KEY] = ctx end |
.empty ⇒ Object
95 96 97 |
# File 'lib/opentelemetry/context.rb', line 95 def empty new(nil, EMPTY_ENTRIES) end |
.value(key) ⇒ Object
Returns the value associated with key in the current context
87 88 89 |
# File 'lib/opentelemetry/context.rb', line 87 def value(key) current.value(key) end |
.with_current(ctx) {|context| ... } ⇒ Object
Executes a block with ctx as the current context. It restores the previous context upon exiting.
44 45 46 47 48 49 |
# File 'lib/opentelemetry/context.rb', line 44 def with_current(ctx) prev = ctx.attach yield ctx ensure ctx.detach(prev) end |
.with_value(key, value) {|context, value| ... } ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/opentelemetry/context.rb', line 59 def with_value(key, value) ctx = current.set_value(key, value) prev = ctx.attach yield ctx, value ensure ctx.detach(prev) end |
.with_values(values) {|context, values| ... } ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/opentelemetry/context.rb', line 76 def with_values(values) ctx = current.set_values(values) prev = ctx.attach yield ctx, values ensure ctx.detach(prev) end |
Instance Method Details
#attach ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
138 139 140 141 142 |
# File 'lib/opentelemetry/context.rb', line 138 def attach prev = self.class.current self.class.current = self prev end |
#detach(ctx_to_attach = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
145 146 147 148 149 150 |
# File 'lib/opentelemetry/context.rb', line 145 def detach(ctx_to_attach = nil) OpenTelemetry.logger.warn 'Calls to detach should match corresponding calls to attach' if self.class.current != self ctx_to_attach ||= @parent || ROOT ctx_to_attach.attach end |
#set_value(key, value) ⇒ Context
Returns a new Context where entries contains the newly added key and value
120 121 122 123 124 |
# File 'lib/opentelemetry/context.rb', line 120 def set_value(key, value) new_entries = @entries.dup new_entries[key] = value Context.new(self, new_entries) end |
#set_values(values) ⇒ Context
Returns a new Context with the current context's entries merged with the new entries
133 134 135 |
# File 'lib/opentelemetry/context.rb', line 133 def set_values(values) # rubocop:disable Naming/AccessorMethodName: Context.new(self, @entries.merge(values)) end |
#value(key) ⇒ Object Also known as: []
Returns the corresponding value (or nil) for key
109 110 111 |
# File 'lib/opentelemetry/context.rb', line 109 def value(key) @entries[key] end |