-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(vertexai): make dont_throw work on async functions #4415
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
Open
chiruu12
wants to merge
3
commits into
traceloop:main
Choose a base branch
from
chiruu12:fix/vertexai-dont-throw-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−9
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
packages/opentelemetry-instrumentation-vertexai/tests/test_dont_throw.py
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
| from opentelemetry.instrumentation.vertexai.utils import dont_throw | ||
|
|
||
|
|
||
| def test_dont_throw_swallows_sync_exceptions(): | ||
| @dont_throw | ||
| def boom(): | ||
| raise RuntimeError("instrumentation failed") | ||
|
|
||
| assert boom() is None, "sync instrumentation errors must not reach the caller" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dont_throw_swallows_async_exceptions(): | ||
| """An async function returns a coroutine immediately, so a sync-only | ||
| wrapper exits its try block before the body runs and the caller awaits | ||
| outside the guard. span_utils.set_input_attributes and _handle_request are | ||
| both async and both decorated, and _handle_request is awaited unguarded.""" | ||
|
|
||
| @dont_throw | ||
| async def boom(): | ||
| raise RuntimeError("instrumentation failed") | ||
|
|
||
| assert await boom() is None, "async instrumentation errors must not reach the caller" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dont_throw_returns_async_values(): | ||
| @dont_throw | ||
| async def fine(): | ||
| return "ok" | ||
|
|
||
| assert await fine() == "ok", "decorator must not swallow the return value" | ||
|
|
||
|
|
||
| def test_dont_throw_preserves_sync_return(): | ||
| @dont_throw | ||
| def fine(): | ||
| return "ok" | ||
|
|
||
| assert fine() == "ok" | ||
|
|
||
|
|
||
| def test_dont_throw_picks_wrapper_by_function_kind(): | ||
| @dont_throw | ||
| async def coro(): | ||
| return None | ||
|
|
||
| @dont_throw | ||
| def plain(): | ||
| return None | ||
|
|
||
| assert asyncio.iscoroutinefunction(coro), "async functions need the async wrapper" | ||
| assert not asyncio.iscoroutinefunction(plain) | ||
|
|
||
|
|
||
| def test_dont_throw_survives_a_failing_exception_logger(): | ||
| """A user-supplied exception_logger that itself raises must not reach the caller.""" | ||
| from opentelemetry.instrumentation.vertexai.config import Config | ||
|
|
||
| def angry_logger(_e): | ||
| raise ValueError("exception logger is broken") | ||
|
|
||
| previous = Config.exception_logger | ||
| Config.exception_logger = angry_logger | ||
| try: | ||
| @dont_throw | ||
| def boom(): | ||
| raise RuntimeError("instrumentation failed") | ||
|
|
||
| assert boom() is None, "a failing exception_logger must not surface to the caller" | ||
| finally: | ||
| Config.exception_logger = previous | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dont_throw_async_survives_a_failing_exception_logger(): | ||
| from opentelemetry.instrumentation.vertexai.config import Config | ||
|
|
||
| def angry_logger(_e): | ||
| raise ValueError("exception logger is broken") | ||
|
|
||
| previous = Config.exception_logger | ||
| Config.exception_logger = angry_logger | ||
| try: | ||
| @dont_throw | ||
| async def boom(): | ||
| raise RuntimeError("instrumentation failed") | ||
|
|
||
| assert await boom() is None | ||
| finally: | ||
| Config.exception_logger = previous | ||
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.