Skip to content

Span should support async context manager protocol #2007

Description

@notypecheck

Problem Statement

Span and Transaction should support async context manager protocol to avoid unnecessary nesting.

As an example:

async def main() -> None:
    async with a:
        with sentry_sdk.start_transaction(
            op="op",
            name="name",
        ):
            with sentry_sdk.start_span(op="b"):
                async with b:
                    await b.run()

            with sentry_sdk.start_span(op="c"):
                async with c:
                    await c.run()

Could be shortened to

async def main() -> None:
    async with a, sentry_sdk.start_transaction(
        op="op",
        name="name",
    ):
        async with sentry_sdk.start_span(op="b"), b:
            await b.run()

        async with sentry_sdk.start_span(op="c"), c:
            await c.run()

Solution Brainstorm

I think currently __aenter__ and __aexit__ could do the same as their synchronous counterparts and later could be replaced with async code, if that would be possible at some point 🤔

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions