-
Notifications
You must be signed in to change notification settings - Fork 557
Integrate async transport with SDK #4615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sl0thentr0py
merged 157 commits into
srothh/transport-class-hierarchy
from
srothh/async-transport-integration
Aug 14, 2025
+891
−158
Merged
Changes from all commits
Commits
Show all changes
157 commits
Select commit
Hold shift + click to select a range
63b1f24
ref(transport): Added shared sync/async transport superclass and crea…
srothh 666ff3a
ref(transport) Removed Todo and reverted class name change
srothh 748764e
test(transport): Add test for HTTP error status handling
srothh ee6dbee
test(transport): Restore accidentally removed comments
srothh 19405fd
ref(transport) Refactor class names to reflect previous functionality
srothh 3736c03
ref(transport): Add flush_async in the Transport abc
srothh 3607d44
ref(transport): Move flush_async from ABC
srothh 0ba5a83
ref(transport): add async type annotations to HTTPTransportCore
srothh 9bb628e
ref(transport): Add abstract base class for worker implementation
srothh a81487e
ref(transport): Add _create_worker factory method to Transport
srothh 8960e6f
ref(worker): Add flush_async method to Worker ABC
srothh 0f7937b
ref(worker): Move worker flush_async from Worker ABC
srothh 268ea1a
ref(worker): Amend function signature for coroutines
srothh b3c05cc
feat(transport): Add an async task-based worker for transport
srothh fb0ad18
ref(worker): Make worker work with new ABC interface
srothh 7edbbaf
fix(worker): Check if callbacks from worker queue are coroutines or f…
srothh 0f63d24
ref(worker): Amend return type of submit and flush to accomodate for …
srothh 2430e2e
ref(worker): Add type parameters for AsyncWorker variables
srothh 96fcd85
ref(worker): Remove loop upon killing worker
srothh 331e40b
feat(worker): Enable concurrent callbacks on async task worker
srothh 5f67485
fix(worker): Modify kill behaviour to mirror threaded worker
srothh 97c5e3d
ref(worker): add proper type annotation to worker task list
srothh 8809b08
feat(transport): Add async transport class
srothh 7c5dec0
ref(transport): Fix event loop handling in async transport
srothh b0390e6
feat(transport): Add kill method for async transport
srothh f01b00d
ref(transport): Fix type errors in async transport
srothh 23b8ea2
Add silent failing to kill on event loop errors
srothh 176a1d1
ref(transport): Fix event loop check in make_transport
srothh 4fe61bf
ref(transport): Add missing transport instantiation in non-async context
srothh 52c9e36
ref(transport): Fix httpcore async specific request handling
srothh 6d69406
ref(transport): Add gc safety to async kill
srothh 3629609
ref(transport): Add missing httpcore extensions
srothh 21cde52
fix(transport): Fix fallback sync transport creating async worker
srothh 9f24136
ref(transport): Add abstract base class for worker implementation
srothh 001f36c
ref(transport): Add _create_worker factory method to Transport
srothh 401b1bc
ref(worker): Add flush_async method to Worker ABC
srothh 3f43d8f
ref(worker): Move worker flush_async from Worker ABC
srothh 15fa295
ref(worker): Amend function signature for coroutines
srothh ef780f3
ref(worker): Add missing docstrings to worker ABC
srothh c541bd7
ref(transport): Make kill optionally return a task for async
srothh 2808062
Integrate AsyncHttpTransport as a new experimental option
srothh ea5f557
ref(transport): Fix type issues in AsyncTransport
srothh c61eb02
ref(transport): Add missing async transport loop type annotation
srothh 38baead
fix(client): Fix mypy type errors
srothh e4ed773
ref(client): Fix mypy inheritance type error
srothh 236ae2c
ref(client): Move mypy annotations to correct place
srothh 9dd546c
ref(client): Add event loop handling to client flush/close
srothh f4ac157
ref(client): Moved close done callback into async task
srothh ed392e9
ref(client): Move timeout check in client to properly cover async
srothh 50553d4
feat(asyncio): Add patching for loop.close in asyncio
srothh 4a7b8ce
ref(asyncio): Fix mypy type annotation errors
srothh cd8a35f
ref(client): Fix redundant async flush helper flushes
srothh b9f2ec7
ref(client): remove wrongful indent
srothh 98d74ed
test(transport): Add inital transport tests
srothh 9df5ec5
fix(requirements): Fix requirements for async transport testing on py…
srothh 23d8740
fix(dependencies): Remove version constraint from httpcore
srothh a496787
fix(dependencies): Version guards for correct httpcore version
srothh 32a9abd
ref(tox): remove anyio version pin
srothh a69f7bb
Revert "ref(tox): remove anyio version pin"
srothh c80b095
ref(dependencies): Revert dependency changes
srothh 5904968
ref(test): Remove flush from async transport test
srothh 09034b7
ref(client): Remove wrongful indents in client sync flush
srothh 21b1cda
fix(testing): Changed httpx anyio version pin to >=3, <5
srothh 9d0cde4
fix(test): Properly modify httpx anyio pin
srothh f21e2ea
fix(test): Add fastapi anyio pin for <0.8
srothh 76aae83
feat(test): Add tests for specific async transport functionality
srothh 4c1e99b
feat(test): Add flush to async transport test
srothh 6df7037
ref(client): Adapt client for blocking kill in async transport
srothh 25c04fc
ref(client): Fix transport shutdown if loop is not running
srothh e8d889c
ref(test): Add config tests for SSL/proxy for async
srothh f63e46f
feat(transport): Add an async task-based worker for transport
srothh 1804271
ref(worker): Make worker work with new ABC interface
srothh 11da869
fix(worker): Check if callbacks from worker queue are coroutines or f…
srothh 779a0d6
ref(worker): Amend return type of submit and flush to accomodate for …
srothh 0895d23
ref(worker): Add type parameters for AsyncWorker variables
srothh bbf426b
ref(worker): Remove loop upon killing worker
srothh 744dc8a
feat(worker): Enable concurrent callbacks on async task worker
srothh fcc8040
fix(worker): Modify kill behaviour to mirror threaded worker
srothh 9a43d9b
ref(worker): add proper type annotation to worker task list
srothh b5eda0e
ref(worker): Refactor implementation to incorporate feedback
srothh 9e380b8
ref(worker): fix queue initialization
srothh ee44621
ref(worker): Add queue as optional to allow for initialisation in start
srothh d9f7383
ref(worker): Change to sync flush method that launches task
srothh a644465
feat(transport): Add async transport class
srothh c935e9e
ref(transport): Fix event loop handling in async transport
srothh b90daf4
feat(transport): Add kill method for async transport
srothh e1d7cdb
ref(transport): Fix type errors in async transport
srothh 90346a5
Add silent failing to kill on event loop errors
srothh 47416f4
ref(transport): Fix event loop check in make_transport
srothh 73cdc6d
ref(transport): Add missing transport instantiation in non-async context
srothh 1ae8708
ref(transport): Fix httpcore async specific request handling
srothh 6f18657
ref(transport): Add gc safety to async kill
srothh 87a9b2f
ref(transport): Add missing httpcore extensions
srothh 4fd7fa0
fix(transport): Fix fallback sync transport creating async worker
srothh 69734cd
ref(transport): Make kill optionally return a task for async
srothh f5ef707
ref(transport): Adapt transport for synchronous flush interface
srothh fca8740
ref(transport): Fix mypy error
srothh 4b0d09b
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh e23efd7
ref(transport): Make client work with sync flush changes
srothh d2e647b
ref(worker): Readd coroutine check for worker callbacks
srothh 55b606a
ref(client): Properly add client changes for sync flush
srothh d89abed
feat(transport): Add async transport class
srothh 5e1e0c6
ref(transport): Fix event loop handling in async transport
srothh 8fdf43d
feat(transport): Add kill method for async transport
srothh 6619670
ref(transport): Fix type errors in async transport
srothh 2eee1b1
Add silent failing to kill on event loop errors
srothh 6c787a4
ref(transport): Fix event loop check in make_transport
srothh b79d346
ref(transport): Add missing transport instantiation in non-async context
srothh 1717888
ref(transport): Fix httpcore async specific request handling
srothh e1fd57a
ref(transport): Add gc safety to async kill
srothh b87c68e
ref(transport): Add missing httpcore extensions
srothh a827d0d
fix(transport): Fix fallback sync transport creating async worker
srothh ee0b440
ref(transport): Make kill optionally return a task for async
srothh 70f228e
ref(transport): Adapt transport for synchronous flush interface
srothh 328d8ad
ref(transport): Fix mypy error
srothh ef61134
ref(transport): Make transport loop public
srothh ad93516
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh 42d3a34
ref(transport): Add import checking for async transport
srothh 10d85f6
ref(transport): Fix typing errors
srothh aaae195
ref(transport): Fix import checking
srothh de47da2
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh 6e2c4f6
ref(client): Fix type checking with fallback asynctransport
srothh 9da7be8
ref(asyncio): Refactor loop close patch in asyncio integration
srothh 8a5ab06
ref(client): Split client flush into seperate function for readability
srothh 295a0e9
ref(transport): Refactor async transport to be more aligned with sync
srothh e754a85
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh 859a0e2
ref(worker): Remove sync callbacks from worker processing for now
srothh 4a58ce7
feat(transport): Add async transport class
srothh cbecde7
ref(transport): Fix event loop handling in async transport
srothh c8bb55a
feat(transport): Add kill method for async transport
srothh 05a7de7
ref(transport): Fix type errors in async transport
srothh 38246d0
Add silent failing to kill on event loop errors
srothh 8b226cb
ref(transport): Fix event loop check in make_transport
srothh 823215e
ref(transport): Add missing transport instantiation in non-async context
srothh 4eed4fd
ref(transport): Fix httpcore async specific request handling
srothh afd494d
ref(transport): Add gc safety to async kill
srothh fcc7ac3
ref(transport): Add missing httpcore extensions
srothh f659514
fix(transport): Fix fallback sync transport creating async worker
srothh 8c542ce
ref(transport): Make kill optionally return a task for async
srothh 30dde67
ref(transport): Adapt transport for synchronous flush interface
srothh 3392e0e
ref(transport): Fix mypy error
srothh 6c85500
ref(transport): Make transport loop public
srothh ae5a864
ref(transport): Add import checking for async transport
srothh 9c537e6
ref(transport): Fix typing errors
srothh f7554b2
ref(transport): Fix import checking
srothh 6cb72ad
ref(transport): Refactor async transport to be more aligned with sync
srothh 9f226cf
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh 9171c5d
Merge remote-tracking branch 'origin/srothh/transport-class-hierarchy…
sl0thentr0py 111861b
ref(transport): Improve transport code quality
srothh 1334a29
Merge branch 'srothh/async-transport' into srothh/async-transport-int…
srothh a2aafb4
Merge remote-tracking branch 'origin/srothh/transport-class-hierarchy…
sl0thentr0py 6dd8138
test(transport): Remove background tasks from transport tests
srothh 5f7af94
fix(transport): Add asyncio integration check to async transport
srothh f44f690
fix(transport): Fix asyncio integration check for transport
srothh 1db746a
ref(async transport): Separate flush/close and async flush/close
srothh 54b7fb6
ref(client): Fix mypy errors
srothh e36e9fa
ref(client): Remove unused function
srothh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from sentry_sdk.consts import OP | ||
from sentry_sdk.integrations import Integration, DidNotEnable | ||
from sentry_sdk.utils import event_from_exception, logger, reraise | ||
from sentry_sdk.transport import AsyncHttpTransport | ||
|
||
try: | ||
import asyncio | ||
|
@@ -29,6 +30,45 @@ def get_name(coro: Any) -> str: | |
) | ||
|
||
|
||
def patch_loop_close() -> None: | ||
"""Patch loop.close to flush pending events before shutdown.""" | ||
# Atexit shutdown hook happens after the event loop is closed. | ||
# Therefore, it is necessary to patch the loop.close method to ensure | ||
# that pending events are flushed before the interpreter shuts down. | ||
try: | ||
loop = asyncio.get_running_loop() | ||
except RuntimeError: | ||
# No running loop → cannot patch now | ||
return | ||
|
||
if getattr(loop, "_sentry_flush_patched", False): | ||
return | ||
|
||
async def _flush() -> None: | ||
client = sentry_sdk.get_client() | ||
if not client: | ||
return | ||
|
||
try: | ||
if not isinstance(client.transport, AsyncHttpTransport): | ||
return | ||
|
||
await client.close_async() | ||
except Exception: | ||
logger.warning("Sentry flush failed during loop shutdown", exc_info=True) | ||
|
||
orig_close = loop.close | ||
|
||
def _patched_close() -> None: | ||
try: | ||
loop.run_until_complete(_flush()) | ||
finally: | ||
orig_close() | ||
|
||
loop.close = _patched_close # type: ignore | ||
loop._sentry_flush_patched = True # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Async Loop Patching Fails on InitializationThe
|
||
|
||
|
||
def patch_asyncio() -> None: | ||
orig_task_factory = None | ||
try: | ||
|
@@ -124,3 +164,4 @@ class AsyncioIntegration(Integration): | |
@staticmethod | ||
def setup_once() -> None: | ||
patch_asyncio() | ||
patch_loop_close() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.