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/rack_env_getter.rb,
lib/opentelemetry/context/propagation/text_map_getter.rb,
lib/opentelemetry/context/propagation/text_map_setter.rb,
lib/opentelemetry/context/propagation/text_map_propagator.rb,
lib/opentelemetry/context/propagation/noop_text_map_propagator.rb,
lib/opentelemetry/context/propagation/composite_text_map_propagator.rb
Overview
Manages context on a per-fiber basis
Defined Under Namespace
Modules: Propagation Classes: Key
Constant Summary collapse
- DetachError =
Class.new(OpenTelemetry::Error)
- ROOT =
empty.freeze
Class Method Summary collapse
-
.attach(context) ⇒ Object
Associates a Context with the caller's current Fiber.
-
.clear ⇒ Object
Clears the fiber-local Context stack.
-
.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.
-
.detach(token) ⇒ Boolean
Restores the previous Context associated with the current Fiber.
- .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
-
#initialize(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(entries) ⇒ Context
Returns a new instance of Context.
134 135 136 |
# File 'lib/opentelemetry/context.rb', line 134 def initialize(entries) @entries = entries.freeze end |
Class Method Details
.attach(context) ⇒ Object
Associates a Context with the caller's current Fiber. Every call to this operation should be paired with a corresponding call to detach.
Returns a token to be used with the matching call to detach
42 43 44 45 46 |
# File 'lib/opentelemetry/context.rb', line 42 def attach(context) s = stack s.push(context) s.size end |
.clear ⇒ Object
Clears the fiber-local Context stack. This allocates a new array for the stack, which is important in some use-cases to avoid sharing the backing array between fibers.
119 120 121 |
# File 'lib/opentelemetry/context.rb', line 119 def clear Thread.current[STACK_KEY] = [] end |
.create_key(name) ⇒ Context::Key
Returns a key used to index a value in a Context
24 25 26 |
# File 'lib/opentelemetry/context.rb', line 24 def create_key(name) Key.new(name) end |
.current ⇒ Context
Returns current context, which is never nil
31 32 33 |
# File 'lib/opentelemetry/context.rb', line 31 def current stack.last || ROOT end |
.detach(token) ⇒ Boolean
Restores the previous Context associated with the current Fiber. The supplied token is used to check if the call to detach is balanced with a corresponding attach call. A warning is logged if the calls are unbalanced.
55 56 57 58 59 60 61 62 |
# File 'lib/opentelemetry/context.rb', line 55 def detach(token) s = stack calls_matched = (token == s.size) OpenTelemetry.handle_error(exception: DetachError.new('calls to detach should match corresponding calls to attach.')) unless calls_matched s.pop calls_matched end |
.empty ⇒ Object
123 124 125 |
# File 'lib/opentelemetry/context.rb', line 123 def empty new(EMPTY_ENTRIES) end |
.value(key) ⇒ Object
Returns the value associated with key in the current context
112 113 114 |
# File 'lib/opentelemetry/context.rb', line 112 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.
69 70 71 72 73 74 |
# File 'lib/opentelemetry/context.rb', line 69 def with_current(ctx) token = attach(ctx) yield ctx ensure detach(token) end |
.with_value(key, value) {|context, value| ... } ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/opentelemetry/context.rb', line 84 def with_value(key, value) ctx = current.set_value(key, value) token = attach(ctx) yield ctx, value ensure detach(token) end |
.with_values(values) {|context, values| ... } ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/opentelemetry/context.rb', line 101 def with_values(values) ctx = current.set_values(values) token = attach(ctx) yield ctx, values ensure detach(token) end |
Instance Method Details
#set_value(key, value) ⇒ Context
Returns a new Context where entries contains the newly added key and value
153 154 155 156 157 |
# File 'lib/opentelemetry/context.rb', line 153 def set_value(key, value) new_entries = @entries.dup new_entries[key] = value Context.new(new_entries) end |
#set_values(values) ⇒ Context
Returns a new Context with the current context's entries merged with the new entries
166 167 168 |
# File 'lib/opentelemetry/context.rb', line 166 def set_values(values) # rubocop:disable Naming/AccessorMethodName: Context.new(@entries.merge(values)) end |
#value(key) ⇒ Object Also known as: []
Returns the corresponding value (or nil) for key
142 143 144 |
# File 'lib/opentelemetry/context.rb', line 142 def value(key) @entries[key] end |