Skip to content

Improvement: Export 'async_capture_span' from the top-level 'elasticapm' package for easier imports #2462

@AlirezaHaghi

Description

@AlirezaHaghi

Describe the bug: "async_capture_span" is not exported from module "elasticapm"
Import from "elasticapm.contrib.asyncio.traces" instead

Description
Currently, trying to import async_capture_span directly from the top-level elasticapm package fails. This creates friction for developers using static analysis tools (like Pylance/Pyright in VS Code), as it generates import errors.

Steps to Reproduce
In a Python file with elastic-apm installed, try to import or use async_capture_span from the top-level package:

--- Pattern 1: Direct import ---
import elasticapm
from elasticapm import async_capture_span
--- Pattern 2: Attribute access ---
@elasticapm.async_capture_span()
async def my_async_task():
    pass
Actual Behavior

Both patterns result in a static analysis error:

"async_capture_span" is not exported from module "elasticapm" Import from "elasticapm.contrib.asyncio.traces" instead

While the code might run (depending on import magic), it is not correctly exposed for static analysis, forcing developers to use # type: ignore comments or scattering deep import paths throughout the codebase.

Expected Behavior

For developer convenience (DX) and consistency with other common functions (like capture_span), async_capture_span should be exposed on the top-level elasticapm module.

This would allow the following to work without any linter/type-checker errors:

from elasticapm import async_capture_span

@async_capture_span()
async def my_async_task():
    pass

Suggested Solution
This issue can likely be resolved by importing async_capture_span into the main elasticapm/init.py file and adding it to the all list.

This would make the asyncio helper more discoverable and easier to use.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions