# opentelemetry.context package

## Module contents

class opentelemetry.context.Context

  Bases: `dict`[`str`, `object`]

opentelemetry.context.attach(context)

  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)

  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)

  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()

  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)

  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 (`Context` | `None`) – 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)

  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 (`Context` | `None`) – The context to copy, if None, the current context is used.

  Return type:

    `Context`

  Returns:

    A new `Context` containing the value set.