opentelemetry.context package

Module contents

class opentelemetry.context.Context[source]

Bases: dict[str, object]

setdefault(key, default=None)[source]

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

Return type:

object

pop(key, *args)[source]

If the key is not found, return the default if given; otherwise, raise a KeyError.

Return type:

object

popitem()[source]

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

Return type:

tuple[str, object]

clear()[source]
Return type:

None

update(*args, **kwargs)[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

Return type:

None

opentelemetry.context.attach(context)[source]

Associates a Context with the caller’s current execution unit. Returns a token that can be used to restore the previous Context.

Parameters:

context (Context) – The Context to set as current.

Return type:

Token[Context]

Returns:

A token that can be used with detach to reset the context.

opentelemetry.context.create_key(keyname)[source]

To allow cross-cutting concern to control access to their local state, the RuntimeContext API provides a function which takes a keyname as input, and returns a unique key. :type keyname: str :param keyname: The key name is for debugging purposes and is not required to be unique.

Return type:

str

Returns:

A unique string representing the newly created key.

opentelemetry.context.detach(token)[source]

Resets the Context associated with the caller’s current execution unit to the value it had before attaching a specified Context.

Parameters:

token (Token[Context]) – The Token that was returned by a previous call to attach a Context.

Return type:

None

opentelemetry.context.get_current()[source]

To access the context associated with program execution, the Context API provides a function which takes no arguments and returns a Context.

Return type:

Context

Returns:

The current Context object.

opentelemetry.context.get_value(key, context=None)[source]

To access the local state of a concern, the RuntimeContext API provides a function which takes a context and a key as input, and returns a value.

Parameters:
  • key (str) – The key of the value to retrieve.

  • context (Optional[Context]) – The context from which to retrieve the value, if None, the current context is used.

Return type:

object

Returns:

The value associated with the key.

opentelemetry.context.set_value(key, value, context=None)[source]

To record the local state of a cross-cutting concern, the RuntimeContext API provides a function which takes a context, a key, and a value as input, and returns an updated context which contains the new value.

Parameters:
  • key (str) – The key of the entry to set.

  • value (object) – The value of the entry to set.

  • context (Optional[Context]) – The context to copy, if None, the current context is used.

Return type:

Context

Returns:

A new Context containing the value set.