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..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,8 +622,16 @@ 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); + 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 f15f73871..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 @@ -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,67 @@ 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)); + } + + @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