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

revert BaseException change #4494

Merged
merged 7 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,9 @@ def use_span(
finally:
context_api.detach(token)

except BaseException as exc: # pylint: disable=broad-exception-caught
# Record only exceptions that inherit Exception class but not BaseException, because
# classes that directly inherit BaseException are not technically errors, e.g. GeneratorExit.
except Exception as exc: # pylint: disable=broad-exception-caught
if isinstance(span, Span) and span.is_recording():
# Record the exception as an event
if record_exception:
Expand Down
59 changes: 44 additions & 15 deletions opentelemetry-api/tests/trace/test_globals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from unittest.mock import Mock, patch

Expand All @@ -13,7 +27,12 @@ class SpanTest(trace.NonRecordingSpan):
recorded_status = Status(status_code=StatusCode.UNSET)

def set_status(self, status, description=None):
self.recorded_status = status
if isinstance(status, Status):
self.recorded_status = status
else:
self.recorded_status = Status(
status_code=status, description=description
)

def end(self, end_time=None):
self.has_ended = True
Expand Down Expand Up @@ -133,18 +152,6 @@ class TestUseSpanException(Exception):

self.assertEqual(test_span.recorded_exception, exception)

def test_use_span_base_exception(self):
class TestUseSpanBaseException(BaseException):
pass

test_span = SpanTest(trace.INVALID_SPAN_CONTEXT)
exception = TestUseSpanBaseException("test exception")
with self.assertRaises(TestUseSpanBaseException):
with trace.use_span(test_span):
raise exception

self.assertEqual(test_span.recorded_exception, exception)

def test_use_span_set_status(self):
class TestUseSpanException(Exception):
pass
Expand All @@ -155,10 +162,32 @@ class TestUseSpanException(Exception):
raise TestUseSpanException("test error")

self.assertEqual(
test_span.recorded_status.status_code, # type: ignore[reportAttributeAccessIssue]
test_span.recorded_status.status_code,
StatusCode.ERROR,
)
self.assertEqual(
test_span.recorded_status.description, # type: ignore[reportAttributeAccessIssue]
test_span.recorded_status.description,
"TestUseSpanException: test error",
)

def test_use_span_base_exceptions(self):
base_exception_classes = [
BaseException,
GeneratorExit,
SystemExit,
KeyboardInterrupt,
]

for exc_cls in base_exception_classes:
with self.subTest(exc=exc_cls.__name__):
test_span = SpanTest(trace.INVALID_SPAN_CONTEXT)

with self.assertRaises(exc_cls):
with trace.use_span(test_span):
raise exc_cls()

self.assertEqual(
test_span.recorded_status.status_code,
StatusCode.UNSET,
)
self.assertIsNone(test_span.recorded_status.description)
Loading