Skip to content

Span should support async context manager protocol #2007

@notypecheck

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 🤔

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions