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.



131
132
133
# File 'lib/opentelemetry/context.rb', line 131

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



116
117
118
# File 'lib/opentelemetry/context.rb', line 116

def clear
  stack.clear
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



120
121
122
# File 'lib/opentelemetry/context.rb', line 120

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:



150
151
152
153
154
# File 'lib/opentelemetry/context.rb', line 150

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:



163
164
165
# File 'lib/opentelemetry/context.rb', line 163

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)


139
140
141
# File 'lib/opentelemetry/context.rb', line 139

def value(key)
  @entries[key]
end