Class: OpenTelemetry::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • context (Context)

    The new context

Returns:

  • (Object)

    A token to be used when detaching



42
43
44
45
46
# File 'lib/opentelemetry/context.rb', line 42

def attach(context)
  s = stack
  s.push(context)
  s.size
end

.clearObject

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

Parameters:

  • name (String)

    The key name

Returns:



24
25
26
# File 'lib/opentelemetry/context.rb', line 24

def create_key(name)
  Key.new(name)
end

.currentContext

Returns current context, which is never nil

Returns:



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.

Parameters:

  • token (Object)

    The token provided by the matching call to attach

Returns:

  • (Boolean)

    True if the calls matched, false otherwise



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

.emptyObject



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

Parameters:

  • key (String)

    The lookup key



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.

Parameters:

  • ctx (Context)

    The context to be made active

Yields:

  • (context)

    Yields context to the block



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

Parameters:

  • key (String)

    The lookup key

  • value (Object)

    The object stored under key

  • Block (Callable)

    to execute in a new context

Yields:

  • (context, value)

    Yields the newly created context and value to the block



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

Parameters:

  • key (String)

    The lookup key

  • values (Hash)

    Will be merged with values of the current context and returned in a new context

  • Block (Callable)

    to execute in a new context

Yields:

  • (context, values)

    Yields the newly created context and values to the block



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

Parameters:

  • key (Key)

    The key to store this value under

  • value (Object)

    Object to be stored under key

Returns:



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

Parameters:

  • values (Hash)

    The values to be merged with the current context's entries.

  • value (Object)

    Object to be stored under key

Returns:



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

Parameters:

  • key (Key)

    The lookup key

Returns:

  • (Object)


142
143
144
# File 'lib/opentelemetry/context.rb', line 142

def value(key)
  @entries[key]
end