|
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):
|
@@ -1852,3 +1857,88 @@ def test_constant_default_trace_options(self):
|
1852 | 1857 | self.assertEqual(
|
1853 | 1858 | trace_api.DEFAULT_TRACE_OPTIONS, trace_api.TraceFlags.DEFAULT
|
1854 | 1859 | )
|
| 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