Skip to content

Commit 2b12af6

Browse files
committed
feat: add MDC to workflow execution (#3188)
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 06fa48a commit 2b12af6

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public static void withMDCForEvent(
6666
}
6767
}
6868

69+
public static void withMDCForResource(HasMetadata resource, Runnable runnable) {
70+
try {
71+
MDCUtils.addResourceInfo(resource);
72+
runnable.run();
73+
} finally {
74+
MDCUtils.removeResourceInfo();
75+
}
76+
}
77+
6978
public static void addResourceIDInfo(ResourceID resourceID) {
7079
if (enabled) {
7180
MDC.put(NAME, resourceID.getName());

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/NodeExecutor.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.slf4j.LoggerFactory;
2020

2121
import io.fabric8.kubernetes.api.model.HasMetadata;
22+
import io.javaoperatorsdk.operator.processing.MDCUtils;
2223

2324
abstract class NodeExecutor<R, P extends HasMetadata> implements Runnable {
2425

@@ -36,19 +37,22 @@ protected NodeExecutor(
3637

3738
@Override
3839
public void run() {
39-
try {
40-
doRun(dependentResourceNode);
41-
42-
} catch (Exception e) {
43-
// Exception is required because of Kotlin
44-
workflowExecutor.handleExceptionInExecutor(dependentResourceNode, e);
45-
} catch (Error e) {
46-
// without this user would see no sign about the error
47-
log.error("java.lang.Error during execution", e);
48-
throw e;
49-
} finally {
50-
workflowExecutor.handleNodeExecutionFinish(dependentResourceNode);
51-
}
40+
MDCUtils.withMDCForResource(
41+
workflowExecutor.primary,
42+
() -> {
43+
try {
44+
doRun(dependentResourceNode);
45+
} catch (Exception e) {
46+
// Exception is required because of Kotlin
47+
workflowExecutor.handleExceptionInExecutor(dependentResourceNode, e);
48+
} catch (Error e) {
49+
// without this user would see no sign about the error
50+
log.error("java.lang.Error during execution", e);
51+
throw e;
52+
} finally {
53+
workflowExecutor.handleNodeExecutionFinish(dependentResourceNode);
54+
}
55+
});
5256
}
5357

5458
protected abstract void doRun(DependentResourceNode<R, P> dependentResourceNode);

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/NodeExecutorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
import org.junit.jupiter.api.Disabled;
2222
import org.junit.jupiter.api.Test;
2323

24-
import static org.junit.jupiter.api.Assertions.*;
25-
2624
class NodeExecutorTest {
2725

28-
private NodeExecutor errorThrowingNodeExecutor =
26+
@SuppressWarnings({"rawtypes", "unchecked"})
27+
private final NodeExecutor errorThrowingNodeExecutor =
2928
new NodeExecutor(null, null) {
3029
@Override
3130
protected void doRun(DependentResourceNode dependentResourceNode) {

0 commit comments

Comments
 (0)