Skip to content

Conversation

@kxbnb
Copy link

@kxbnb kxbnb commented Jan 20, 2026

Summary

Adds error logging to all four instrumented Bedrock functions so exceptions are properly recorded in spans.

Changes:

  • _instrumented_model_invoke - wrap call in try/except
  • _instrumented_model_invoke_with_response_stream - wrap call in try/except
  • _instrumented_converse - wrap call in try/except
  • _instrumented_converse_stream - wrap call in try/except

When an error occurs, each function now:

  • Sets error.type attribute with the exception class name
  • Records exception via span.record_exception(e)
  • Sets span status to ERROR
  • Increments the exception counter metric

Why

Per the OTel spec for recording exceptions in spans, errors should be captured so they're visible in traces.

Testing

  • All 75 existing tests pass
  • Lint passes

Partial fix for #412


Important

Add error logging to instrumented Bedrock functions to capture exceptions in spans.

  • Error Logging:
    • Add try/except blocks to _instrumented_model_invoke, _instrumented_model_invoke_with_response_stream, _instrumented_converse, _instrumented_converse_stream.
    • On exception, set error.type attribute, record exception, set span status to ERROR, increment exception counter.
  • Testing:
    • All 75 existing tests pass.
    • Lint passes.

This description was created by Ellipsis for 9685686. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

Release Notes

  • New Features
    • Improved error tracking and observability for Bedrock operations with automatic exception handling, error metrics collection, and enhanced span annotations across all API call types.

✏️ Tip: You can customize this high-level summary in your review settings.

Adds try/except blocks to record exceptions in spans for all four
instrumented functions:
- _instrumented_model_invoke
- _instrumented_model_invoke_with_response_stream
- _instrumented_converse
- _instrumented_converse_stream

When an exception occurs, the span now:
- Sets ERROR_TYPE attribute with the exception class name
- Records the exception details via span.record_exception()
- Sets span status to ERROR with the exception message
- Increments the exception counter metric

This follows the OTel error recording pattern per:
https://opentelemetry.io/docs/languages/python/instrumentation/#record-exceptions-in-spans

Partial fix for traceloop#412
@CLAassistant
Copy link

CLAassistant commented Jan 20, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link

coderabbitai bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

This change adds comprehensive error handling and metrics recording to Bedrock instrumentation wrappers. For each API call path (invoke, invoke stream, converse, converse stream), exceptions are caught, spans are annotated with error types, span status is set to ERROR, exception counters are incremented, and exceptions are re-raised. New imports for Status, StatusCode, and ERROR_TYPE were added.

Changes

Cohort / File(s) Summary
Error Handling & Metrics for Bedrock Calls
packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py
Added try/except blocks around wrapped Bedrock API calls (model invoke, invoke with response stream, converse, converse stream). Each catch block annotates spans with error type, sets span status to ERROR, records exception metrics with error.type attribute, explicitly ends spans where applicable, and re-raises the exception. Imported Status, StatusCode, and ERROR_TYPE from opentelemetry.trace.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hop, catch, annotate, and bound,
Error metrics dance around,
Bedrock spans now stand so tall,
With try-except blocks for all,
Exceptions logged before they fall! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: adding error logging/handling to instrumented Bedrock functions through try/except blocks with exception metrics and span annotations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to 9685686 in 1 minute and 0 seconds. Click for details.
  • Reviewed 96 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py:207
  • Draft comment:
    Good use of try/except in _instrumented_model_invoke to record exceptions on the span. The error attributes and metric update look correct.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py:236
  • Draft comment:
    The _instrumented_model_invoke_with_response_stream correctly calls span.end() on exception and updates the counter; this aligns with non-context span usage.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py:268
  • Draft comment:
    Exception handling in _instrumented_converse properly records errors, sets span status, and updates metrics in line with OTel guidelines.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
4. packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py:295
  • Draft comment:
    In _instrumented_converse_stream, the try/except properly ends the span on error and updates metrics, which is consistent with other implementations.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_s6DpVBM1bjVYIxGt

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@kxbnb
Copy link
Author

kxbnb commented Jan 21, 2026

recheck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants