fix(cohere): avoid double-ending async error spans#4375
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe async Cohere wrapper no longer explicitly ends spans in its exception path. A new test verifies exception recording, error status, single span completion, and absence of duplicate-end warnings during cleanup. ChangesCohere async error handling
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/opentelemetry-instrumentation-cohere/tests/test_async_error_handling.py`:
- Around line 29-35: Extend the async error-handling test assertions for the
finished span returned by span_exporter.get_finished_spans() to verify it
contains an exception event with the recorded failure message "cohere async
failure". Keep the existing status, description, and caplog assertions
unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 5ef2b7b8-d193-4da8-944e-75389fbad6b5
📒 Files selected for processing (2)
packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/__init__.pypackages/opentelemetry-instrumentation-cohere/tests/test_async_error_handling.py
💤 Files with no reviewable changes (1)
- packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/init.py
Extend the async error-path regression test to assert the recorded exception event message, matching CodeRabbit review feedback.
gnanirahulnutakki
left a comment
There was a problem hiding this comment.
I reproduced one telemetry-correctness issue on the submitted head. The inline comment includes the failing exact-cardinality assertion and a locally verified minimal fix; otherwise the change is focused and the stated async tests pass.
| @@ -287,7 +287,6 @@ async def _awrap( | |||
| if span.is_recording(): | |||
| span.set_status(Status(StatusCode.ERROR, str(e))) | |||
| span.record_exception(e) | |||
There was a problem hiding this comment.
The outer start_as_current_span(...) context manager already records any exception that escapes this block (its default is record_exception=True), so this explicit call still creates duplicate exception telemetry even though the duplicate end() is gone. I reproduced this on 9c303fd: changing the new test to assert len(exception_events) == 1 fails with 2 == 1.
Please give one layer sole ownership of exception recording—e.g. remove this manual try/except and 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 plus test_cohere_v2_rerank_legacy_async and test_cohere_chat_legacy_async all pass locally.
Summary
Fixes Cohere async instrumentation error handling so spans are not ended twice when an async Cohere call raises.
_awrap()usestracer.start_as_current_span(...), which already ends the span when the context manager exits. The exception handler also calledspan.end(), causing OpenTelemetry to emit:This removes the redundant manual end call while preserving ERROR status, exception recording, and re-raising behavior.
Related
Related to #2338, which tracks Cohere async instrumentation behavior.
Test Plan
uv run pytest tests/test_async_error_handling.py tests/test_rerank.py::test_cohere_v2_rerank_legacy_async tests/test_chat.py::test_cohere_chat_legacy_async -quv run pytest tests/ -quv run ruff check .Summary by CodeRabbit
ERRORstatus, and spans now finish cleanly without duplicate-end warnings.exceptionevent.