-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(cohere): avoid double-ending async error spans #4375
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
euisuh
wants to merge
2
commits into
traceloop:main
Choose a base branch
from
euisuh:fix/cohere-async-error-double-end
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.
+42
−1
Open
Changes from all commits
Commits
Show all changes
2 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
42 changes: 42 additions & 0 deletions
42
packages/opentelemetry-instrumentation-cohere/tests/test_async_error_handling.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,42 @@ | ||
| import logging | ||
|
|
||
| import cohere | ||
| import pytest | ||
| from opentelemetry.instrumentation.cohere import CohereInstrumentor | ||
| from opentelemetry.trace import StatusCode | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_cohere_exception_records_error_once( | ||
| caplog, monkeypatch, span_exporter, tracer_provider | ||
| ): | ||
| async def failing_chat(self, *args, **kwargs): | ||
| raise RuntimeError("cohere async failure") | ||
|
|
||
| monkeypatch.setattr(cohere.AsyncClient, "chat", failing_chat) | ||
| instrumentor = CohereInstrumentor() | ||
| instrumentor.instrument(tracer_provider=tracer_provider) | ||
|
|
||
| try: | ||
| with caplog.at_level(logging.WARNING, logger="opentelemetry.sdk.trace"): | ||
| with pytest.raises(RuntimeError, match="cohere async failure"): | ||
| await cohere.AsyncClient("test_api_key").chat( | ||
| model="command", message="Tell me a joke, pirate style" | ||
| ) | ||
| finally: | ||
| instrumentor.uninstrument() | ||
|
|
||
| spans = span_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span.name == "cohere.chat" | ||
| assert span.status.status_code == StatusCode.ERROR | ||
| assert "cohere async failure" in span.status.description | ||
| assert any( | ||
| event.name == "exception" | ||
| and "cohere async failure" in event.attributes.get("exception.message", "") | ||
| for event in span.events | ||
| ) | ||
| assert not any( | ||
| "Calling end() on an ended span" in record.message for record in caplog.records | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outer
start_as_current_span(...)context manager already records any exception that escapes this block (its default isrecord_exception=True), so this explicit call still creates duplicate exception telemetry even though the duplicateend()is gone. I reproduced this on9c303fd: changing the new test toassert len(exception_events) == 1fails with2 == 1.Please give one layer sole ownership of exception recording—e.g. remove this manual
try/exceptand let the context manager set ERROR/record the exception, or disable its automatic exception handling—and make the regression assert exact cardinality. With the manual handler removed, the strengthened test plustest_cohere_v2_rerank_legacy_asyncandtest_cohere_chat_legacy_asyncall pass locally.