|
55 | 55 | get_span_with_dropped_attributes_events_links, |
56 | 56 | new_tracer, |
57 | 57 | ) |
58 | | -from opentelemetry.trace import Status, StatusCode |
| 58 | +from opentelemetry.trace import ( |
| 59 | + Status, |
| 60 | + StatusCode, |
| 61 | + get_tracer, |
| 62 | + set_tracer_provider, |
| 63 | +) |
59 | 64 |
|
60 | 65 |
|
61 | 66 | class TestTracer(unittest.TestCase): |
@@ -1828,3 +1833,52 @@ def test_constant_default_trace_options(self): |
1828 | 1833 | self.assertEqual( |
1829 | 1834 | trace_api.DEFAULT_TRACE_OPTIONS, trace_api.TraceFlags.DEFAULT |
1830 | 1835 | ) |
| 1836 | + |
| 1837 | + |
| 1838 | +class TestParentChildSpanException(unittest.TestCase): |
| 1839 | + def test_parent_child_span_exception(self): |
| 1840 | + """ |
| 1841 | + Tests that a parent span has its status set to ERROR when a child span |
| 1842 | + raises an exception even when the child span has its |
| 1843 | + ``record_exception`` and ``set_status_on_exception`` attributes |
| 1844 | + set to ``False``. |
| 1845 | + """ |
| 1846 | + |
| 1847 | + set_tracer_provider(TracerProvider()) |
| 1848 | + tracer = get_tracer(__name__) |
| 1849 | + |
| 1850 | + exception = Exception("exception") |
| 1851 | + |
| 1852 | + exception_type = exception.__class__.__name__ |
| 1853 | + exception_message = exception.args[0] |
| 1854 | + |
| 1855 | + try: |
| 1856 | + with tracer.start_as_current_span( |
| 1857 | + "parent", |
| 1858 | + ) as parent_span: |
| 1859 | + with tracer.start_as_current_span( |
| 1860 | + "child", |
| 1861 | + record_exception=False, |
| 1862 | + set_status_on_exception=False, |
| 1863 | + ) as child_span: |
| 1864 | + raise exception |
| 1865 | + |
| 1866 | + except Exception: # pylint: disable=broad-except |
| 1867 | + pass |
| 1868 | + |
| 1869 | + self.assertTrue(child_span.status.is_ok) |
| 1870 | + self.assertIsNone(child_span.status.description) |
| 1871 | + self.assertTupleEqual(child_span.events, ()) |
| 1872 | + |
| 1873 | + self.assertFalse(parent_span.status.is_ok) |
| 1874 | + self.assertEqual( |
| 1875 | + parent_span.status.description, |
| 1876 | + f"{exception_type}: {exception_message}", |
| 1877 | + ) |
| 1878 | + self.assertEqual( |
| 1879 | + parent_span.events[0].attributes["exception.type"], exception_type |
| 1880 | + ) |
| 1881 | + self.assertEqual( |
| 1882 | + parent_span.events[0].attributes["exception.message"], |
| 1883 | + exception_message, |
| 1884 | + ) |
0 commit comments