Skip to content

Commit 3f459d3

Browse files
authored
Add test case for child-parent exception handling (#3356)
1 parent 570d27e commit 3f459d3

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,11 @@ def use_span(
588588
description=f"{type(exc).__name__}: {exc}",
589589
)
590590
)
591+
592+
# This causes parent spans to set their status to ERROR and to record
593+
# an exception as an event if a child span raises an exception even if
594+
# such child span was started with both record_exception and
595+
# set_status_on_exception attributes set to False.
591596
raise
592597

593598
finally:

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@
5555
get_span_with_dropped_attributes_events_links,
5656
new_tracer,
5757
)
58-
from opentelemetry.trace import Status, StatusCode
58+
from opentelemetry.trace import (
59+
Status,
60+
StatusCode,
61+
get_tracer,
62+
set_tracer_provider,
63+
)
5964

6065

6166
class TestTracer(unittest.TestCase):
@@ -1852,3 +1857,88 @@ def test_constant_default_trace_options(self):
18521857
self.assertEqual(
18531858
trace_api.DEFAULT_TRACE_OPTIONS, trace_api.TraceFlags.DEFAULT
18541859
)
1860+
1861+
1862+
class TestParentChildSpanException(unittest.TestCase):
1863+
def test_parent_child_span_exception(self):
1864+
"""
1865+
Tests that a parent span has its status set to ERROR when a child span
1866+
raises an exception even when the child span has its
1867+
``record_exception`` and ``set_status_on_exception`` attributes
1868+
set to ``False``.
1869+
"""
1870+
1871+
set_tracer_provider(TracerProvider())
1872+
tracer = get_tracer(__name__)
1873+
1874+
exception = Exception("exception")
1875+
1876+
exception_type = exception.__class__.__name__
1877+
exception_message = exception.args[0]
1878+
1879+
try:
1880+
with tracer.start_as_current_span(
1881+
"parent",
1882+
) as parent_span:
1883+
with tracer.start_as_current_span(
1884+
"child",
1885+
record_exception=False,
1886+
set_status_on_exception=False,
1887+
) as child_span:
1888+
raise exception
1889+
1890+
except Exception: # pylint: disable=broad-except
1891+
pass
1892+
1893+
self.assertTrue(child_span.status.is_ok)
1894+
self.assertIsNone(child_span.status.description)
1895+
self.assertTupleEqual(child_span.events, ())
1896+
1897+
self.assertFalse(parent_span.status.is_ok)
1898+
self.assertEqual(
1899+
parent_span.status.description,
1900+
f"{exception_type}: {exception_message}",
1901+
)
1902+
self.assertEqual(
1903+
parent_span.events[0].attributes["exception.type"], exception_type
1904+
)
1905+
self.assertEqual(
1906+
parent_span.events[0].attributes["exception.message"],
1907+
exception_message,
1908+
)
1909+
1910+
def test_child_parent_span_exception(self):
1911+
"""
1912+
Tests that a child span does not have its status set to ERROR when a
1913+
parent span raises an exception and the parent span has its
1914+
``record_exception`` and ``set_status_on_exception`` attributes
1915+
set to ``False``.
1916+
"""
1917+
1918+
set_tracer_provider(TracerProvider())
1919+
tracer = get_tracer(__name__)
1920+
1921+
exception = Exception("exception")
1922+
1923+
try:
1924+
with tracer.start_as_current_span(
1925+
"parent",
1926+
record_exception=False,
1927+
set_status_on_exception=False,
1928+
) as parent_span:
1929+
with tracer.start_as_current_span(
1930+
"child",
1931+
) as child_span:
1932+
pass
1933+
raise exception
1934+
1935+
except Exception: # pylint: disable=broad-except
1936+
pass
1937+
1938+
self.assertTrue(child_span.status.is_ok)
1939+
self.assertIsNone(child_span.status.description)
1940+
self.assertTupleEqual(child_span.events, ())
1941+
1942+
self.assertTrue(parent_span.status.is_ok)
1943+
self.assertIsNone(parent_span.status.description)
1944+
self.assertTupleEqual(parent_span.events, ())

0 commit comments

Comments
 (0)