Skip to content

Commit 2be4d93

Browse files
authored
fix(otel): omit empty joined exception stacks (#902)
* test(otel): cover empty joined exception stacktrace * fix(otel): omit empty joined exception stacks
1 parent 4ecdecd commit 2be4d93

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎packages/aws-durable-execution-sdk-python-otel/README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,11 @@ enclosing Invocation and Workflow spans successful if the handler succeeds.
458458
An exception event carries `exception.type`, `exception.message` and
459459
`exception.stacktrace` only when the checkpoint stored them, so a partial error
460460
such as a callback failure with a message and no type omits the field it lacks
461-
instead of reporting an invented one. An explicitly supplied empty string stays
462-
an empty string and remains distinct from an absent field. The OTel exception
461+
instead of reporting an invented one. An explicitly supplied empty type or
462+
message stays an empty string and remains distinct from an absent field. A stack
463+
is emitted only when joining its stored lines with newlines produces a nonempty
464+
string; supplied whitespace and newlines are preserved. The plugin does not
465+
recover handler frames that the checkpoint did not store. The OTel exception
463466
conventions require whichever of type and message the other lacks, so an error
464467
supplying neither records no event at all; its failure still appears in the
465468
span's error status and in `durable.operation.status`.

‎packages/aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/exception_events.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def error_object_exception_attributes(error: ErrorObject) -> dict[str, Any]:
2727
attributes["exception.type"] = error.type
2828
if error.message is not None:
2929
attributes["exception.message"] = error.message
30-
if error.stack_trace:
31-
attributes["exception.stacktrace"] = "\n".join(error.stack_trace)
30+
stack_trace = "\n".join(error.stack_trace or [])
31+
if stack_trace:
32+
attributes["exception.stacktrace"] = stack_trace
3233
return attributes
3334

3435

‎packages/aws-durable-execution-sdk-python-otel/tests/test_status_mapping.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,49 @@ def test_replay_attempt_event_uses_stored_error_metadata(
468468
]
469469

470470

471+
@pytest.mark.parametrize("is_replayed", [False, True])
472+
@pytest.mark.parametrize("event_kind", ["operation", "attempt"])
473+
@pytest.mark.parametrize(
474+
"stack_trace", [None, [], [""], ["", ""], [" "], ["frame\n", "error"]]
475+
)
476+
def test_exception_stacktrace_requires_nonempty_joined_stack(
477+
instrumented_view: InstrumentedView,
478+
stack_trace: list[str] | None,
479+
event_kind: str,
480+
is_replayed: bool,
481+
) -> None:
482+
"""Only a nonempty stored stack string belongs in the exception event."""
483+
plugin, exporter = instrumented_view
484+
_start_invocation(plugin)
485+
error = ErrorObject.from_dict(
486+
ErrorObject("boom", "RuntimeError", None, stack_trace).to_dict()
487+
)
488+
if event_kind == "operation":
489+
plugin.on_operation_end(_operation_end_info(error, is_replayed=is_replayed))
490+
span_name = "wait"
491+
else:
492+
_run_failed_step_attempt(plugin, error, is_replayed=is_replayed)
493+
span_name = "step-1 attempt 1"
494+
_end_invocation(plugin)
495+
if (
496+
is_replayed
497+
and event_kind == "operation"
498+
and isinstance(plugin, ExecutionOtelPlugin)
499+
):
500+
assert all(s.name != span_name for s in exporter.get_finished_spans())
501+
return
502+
span = next(s for s in exporter.get_finished_spans() if s.name == span_name)
503+
expected = {"exception.type": "RuntimeError", "exception.message": "boom"}
504+
joined_stack = "\n".join(stack_trace or [])
505+
if joined_stack:
506+
expected["exception.stacktrace"] = joined_stack
507+
assert dict(_exception_attributes(span)) == expected
508+
assert span.status.status_code is StatusCode.ERROR
509+
assert span.start_time is not None
510+
assert span.end_time is not None
511+
assert span.start_time <= span.events[0].timestamp <= span.end_time
512+
513+
471514
@pytest.mark.parametrize(("message", "error_type", "expected"), PARTIAL_CASES)
472515
def test_failed_operation_omits_absent_error_fields(
473516
instrumented_view: InstrumentedView,

0 commit comments

Comments
 (0)