Skip to content

Use parent context

Temporarily activate the OpenTelemetry parent context of an event.

The previously active context is restored when the scope exits.

Source code in event_sourcery_otel/_context.py
@contextmanager
def use_parent_context(event_context: WrappedEvent | Context) -> Iterator[None]:
    """Temporarily activate the OpenTelemetry parent context of an event.

    The previously active context is restored when the scope exits.
    """
    token = context.attach(get_parent_context(event_context))
    try:
        yield
    finally:
        context.detach(token)