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+
115import unittest
216from 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