fix(replicate): record errors on spans#4023
Conversation
Wrap replicate instrumentation calls in try/except to properly record exceptions on spans. On error, sets error.type attribute, records the exception event, and sets span status to ERROR before re-raising. Adds two tests verifying error handling for both run and stream paths. Closes traceloop#412
📝 WalkthroughWalkthroughThe changes add comprehensive error telemetry to the Replicate instrumentation by recording exceptions, setting error status codes on spans, and ensuring spans are properly closed even when errors occur during execution or streaming operations. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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.
🧹 Nitpick comments (1)
packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/__init__.py (1)
63-79: Looks correct; consider extracting the error-recording block.The
try/except/finallycorrectly handles normal completion, iterator errors, andGeneratorExit/GC (span is always ended). The same four-line error-recording sequence is duplicated in_wrap(lines 154-157). Consider extracting a small helper to keep the two sites in sync as instrumentation evolves.♻️ Suggested helper
+def _record_exception_on_span(span, e): + span.set_attribute(ERROR_TYPE, e.__class__.__name__) + span.record_exception(e) + span.set_status(Status(StatusCode.ERROR, str(e))) + + def _build_from_streaming_response(span, event_logger, response): complete_response = "" try: for item in response: item_to_yield = item complete_response += str(item) yield item_to_yield _handle_response(span, event_logger, complete_response) except Exception as e: - span.set_attribute(ERROR_TYPE, e.__class__.__name__) - span.record_exception(e) - span.set_status(Status(StatusCode.ERROR, str(e))) + _record_exception_on_span(span, e) raise finally: span.end()🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/__init__.py` around lines 63 - 79, Extract the duplicated four-line error recording logic into a helper (e.g., _record_span_error) and call it from both _build_from_streaming_response and _wrap: implement _record_span_error(span, exc) to call span.set_attribute(ERROR_TYPE, exc.__class__.__name__), span.record_exception(exc), and span.set_status(Status(StatusCode.ERROR, str(exc))); then replace the duplicated lines in _build_from_streaming_response and the identical block in _wrap with a call to _record_span_error(span, e).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/__init__.py`:
- Around line 63-79: Extract the duplicated four-line error recording logic into
a helper (e.g., _record_span_error) and call it from both
_build_from_streaming_response and _wrap: implement _record_span_error(span,
exc) to call span.set_attribute(ERROR_TYPE, exc.__class__.__name__),
span.record_exception(exc), and span.set_status(Status(StatusCode.ERROR,
str(exc))); then replace the duplicated lines in _build_from_streaming_response
and the identical block in _wrap with a call to _record_span_error(span, e).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b9fb01d4-3a2c-4c20-b3a1-5d613a57dcd7
⛔ Files ignored due to path filters (1)
packages/opentelemetry-instrumentation-replicate/uv.lockis excluded by!**/*.lock
📒 Files selected for processing (2)
packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/__init__.pypackages/opentelemetry-instrumentation-replicate/tests/test_llama.py
Wrap replicate instrumentation calls in try/except to properly record exceptions on spans. On error, sets error.type attribute, records the exception event, and sets span status to ERROR before re-raising.
Adds two tests verifying error handling for both run and stream paths.
Closes #412
feat(instrumentation): ...orfix(instrumentation): ....Summary by CodeRabbit
Release Notes
New Features
Tests