Skip to content

Commit 525b523

Browse files
emdnetogrihabor
andauthored
api: revert catching BaseException in trace.use_span (#4494)
This reverts 1bd9ec6 since it's setting span as error for non error exceptions Signed-off-by: emdneto <[email protected]> Co-authored-by: Gregory Borodin <[email protected]>
1 parent 534cd38 commit 525b523

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
([#4475](https://github.com/open-telemetry/opentelemetry-python/pull/4475))
1212
- Improve performance of baggage operations
1313
([#4466](https://github.com/open-telemetry/opentelemetry-python/pull/4466))
14+
- api: Revert record `BaseException` change in `trace_api.use_span()`
15+
([#4494](https://github.com/open-telemetry/opentelemetry-python/pull/4494))
1416

1517
## Version 1.31.0/0.52b0 (2025-03-12)
1618

@@ -19,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1921
- Add type annotations to context's attach & detach
2022
([#4346](https://github.com/open-telemetry/opentelemetry-python/pull/4346))
2123
- Fix OTLP encoders missing instrumentation scope schema url and attributes
22-
([#4359](https://github.com/open-telemetry/opentelemetry-python/pull/4359))
24+
([#4359](https://github.com/open-telemetry/opentelemetry-python/pull/4359))
2325
- prometheus-exporter: fix labels out of place for data points with different
2426
attribute sets
2527
([#4413](https://github.com/open-telemetry/opentelemetry-python/pull/4413))

opentelemetry-api/src/opentelemetry/trace/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,10 @@ def use_span(
588588
finally:
589589
context_api.detach(token)
590590

591-
except BaseException as exc: # pylint: disable=broad-exception-caught
591+
# Record only exceptions that inherit Exception class but not BaseException, because
592+
# classes that directly inherit BaseException are not technically errors, e.g. GeneratorExit.
593+
# See https://github.com/open-telemetry/opentelemetry-python/issues/4484
594+
except Exception as exc: # pylint: disable=broad-exception-caught
592595
if isinstance(span, Span) and span.is_recording():
593596
# Record the exception as an event
594597
if record_exception:

opentelemetry-api/tests/trace/test_globals.py

+45-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import unittest
216
from unittest.mock import Mock, patch
317

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

1529
def set_status(self, status, description=None):
16-
self.recorded_status = status
30+
if isinstance(status, Status):
31+
self.recorded_status = status
32+
else:
33+
self.recorded_status = Status(
34+
status_code=status, description=description
35+
)
1736

1837
def end(self, end_time=None):
1938
self.has_ended = True
@@ -133,18 +152,6 @@ class TestUseSpanException(Exception):
133152

134153
self.assertEqual(test_span.recorded_exception, exception)
135154

136-
def test_use_span_base_exception(self):
137-
class TestUseSpanBaseException(BaseException):
138-
pass
139-
140-
test_span = SpanTest(trace.INVALID_SPAN_CONTEXT)
141-
exception = TestUseSpanBaseException("test exception")
142-
with self.assertRaises(TestUseSpanBaseException):
143-
with trace.use_span(test_span):
144-
raise exception
145-
146-
self.assertEqual(test_span.recorded_exception, exception)
147-
148155
def test_use_span_set_status(self):
149156
class TestUseSpanException(Exception):
150157
pass
@@ -155,10 +162,33 @@ class TestUseSpanException(Exception):
155162
raise TestUseSpanException("test error")
156163

157164
self.assertEqual(
158-
test_span.recorded_status.status_code, # type: ignore[reportAttributeAccessIssue]
165+
test_span.recorded_status.status_code,
159166
StatusCode.ERROR,
160167
)
161168
self.assertEqual(
162-
test_span.recorded_status.description, # type: ignore[reportAttributeAccessIssue]
169+
test_span.recorded_status.description,
163170
"TestUseSpanException: test error",
164171
)
172+
173+
def test_use_span_base_exceptions(self):
174+
base_exception_classes = [
175+
BaseException,
176+
GeneratorExit,
177+
SystemExit,
178+
KeyboardInterrupt,
179+
]
180+
181+
for exc_cls in base_exception_classes:
182+
with self.subTest(exc=exc_cls.__name__):
183+
test_span = SpanTest(trace.INVALID_SPAN_CONTEXT)
184+
185+
with self.assertRaises(exc_cls):
186+
with trace.use_span(test_span):
187+
raise exc_cls()
188+
189+
self.assertEqual(
190+
test_span.recorded_status.status_code,
191+
StatusCode.UNSET,
192+
)
193+
self.assertIsNone(test_span.recorded_status.description)
194+
self.assertIsNone(test_span.recorded_exception)

0 commit comments

Comments
 (0)