Skip to content

fix(otel-plugin): clear MDC spanId when an attempt ends in ExecutionOtelPlugin - #896

Merged
zhongkechen merged 2 commits into
aws:mainfrom
pcbeingused333:fix/c4623-mdc-span-id
Nov 19, 2026
Merged

zhongkechen merged 2 commits into
aws:mainfrom
pcbeingused333:fix/c4623-mdc-span-id

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Issue Link, if available

Fixes #815

Description

ExecutionOtelPlugin.onUserFunctionEnd closed the attempt scope but left the spanId MDC key set. onUserFunctionStart injects traceId, spanId and otelTraceSampled, so after an attempt ended, handler-level log lines between steps carried the spanId of the finished attempt. InvocationOtelPlugin.onUserFunctionEnd already removes spanId there and keeps traceId; this makes ExecutionOtelPlugin do the same (guarded by enableMdc).

Regression test (rule 5): ExecutionOtelPluginTest.onUserFunctionEnd_withMdcEnabled_clearsSpanIdButKeepsTraceId fails on current main (spanId should be cleared after the attempt ends) and passes with this change.

Demo/Screenshots

Not applicable.

Design

  • Design note (rule 3): no new API or option; the same one-line behavior InvocationOtelPlugin already has.
  • Simpler options considered (rule 2): none simpler.
  • Root cause (rule 5): onUserFunctionEnd in ExecutionOtelPlugin never touched the MDC.
  • Net change: +5 source lines, 0 public API members.

Assumptions

  • traceId and otelTraceSampled should persist between steps, as in InvocationOtelPlugin. Evidence: MdcSpanEnricherTest.plugin_withMdcEnabled_setsFieldsInMdc.

Decisions not covered by the issue or the design note

  • None.

Checklist

  • I have filled out every section of the PR template
  • I have thoroughly tested this change
  • Every new branch has a test that fails without it (rule 4)
  • For a bug fix: the description or commit order shows the regression test failing before the fix and passing after it (rule 5)
  • Every edited test expectation is listed in the description, with the reason (rule 5): none edited

Testing

Unit Tests

Yes: ExecutionOtelPluginTest.onUserFunctionEnd_withMdcEnabled_clearsSpanIdButKeepsTraceId (MDC enabled; spanId removed after the attempt ends, traceId kept until the invocation ends). mvn -pl otel-plugin -am test -Dtest=ExecutionOtelPluginTest passes.

Integration Tests

No: isolated bug fix fully covered by the unit test (CONTRIBUTING step 3).

Examples

Not applicable.

Disclosure

This change was prepared with the help of an AI coding assistant (Claude). The code and tests were run locally; the final diff is submitted for maintainer review.

…telPlugin

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@pcbeingused333
pcbeingused333 requested a review from a team November 19, 2026 12:12
@pcbeingused333
pcbeingused333 deployed to ai-pr-review-runtime November 19, 2026 12:12 — with GitHub Actions Active
@pcbeingused333

Copy link
Copy Markdown
Contributor Author
  • "What if this line throws?": the MDC cleanup now runs in a finally, so attemptScopes.close(key) throwing can no longer leave a stale spanId in the MDC.
  • "assert span Id is not injected when MDC is disabled": added userFunction_withMdcDisabled_doesNotInjectSpanId.

This comment was drafted with Claude Code.

@zhongkechen

Copy link
Copy Markdown
Contributor

@pcbeingused333 Thank you for your contribution. Approved.

@zhongkechen
zhongkechen merged commit 2d7b273 into aws:main Nov 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Execution OTel view retains the ended attempt spanId in MDC

2 participants