Skip to content

Commit d1dc2e1

Browse files
Add update info to MDC context (#2259)
1 parent 089bbea commit d1dc2e1

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

temporal-sdk/src/main/java/io/temporal/internal/logging/LoggerTag.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ private LoggerTag() {}
3838
public static final String SIDE_EFFECT_ID = "SideEffectId";
3939
public static final String CHILD_WORKFLOW_ID = "ChildWorkflowId";
4040
public static final String ATTEMPT = "Attempt";
41+
public static final String UPDATE_ID = "UpdateId";
42+
public static final String UPDATE_NAME = "UpdateName";
4143
}

temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflow.java

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.temporal.common.context.ContextPropagator;
3333
import io.temporal.common.converter.DataConverter;
3434
import io.temporal.common.converter.DefaultDataConverter;
35+
import io.temporal.internal.logging.LoggerTag;
3536
import io.temporal.internal.replay.ReplayWorkflow;
3637
import io.temporal.internal.replay.ReplayWorkflowContext;
3738
import io.temporal.internal.replay.WorkflowContext;
@@ -47,6 +48,7 @@
4748
import javax.annotation.Nullable;
4849
import org.slf4j.Logger;
4950
import org.slf4j.LoggerFactory;
51+
import org.slf4j.MDC;
5052

5153
/**
5254
* SyncWorkflow supports workflows that use synchronous blocking code. An instance is created per
@@ -166,6 +168,8 @@ public void handleUpdate(
166168
() -> {
167169
try {
168170
workflowContext.setCurrentUpdateInfo(updateInfo);
171+
MDC.put(LoggerTag.UPDATE_ID, updateInfo.getUpdateId());
172+
MDC.put(LoggerTag.UPDATE_NAME, updateInfo.getUpdateName());
169173
// Skip validator on replay
170174
if (!callbacks.isReplaying()) {
171175
try {

temporal-sdk/src/test/java/io/temporal/workflow/LoggerTest.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.ArrayList;
3737
import java.util.List;
3838
import java.util.UUID;
39+
import java.util.concurrent.CompletableFuture;
40+
import java.util.concurrent.ExecutionException;
3941
import org.junit.After;
4042
import org.junit.Before;
4143
import org.junit.Test;
@@ -71,13 +73,21 @@ public void tearDown() throws Exception {
7173

7274
@WorkflowInterface
7375
public interface TestWorkflow {
76+
@UpdateMethod
77+
void update(String id);
78+
7479
@WorkflowMethod
7580
void execute(String id);
7681
}
7782

7883
public static class TestLoggingInWorkflow implements LoggerTest.TestWorkflow {
7984
private final Logger workflowLogger = Workflow.getLogger(TestLoggingInWorkflow.class);
8085

86+
@Override
87+
public void update(String id) {
88+
workflowLogger.info("Updating workflow {}.", id);
89+
}
90+
8191
@Override
8292
public void execute(String id) {
8393
workflowLogger.info("Start executing workflow {}.", id);
@@ -107,7 +117,7 @@ public void executeChild(String id) {
107117
}
108118

109119
@Test
110-
public void testWorkflowLogger() {
120+
public void testWorkflowLogger() throws ExecutionException, InterruptedException {
111121
Worker worker = env.newWorker(taskQueue);
112122
worker.registerWorkflowImplementationTypes(
113123
TestLoggingInWorkflow.class, TestLoggerInChildWorkflow.class);
@@ -122,14 +132,18 @@ public void testWorkflowLogger() {
122132
LoggerTest.TestWorkflow workflow =
123133
workflowClient.newWorkflowStub(LoggerTest.TestWorkflow.class, options);
124134
String wfId = UUID.randomUUID().toString();
125-
workflow.execute(wfId);
126-
127-
assertEquals(1, matchingLines(String.format("Start executing workflow %s.", wfId)));
128-
assertEquals(1, matchingLines(String.format("Executing child workflow %s.", wfId)));
129-
assertEquals(1, matchingLines(String.format("Done executing workflow %s.", wfId)));
135+
CompletableFuture<Void> result = WorkflowClient.execute(workflow::execute, wfId);
136+
workflow.update(wfId);
137+
result.get();
138+
139+
assertEquals(1, matchingLines(String.format("Start executing workflow %s.", wfId), false));
140+
assertEquals(1, matchingLines(String.format("Executing child workflow %s.", wfId), false));
141+
assertEquals(1, matchingLines(String.format("Done executing workflow %s.", wfId), false));
142+
// Assert the update log is present
143+
assertEquals(1, matchingLines(String.format("Updating workflow %s.", wfId), true));
130144
}
131145

132-
private int matchingLines(String message) {
146+
private int matchingLines(String message, boolean isUpdateMethod) {
133147
int i = 0;
134148
// Make copy to avoid ConcurrentModificationException
135149
List<ILoggingEvent> list = new ArrayList<>(listAppender.list);
@@ -139,6 +153,10 @@ private int matchingLines(String message) {
139153
assertTrue(event.getMDCPropertyMap().containsKey(LoggerTag.WORKFLOW_TYPE));
140154
assertTrue(event.getMDCPropertyMap().containsKey(LoggerTag.RUN_ID));
141155
assertTrue(event.getMDCPropertyMap().containsKey(LoggerTag.TASK_QUEUE));
156+
if (isUpdateMethod) {
157+
assertTrue(event.getMDCPropertyMap().containsKey(LoggerTag.UPDATE_ID));
158+
assertTrue(event.getMDCPropertyMap().containsKey(LoggerTag.UPDATE_NAME));
159+
}
142160
i++;
143161
}
144162
}

0 commit comments

Comments
 (0)