Skip to content

Conversation

@tmaxwell-anthropic
Copy link
Contributor

@tmaxwell-anthropic tmaxwell-anthropic commented Jan 31, 2025

Description

The Python SDK has inconsistent handling of BaseExceptions such as KeyboardInterrupt and StopIteration.

Consider the following code:

with tracer.start_span(...):
    raise BaseException("test")

The BaseException will be passed into Span.__exit__() (code), which will typically call span.record_exception(BaseException("test")); set the span's status to ERROR; and then emit the span.

However, start_as_current_span() behaves differently:

with tracer.start_as_current_span(...):
    raise BaseException("test")

The BaseException will be thrown into trace_api.use_span() (code). However, use_span() only handles except Exception, not except BaseException. So span.record_exception() is not called, and the span's status is not set to ERROR. However, the span is still emitted via the finally: block.

This behavior surprised me. In particular, when a span exits due to asyncio.CancelledError or trio.Cancelled, I was surprised that the span was emitted without recording any exception or setting the status to ERROR. I'm guessing this probably wasn't an intentional decision.

This PR proposes to change the except Exception to except BaseException, so that use_span() will now record BaseExceptions in the same way as regular Exceptions.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • New test in opentelemetry-api/tests/trace/test_globals.py to ensure that BaseException is recorded

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@tmaxwell-anthropic tmaxwell-anthropic requested a review from a team as a code owner January 31, 2025 23:41
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 31, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@xrmx xrmx enabled auto-merge (squash) February 28, 2025 10:29
@xrmx xrmx merged commit 1bd9ec6 into open-telemetry:main Feb 28, 2025
384 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in @xrmx's Python PR digest Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants