Skip to content

[chore] Remove exception handling around instrumentation code#148

Open
lmolkova wants to merge 6 commits into
open-telemetry:mainfrom
lmolkova:defensive-code-in-streams
Open

[chore] Remove exception handling around instrumentation code#148
lmolkova wants to merge 6 commits into
open-telemetry:mainfrom
lmolkova:defensive-code-in-streams

Conversation

@lmolkova

Copy link
Copy Markdown
Member

Remove _safe_finalize_success, _safe_finalize_failure methods and other places where we attempt to handle exceptions raised by instrumentation code.

Instrumentation code is not expected to raise. We must write it in the way that it doesn't.

Copilot AI review requested due to automatic review settings June 18, 2026 01:21
@lmolkova lmolkova requested a review from a team as a code owner June 18, 2026 01:21
@lmolkova lmolkova changed the title Remove exception handling around instrumentation code [chore] Remove exception handling around instrumentation code Jun 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Not ready to approve

It changes exception propagation so instrumentation hook failures can mask original stream/user exceptions and also breaks the repository’s existing stream-wrapper test expectations that these hook errors are swallowed/logged.

Pull request overview

This PR updates the shared GenAI stream wrapper utilities (SyncStreamWrapper / AsyncStreamWrapper) to remove internal “safe” exception-handling paths around instrumentation hooks (chunk processing and stream finalization), based on the expectation that instrumentation code should not raise.

Changes:

  • Replace calls to _safe_finalize_success/_safe_finalize_failure with direct _finalize_success/_finalize_failure.
  • Remove swallowing/logging behavior for exceptions raised by _process_chunk, _on_stream_end, and _on_stream_error (and remove the helper methods that performed that logging).
File summaries
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py Removes “safe” wrappers around instrumentation hook execution in sync/async stream wrappers, changing how exceptions propagate during iteration/close/finalization.

Copilot's findings

Comments suppressed due to low confidence (2)

util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py:126

  • _on_stream_end() exceptions can now escape from _finalize_success(), which can turn a normal end-of-stream into an unexpected exception (masking StopIteration / StopAsyncIteration) and break the wrapper’s "instrumentation must not fail the app" contract (existing tests expect finalize errors to be swallowed).

Catch and debug-log exceptions from the instrumentation hook inside _finalize_success() to keep iteration semantics stable.

    def _finalize_success(self) -> None:
        if self._self_finalized:
            return
        self._self_finalized = True
        self._on_stream_end()

util/opentelemetry-util-genai/src/opentelemetry/util/genai/stream.py:232

  • Async _finalize_success() now directly calls _on_stream_end() without guarding against instrumentation exceptions. If _on_stream_end() raises, it can turn a clean end-of-stream into an exception (masking StopAsyncIteration) and break the wrapper’s best-effort instrumentation behavior.

Catch and debug-log exceptions from _on_stream_end() inside _finalize_success().

    def _finalize_success(self) -> None:
        if self._self_finalized:
            return
        self._self_finalized = True
        self._on_stream_end()
  • Files reviewed: 1/1 changed files
  • Comments generated: 4

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.

@lmolkova lmolkova added the Skip Changelog PR does not require a changelog entry label Jun 18, 2026

@eternalcuriouslearner eternalcuriouslearner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!!

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

Labels

Skip Changelog PR does not require a changelog entry

Development

Successfully merging this pull request may close these issues.

3 participants