File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3+ import pytest
34from opentelemetry import trace
45from opentelemetry .trace import StatusCode
56
@@ -245,3 +246,20 @@ def test_send_error_with_context(spans):
245246 assert event .name == "exception"
246247 assert event .attributes ["exception.type" ] == "tests.test_tracing.FooError"
247248 assert event .attributes ["exception.message" ] == "Whoops!"
249+
250+
251+ def test_send_error_with_context_closes_span_when_block_raises (spans ):
252+ # An error raised inside the `with` block bubbles out to the caller, but the
253+ # error span is still ended and exported.
254+ with pytest .raises (RuntimeError , match = "Kaboom!" ):
255+ with send_error_with_context (raised_error ()):
256+ raise RuntimeError ("Kaboom!" )
257+
258+ span = spans ()[0 ]
259+ assert span .name == "FooError"
260+ assert span .status .status_code == StatusCode .ERROR
261+
262+ event = span .events [0 ]
263+ assert event .name == "exception"
264+ assert event .attributes ["exception.type" ] == "tests.test_tracing.FooError"
265+ assert event .attributes ["exception.message" ] == "Whoops!"
You can’t perform that action at this time.
0 commit comments