You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/09/10 01:03:13 UTC
[brooklyn-server] 02/05: restore MDC logging context to previous
value after task ends
This is an automated email from the ASF dual-hosted git repository.
heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 2f2a36d07094bf0b91c032e8e629f67e21c4821b
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Fri Sep 10 01:41:23 2021 +0100
restore MDC logging context to previous value after task ends
---
.../util/core/task/BasicExecutionManager.java | 32 +++++++++++++++-------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
index 5e33b61..e59e9ed 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
@@ -130,6 +130,7 @@ public class BasicExecutionManager implements ExecutionManager {
Task task;
MDC.MDCCloseable taskMdc=null, entityMdc=null;
+ String prevTaskMdc, prevEntityMdc;
private BrooklynTaskLoggingMdc() {}
@@ -141,6 +142,19 @@ public class BasicExecutionManager implements ExecutionManager {
public BrooklynTaskLoggingMdc start() {
Entity entity = BrooklynTaskTags.getTargetOrContextEntity(task);
+ // set context _before_ the log so that context shows this task as starter;
+ // the log _message_ shows who submitted it, which is more reliable than
+ // the prevTaskMdc as we might be in a different thread and/or the prevTaskMdc
+ // can misleadingly point point to the task which triggered the executor
+ if (task!=null) {
+ prevTaskMdc = MDC.get(LOGGING_MDC_KEY_TASK_ID);
+ taskMdc = MDC.putCloseable(LOGGING_MDC_KEY_TASK_ID, task.getId());
+ }
+ if (entity != null) {
+ prevEntityMdc = MDC.get(LOGGING_MDC_KEY_ENTITY_IDS);
+ entityMdc = MDC.putCloseable(LOGGING_MDC_KEY_ENTITY_IDS, "[" + entity.getApplicationId() + "," + entity.getId() + "]");
+ }
+
if (BrooklynLoggingCategories.TASK_LIFECYCLE_LOG.isDebugEnabled() || BrooklynLoggingCategories.TASK_LIFECYCLE_LOG.isTraceEnabled()){
String taskName = task.getDisplayName();
String message = "Starting task " + task.getId() +
@@ -154,14 +168,6 @@ public class BasicExecutionManager implements ExecutionManager {
}
- // set context _after_ the log so that context shows which task started this
- if (task!=null) {
- taskMdc = MDC.putCloseable(LOGGING_MDC_KEY_TASK_ID, task.getId());
- }
- if (entity != null) {
- entityMdc = MDC.putCloseable(LOGGING_MDC_KEY_ENTITY_IDS, "[" + entity.getApplicationId() + "," + entity.getId() + "]");
- }
-
return this;
}
@@ -172,8 +178,14 @@ public class BasicExecutionManager implements ExecutionManager {
UNINTERESTING_TASK_NAMES.contains(taskName) ? BrooklynLogging.LoggingLevel.TRACE : BrooklynLogging.LoggingLevel.DEBUG,
"Ending task {}", task.getId());
}
- if (entityMdc != null) entityMdc.close();
- if (taskMdc != null) taskMdc.close();
+ if (entityMdc != null) {
+ entityMdc.close();
+ if (prevEntityMdc!=null) MDC.put(LOGGING_MDC_KEY_ENTITY_IDS, prevEntityMdc);
+ }
+ if (taskMdc != null) {
+ taskMdc.close();
+ if (prevTaskMdc!=null) MDC.put(LOGGING_MDC_KEY_TASK_ID, prevTaskMdc);
+ }
}
public void close() {