Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5821.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-sdk`: The experimental `Tracer._start_typed_span` and `Tracer._start_as_current_typed_span` no longer record exception span events by default; pass `record_exception=True` to opt in
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ def _start_typed_span(
a case-sensitive ASCII string of at most 255 characters that
starts with a letter, followed by letters, digits, ``_``,
``.``, ``-`` or ``/``. Not validated by the API.
kwargs: Options from :class:`_SpanOptions`, with the same defaults
as :meth:`start_span`. Implementations ignore options they
don't know.
kwargs: Options from :class:`_SpanOptions`. ``record_exception``
defaults to ``False``, ``set_status_on_exception`` to ``True``.
Implementations ignore options they don't know.

The default implementation calls :meth:`start_span` without ``type_``.
Implementations that support span type should override this method.
Expand Down
7 changes: 4 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

_KNOWN_SPAN_OPTIONS = frozenset(("record_exception", "set_status_on_exception"))
# (record_exception, set_status_on_exception) when no options are passed
_DEFAULT_SPAN_OPTIONS = (True, True)
_DEFAULT_SPAN_OPTIONS = (False, True)


def _is_valid_span_type(span_type: object) -> bool:
Expand All @@ -106,9 +106,10 @@ def _read_span_options(kwargs: Mapping[str, Any]) -> tuple[bool, bool]:
"Ignoring unknown span start options: %s",
sorted(kwargs.keys() - _KNOWN_SPAN_OPTIONS),
)
# Both default to True. Only an explicit False turns them off, other values are ignored.
# record_exception defaults to False, set_status_on_exception to True.
# Non-bool values fall back to the default.
return (
kwargs.get("record_exception") is not False,
kwargs.get("record_exception") is True,
kwargs.get("set_status_on_exception") is not False,
)

Expand Down
10 changes: 5 additions & 5 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,25 +885,25 @@ def test_typed_span_exception_handling_defaults(self):
with tracer._start_as_current_typed_span("s", type_="a") as span:
raise DummyError()
self.assertIs(span.status.status_code, StatusCode.ERROR)
self.assertEqual(span.events[0].name, "exception")
self.assertEqual(len(span.events), 0)

def test_typed_span_exception_flags_ignore_non_bool(self):
tracer = new_tracer()
with self.assertRaises(DummyError):
with tracer._start_as_current_typed_span("s", record_exception="no", set_status_on_exception=0) as span:
with tracer._start_as_current_typed_span("s", record_exception="yes", set_status_on_exception=0) as span:
raise DummyError()
self.assertIs(span.status.status_code, StatusCode.ERROR)
self.assertEqual(span.events[0].name, "exception")
self.assertEqual(len(span.events), 0)

def test_typed_span_exception_handling_from_kwargs(self):
tracer = new_tracer()
with self.assertRaises(DummyError):
with tracer._start_as_current_typed_span(
"s", type_="a", record_exception=False, set_status_on_exception=False
"s", type_="a", record_exception=True, set_status_on_exception=False
) as span:
raise DummyError()
self.assertIs(span.status.status_code, StatusCode.UNSET)
self.assertEqual(len(span.events), 0)
self.assertEqual(span.events[0].name, "exception")

def test_typed_span_unknown_options_warn(self):
tracer = new_tracer()
Expand Down
Loading