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/propagator.rb,
lib/opentelemetry/context/propagation/noop_injector.rb,
lib/opentelemetry/context/propagation/noop_extractor.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/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

Instance Method Summary collapse

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

.clearObject



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

Parameters:

  • name (String)

    The key name

Returns:



21
22
23
# File 'lib/opentelemetry/context.rb', line 21

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

.currentContext

Returns current context, which is never nil

Returns:



28
29
30
# File 'lib/opentelemetry/context.rb', line 28

def current
  Thread.current[KEY] ||= ROOT
end

.current=(ctx) ⇒ Object

Sets the current context

Parameters:

  • ctx (Context)

    The context to be made active



35
36
37
# File 'lib/opentelemetry/context.rb', line 35

def current=(ctx)
  Thread.current[KEY] = ctx
end

.emptyObject



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

Parameters:

  • key (String)

    The lookup key



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.

Parameters:

  • ctx (Context)

    The context to be made active

Yields:

  • (context)

    Yields context to the block



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

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



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

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



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

#attachObject

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

Parameters:

  • key (Key)

    The key to store this value under

  • value (Object)

    Object to be stored under key

Returns:



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

Parameters:

  • values (Hash)

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

  • value (Object)

    Object to be stored under key

Returns:



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

Parameters:

  • key (Key)

    The lookup key

Returns:

  • (Object)


109
110
111
# File 'lib/opentelemetry/context.rb', line 109

def value(key)
  @entries[key]
end