Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make trace_api.use_span() record BaseException as well as Exception #4406

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tmaxwell-anthropic
Copy link

@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
Copy link

linux-foundation-easycla bot commented Jan 31, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

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