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 =

rubocop:disable Style/EmptyClassDefinition

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.



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

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



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

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

.clearObject

Clears the fiber-local Context stack.



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

def clear
  Fiber.current.opentelemetry_context = []
end

.create_key(name) ⇒ Context::Key

Returns a key used to index a value in a Context

Parameters:

  • name (String)

    The key name

Returns:



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

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

.currentContext

Returns current context, which is never nil

Returns:



32
33
34
# File 'lib/opentelemetry/context.rb', line 32

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



56
57
58
59
60
61
62
63
# File 'lib/opentelemetry/context.rb', line 56

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

.emptyContext

Returns an empty context.

Returns:



127
128
129
# File 'lib/opentelemetry/context.rb', line 127

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



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

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

Yield Parameters:

  • context (Context)

    The active context



71
72
73
74
75
76
# File 'lib/opentelemetry/context.rb', line 71

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

Yields:

  • (context, value)

    Yields the newly created context and value to the block

Yield Parameters:

  • context (Context)

    The newly created context

  • value (Object)

    The object stored under key



87
88
89
90
91
92
93
# File 'lib/opentelemetry/context.rb', line 87

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:

  • values (Hash)

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

Yields:

  • (context, values)

    Yields the newly created context and values to the block

Yield Parameters:

  • context (Context)

    The newly created context

  • values (Hash)

    The values merged into the new context



104
105
106
107
108
109
110
# File 'lib/opentelemetry/context.rb', line 104

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:



157
158
159
160
161
# File 'lib/opentelemetry/context.rb', line 157

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.

Returns:



169
170
171
# File 'lib/opentelemetry/context.rb', line 169

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)


146
147
148
# File 'lib/opentelemetry/context.rb', line 146

def value(key)
  @entries[key]
end