From a9ca0f4525dd09e6eef396f26366f53b06d1e45e Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 08:08:27 -0400 Subject: [PATCH 1/2] fix(otel-plugin): clear MDC spanId when an attempt ends in ExecutionOtelPlugin Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Alex Castillo --- .../durable/otel/ExecutionOtelPlugin.java | 5 +++ .../durable/otel/ExecutionOtelPluginTest.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java b/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java index 0ada70103..a4e9b72e4 100644 --- a/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java +++ b/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java @@ -625,6 +625,11 @@ public void onUserFunctionEnd(UserFunctionEndInfo info) { // A late user-function End still closes the scope on the owning thread. attemptScopes.close(key); + // Clear span-level MDC after user function completes (keep traceId for handler-level logs between steps) + if (enableMdc) { + MDC.remove(MdcSpanEnricher.MDC_SPAN_ID); + } + if (ended) return; // CONTEXT operations don't have attempt spans — scope cleanup is all we need diff --git a/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java b/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java index f15f73871..62053011c 100644 --- a/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java +++ b/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.MDC; import software.amazon.lambda.durable.execution.SuspendExecutionException; import software.amazon.lambda.durable.plugin.*; @@ -68,6 +69,41 @@ void tearDown() { OtelPluginAutoConfigurationState.resetInstalledForTest(); } + @Test + void onUserFunctionEnd_withMdcEnabled_clearsSpanIdButKeepsTraceId() { + var mdcPluginFactory = ExecutionOtelPlugin.factory( + SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(InMemorySpanExporter.create())), + OtelPluginConfig.builder() + .contextExtractor(() -> null) + .enableMdc(true) + .workflowSpanName("Workflow") + .build()); + var mdcPlugin = started(mdcPluginFactory, new InvocationInfo("req-1", ARN, true, Instant.now())); + + mdcPlugin.onUserFunctionStart( + new UserFunctionStartInfo("op-1", "step", "STEP", "Step", null, Instant.now(), false, 1)); + assertNotNull(MDC.get(MdcSpanEnricher.MDC_SPAN_ID)); + + mdcPlugin.onUserFunctionEnd(new UserFunctionEndInfo( + "op-1", + "step", + "STEP", + "Step", + null, + Instant.now(), + Instant.now(), + false, + 1, + UserFunctionOutcome.SUCCEEDED, + null)); + + assertNull(MDC.get(MdcSpanEnricher.MDC_SPAN_ID), "spanId should be cleared after the attempt ends"); + assertNotNull(MDC.get(MdcSpanEnricher.MDC_TRACE_ID), "traceId should persist between steps"); + + mdcPlugin.onInvocationEnd(new InvocationEndInfo("req-1", ARN, true, InvocationStatus.SUCCEEDED, null)); + assertNull(MDC.get(MdcSpanEnricher.MDC_TRACE_ID)); + } + // ─── Java agent path (global provider) ─────────────────────────────── @Test From dfaddafae55d5dfa4ffefd416a70a01df8d131db Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 15:00:20 -0400 Subject: [PATCH 2/2] fix(otel-plugin): clear MDC spanId in finally and test MDC-disabled path --- .../durable/otel/ExecutionOtelPlugin.java | 15 ++++++----- .../durable/otel/ExecutionOtelPluginTest.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java b/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java index a4e9b72e4..9921fa9fa 100644 --- a/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java +++ b/otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ExecutionOtelPlugin.java @@ -622,12 +622,15 @@ public void onUserFunctionEnd(UserFunctionEndInfo info) { var key = attemptKey(info.id(), info.attempt()); - // A late user-function End still closes the scope on the owning thread. - attemptScopes.close(key); - - // Clear span-level MDC after user function completes (keep traceId for handler-level logs between steps) - if (enableMdc) { - MDC.remove(MdcSpanEnricher.MDC_SPAN_ID); + try { + // A late user-function End still closes the scope on the owning thread. + attemptScopes.close(key); + } finally { + // Clear span-level MDC after user function completes (keep traceId for handler-level logs between + // steps), even if closing the scope throws. + if (enableMdc) { + MDC.remove(MdcSpanEnricher.MDC_SPAN_ID); + } } if (ended) return; diff --git a/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java b/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java index 62053011c..1ee1074cf 100644 --- a/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java +++ b/otel-plugin/src/test/java/software/amazon/lambda/durable/otel/ExecutionOtelPluginTest.java @@ -104,6 +104,32 @@ void onUserFunctionEnd_withMdcEnabled_clearsSpanIdButKeepsTraceId() { assertNull(MDC.get(MdcSpanEnricher.MDC_TRACE_ID)); } + @Test + void userFunction_withMdcDisabled_doesNotInjectSpanId() { + var disabledPlugin = started(factory, new InvocationInfo("req-1", ARN, true, Instant.now())); + + disabledPlugin.onUserFunctionStart( + new UserFunctionStartInfo("op-1", "step", "STEP", "Step", null, Instant.now(), false, 1)); + assertNull(MDC.get(MdcSpanEnricher.MDC_SPAN_ID), "spanId should not be injected when MDC is disabled"); + assertNull(MDC.get(MdcSpanEnricher.MDC_TRACE_ID), "traceId should not be injected when MDC is disabled"); + + disabledPlugin.onUserFunctionEnd(new UserFunctionEndInfo( + "op-1", + "step", + "STEP", + "Step", + null, + Instant.now(), + Instant.now(), + false, + 1, + UserFunctionOutcome.SUCCEEDED, + null)); + assertNull(MDC.get(MdcSpanEnricher.MDC_SPAN_ID)); + + disabledPlugin.onInvocationEnd(new InvocationEndInfo("req-1", ARN, true, InvocationStatus.SUCCEEDED, null)); + } + // ─── Java agent path (global provider) ─────────────────────────────── @Test