Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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));
Comment thread
zhongkechen marked this conversation as resolved.
Resolved

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
Expand Down