ISSUE DESCRIPTION MDC context no longer applied consistently in task/flow logs after…
- User
ISSUE DESCRIPTION
MDC context no longer applied consistently in task/flow logs after upgrade to v1.0
Describe the issue
Hi,
We recently upgraded from kestra v0.19.5 to v1.0.3 and noticed a change in the logging behavior.
when using a logback config as such
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <include resource="logback/base.xml" /> <include resource="logback/gcp.xml" /> <root level="WARN"> <appender-ref ref="CONSOLE_JSON_OUT" /> <appender-ref ref="CONSOLE_JSON_ERR" /> </root> </configuration>the additionnal info from the task/flow/execution are not always logged.
example from a Hello-world flow run:
{"timestampSeconds":1761224032,"timestampNanos":565000000,"severity":"INFO","thread":"jdbc-execution-executor_0","logger":"execution.gnu_170208","message":"[tenant: main] [namespace: company.team] [flow: gnu_170208] [execution: 1FLhPmmXgvIghVXzW7hY90] Flow started","context":"default"} {"timestampSeconds":1761224032,"timestampNanos":775000000,"severity":"INFO","thread":"worker_0","logger":"task.gnu_170208.hello","message":"[tenant: main] [namespace: company.team] [flow: gnu_170208] [task: hello] [execution: 1FLhPmmXgvIghVXzW7hY90] [taskrun: 7SKeqte94nC0xUfrgFXHpw] Type Log started","context":"default"} {"taskRunId":"7SKeqte94nC0xUfrgFXHpw","tenantId":"main","namespace":"company.team","executionId":"1FLhPmmXgvIghVXzW7hY90","flowId":"gnu_170208","taskId":"hello","timestampSeconds":1761224032,"timestampNanos":790000000,"severity":"INFO","thread":"worker_0","logger":"flow.gnu_170208.hello","message":"Hello World! 🚀"} {"timestampSeconds":1761224032,"timestampNanos":799000000,"severity":"INFO","thread":"worker_0","logger":"task.gnu_170208.hello","message":"[tenant: main] [namespace: company.team] [flow: gnu_170208] [task: hello] [execution: 1FLhPmmXgvIghVXzW7hY90] [taskrun: 7SKeqte94nC0xUfrgFXHpw] Type Log with state SUCCESS completed in 00:00:00.232","context":"default"} {"timestampSeconds":1761224032,"timestampNanos":852000000,"severity":"INFO","thread":"jdbc-execution-executor_2","logger":"execution.gnu_170208","message":"[tenant: main] [namespace: company.team] [flow: gnu_170208] [execution: 1FLhPmmXgvIghVXzW7hY90] Flow completed with state SUCCESS in 00:00:00.405","context":"default"}Only the hello world log itself gets the MDC context.
It was different in v0.19.5 we relied on this behavior to monitor flow executions.
I've debug kestra locally and i've been able to add that context by manually setting the contextMap in this method https://github.com/kestra-io/kestra/blob/eb22d3f6ee5b6b92f8a12f03948e7517a02e39cc/core/src/main/java/io/kestra/core/services/LogService.java#L45
Assistant
Let me analyze the issue:
- The user upgraded from Kestra v0.19.5 to v1.0.3
- The MDC (Mapped Diagnostic Context) context is no longer being applied consistently in task/flow logs
- Only the actual log message (like "Hello World! 🚀") gets the MDC context
- The surrounding context information (tenant, namespace, flow, execution) is missing from most logs
The user suggests manually setting the contextMap in the
logExecutionmethod inLogService.java.Let me first explore the repository structure and understand the codebase better.
Assistant
Let me search for where logging and MDC context are handled:
Assistant
Let me search for where logging happens and how MDC context is set or used:
<tool_call> <function=bash_terminal> <parameter=command> grep -r "logback|logback/" /kestra/core/src/main/resources 2>/dev/null | head -20 </tool_call>
Assistant
Let me search for logback configuration resources in the repository.
Assistant
Now let me search for where the logger is used and how MDC context is set. The issue mentions that MDC context is not being applied consistently to task/flow logs. Let me look for logging-related code.
Assistant
I can see the
RunContextLogger.javafile. Let me look more closely at theinitializeLoggermethod where the MDC context is being set. On line ~177-181:if (this.logEntry != null) { loggerContext.getMDCAdapter().setContextMap(this.logEntry.toMap()); }So the MDC context is being set from the
logEntry.toMap(). Let me check what fields are included in that map. Let me also check theLogEntryclass to see what's being included.<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/models/executions/LogEntry.java </tool_call>
Assistant
Let me run a bash command to see the LogEntry class.
Assistant
Now I understand the issue better. The
toMap()method includes fields liketenantId,namespace,flowId,taskId,executionId, etc. However, looking at the user's example logs:{"taskRunId":"7SKeqte94nC0xUfrgFXHpw","tenantId":"main","namespace":"company.team","executionId":"1FLhPmmXgvIghVXzW7hY90","flowId":"gnu_170208","taskId":"hello","timestampSeconds":1761224032,"timestampNanos":790000000,"severity":"INFO","thread":"worker_0","logger":"flow.gnu_170208.hello","message":"Hello World! 🚀"}The log message itself has the MDC context (tenantId, namespace, etc.), but other logs like:
{"timestampSeconds":1761224032,"timestampNanos":775000000,"severity":"INFO","thread":"worker_0","logger":"task.gnu_170208.hello","message":"[tenant: main] [namespace: company.team] [flow: gnu_170208] [task: hello] [execution: 1FLhPmmXgvIghVXzW7hY90] [taskrun: 7SKeqte94nC0xUfrgFXHpw] Type Log started","context":"default"}This log doesn't have the MDC context - it has a
context:"default"which suggests the MDC context wasn't applied properly.Assistant
Let me try again with proper command formatting.
Assistant
Now let me look at the RunContextLoggerFactory to see how the logger is created:
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/runners/RunContextLoggerFactory.java </tool_call>
Assistant
I need to properly format the bash command.
Assistant
Now I see how the logger is being created. The
RunContextLoggeris created with aLogEntrywhich contains the context information. In theRunContextLogger.initializeLogger()method, thisLogEntryis used to set the MDC context vialoggerContext.getMDCAdapter().setContextMap(this.logEntry.toMap()).Let me trace back to see where logs are generated that don't have the MDC context properly. The issue seems to be that the MDC context is set on the logger context, but some logs are not using that logger properly, or the MDC context is being cleared before the log is emitted.
Let me search for where the execution-level logs are being generated:
Assistant
Let me look at the Logs.java file to see the logExecution method:
Assistant
Now I see the issue! In the
Logs.javafile, the methods likelogExecution,logTaskRun, andlogTriggerare using standard SLF4J loggers obtained viaLoggerFactory.getLogger(). These loggers are NOT using theRunContextLoggerwhich has the MDC context set up properly.The
RunContextLoggercreates its own logger context with MDC adapter and sets the context fromLogEntry.toMap(). But the class is using the global SLF4J logger which doesn't have this context.Assistant
Let me try again with a proper bash command to search for MDC imports.
Assistant
Let me search for how MDC is used in the codebase:
<tool_call> <function=bash_terminal> <parameter=command> grep -r "MDC." /kestra/core/src/main/java --include="*.java" | head -20 </tool_call>
Assistant
I need to fix the bash command formatting issue.
Assistant
Let me look at the RunContext.java file to see how MDC is managed there.
Assistant
Now let me look at the DefaultRunContext.java to see how MDC is reset:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "resetMDC|MDC|logger." /kestra/core/src/main/java/io/kestra/core/runners/DefaultRunContext.java | head -30 </tool_call>
Assistant
Let me fix the bash command format to search properly for MDC related content in DefaultRunContext.java.
Assistant
Now let me look at the DefaultRunContext.java file to understand how logging works there:
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/runners/DefaultRunContext.java </tool_call>
Assistant
Let me continue searching for where the logging MDC context is managed. The issue is that when using Logs.logExecution() and similar methods, the MDC context is not being set properly.
Looking at the code I've found so far:
RunContextLogger.initializeLogger()sets the MDC context fromLogEntry.toMap()Logs.javausesLoggerFactory.getLogger()which creates global loggers that don't have the MDC context set
The issue is that the MDC context is thread-local. When
Logs.logExecution()is called, it uses a globally configured logger which doesn't have the MDC context set up for the execution.The fix should ensure that when using
Logs.logExecution()and similar methods, we set the MDC context before logging and clear it after. Let me look at how this can be done.Let me first look at the git history to understand what changed between v0.19.5 and v1.0:
Assistant
I need to see more context around line 529 in DefaultRunContext.java. Let me open that file with the editor tool.
Assistant
Let me understand the issue better. The problem is:
- When
RunContextLoggeris created with aLogEntry, it sets the MDC context ininitializeLogger() - But
Logs.javamethods useLoggerFactory.getLogger()which creates separate logger instances that don't have the MDC context set
The solution is to modify
Logs.javato set the MDC context before logging and clear it after, similar to how the user suggested. Let me look at the current implementation and fix it.<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
- When
Assistant
Let me view the Logs.java file using the open command:
Assistant
Let me continue viewing the Logs.java file to see all the methods.
<tool_call> <function=bash_terminal> <parameter=command> tail -50 /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
I need to use the correct tool syntax for running bash commands. Let me continue viewing the Logs.java file.
Assistant
Now I understand the issue. The
Logs.javaclass usesLoggerFactory.getLogger()which creates global SLF4J loggers. These loggers don't have the MDC context set fromLogEntry.toMap()because they're not using theRunContextLogger.The fix is to set the MDC context before logging and clear it after. The standard SLF4J
MDCclass provides methods for this:MDC.put(key, value)MDC.setContextMap(Map<String, String>)MDC.clear()
Let me modify
Logs.javato set the MDC context before logging. I'll need to add the import fororg.slf4j.MDCand update the methods to set and clear the MDC context.Let me look at the full Logs.java file first:
<tool_call> <function=bash_terminal> <parameter=command> wc -l /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
I need to run the bash command properly to count lines in the Logs.java file.
Assistant
Now I understand the issue. The
Logs.javaclass usesLoggerFactory.getLogger()which creates global loggers that don't have the MDC context set up. The fix is to modifyLogs.javato set the MDC context before logging usingorg.slf4j.MDC.Let me create the fix for
Logs.java:Assistant
Let me look at the entire Logs.java file to understand the structure better.
<tool_call> <function=bash_terminal> <parameter=command> cat -n /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
I need to fix my bash command syntax. The previous commands were not running properly. Let me try again with the proper syntax.
Assistant
Now I understand the issue. The
Logs.javaclass usesLoggerFactory.getLogger()which creates global loggers that don't have the MDC context set. The fix is to set the MDC context before logging and clear it after.Let me update the
Logs.javafile to add the MDC context:Assistant
The file was created successfully. Now let me verify the changes by viewing the file:
<tool_call> <function=bash_terminal> <parameter=command> cat -n /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
Let me verify the changes by viewing the updated Logs.java file.
Assistant
The file looks correct. Now let me try to build the project to verify there are no compilation errors:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && mvn compile -pl core -am -DskipTests -q 2>&1 | head -50 </tool_call>
Assistant
I need to fix the bash command format again. Let me try to compile the core module.
Assistant
The project name is different. Let me first check the project structure to understand the correct module name.
Assistant
Let me explore the repository structure better.
Assistant
I see this is a Gradle project. Let me compile the core module using Gradle.
Assistant
The compilation succeeded! The Logs.java file compiles correctly. Now let me run the tests related to logging to make sure the fix doesn't break anything.
Let me first find the relevant tests:
<tool_call> <function=bash_terminal> <parameter=command> find /kestra -path "/test/" -name "Logs" -type f 2>/dev/null </tool_call>
Assistant
I need to find the test files for Logs.java to ensure my changes don't break existing tests.
Assistant
Let me run the LogsTest.java test to make sure my changes don't break existing tests.
Assistant
All 4 tests pass. Let me also look at the test file to see what it's testing to ensure my changes are consistent:
Assistant
The tests pass, and they test the basic logging functionality. Now I need to think about edge cases and whether the fix is complete.
Looking at the original issue, the problem is that logs are not always getting the MDC context. The user mentioned that the
RunContextLoggeris used for task logs (which work), but theLogs.logExecution()methods useLoggerFactory.getLogger()which doesn't have the MDC context.My fix adds MDC context setting before logging in all the
Logsmethods. However, I should also check if there are any other places where logging is done that might not have the MDC context.Let me search for other places where logging might be happening:
<tool_call> <function=bash_terminal> <parameter=command> grep -r "logger.atLevel|logger.log|.info(|.error(" /kestra/core/src/main/java/io/kestra/core/runners --include="*.java" | grep -v "test" | grep -v "//" | head -20 </tool_call>
Assistant
Let me fix the bash command format to search for logging patterns in the runners directory.
Assistant
The logging in
ExecutableUtils.javausesrunContext.logger()which is theRunContextLoggerthat has the MDC context set. Let me check if there are any other places where global loggers are used.Let me look at the specific places where
log.erroris called with@Slf4jannotation:<tool_call> <function=bash_terminal> <parameter=command> grep -rn "private static final.log|@Slf4j" /kestra/core/src/main/java/io/kestra/core/runners/.java | head -20 </tool_call>
Assistant
Let me try to fix the bash command format.
Assistant
Let me look at the
ExecutableUtils.javafile to see if there are any logging calls that might not have the MDC context set.Assistant
Let me check if this logging in ExecutableUtils.java needs MDC context. Looking at the context, this is in a utility method that creates subflow execution results. Let me look at the broader context.
Assistant
This is a utility method that handles subflow execution results. It uses the class-level logger which gets its MDC context from the
RunContextpassed to it. This is fine because it usesrunContext.logger()internally.Let me look at
DefaultFlowMetaStore.javato see if there are logging calls that might need MDC context:<tool_call> <function=bash_terminal> <parameter=command> grep -n "log." /kestra/core/src/main/java/io/kestra/core/runners/DefaultFlowMetaStore.java </tool_call>
Assistant
Let me try to fix the bash command format.
Assistant
These are error logs in
DefaultFlowMetaStore.java. These are using the class-level logger which doesn't have the MDC context set. However, these are not related to task/flow execution logs - they're more internal infrastructure logs.The key issue from the user's report is that when using a logback config, the MDC context is not being applied to task/flow/execution logs. The user's example shows that only the actual task log message (like "Hello World! 🚀") has the MDC context, while the surrounding execution context logs don't have it.
My fix to
Logs.javashould address this issue because theLogs.logExecution()methods are used for logging execution-level events like "Flow started", "Flow completed", etc.Let me now also check if there are any other places where
Logs.logExecution()might need to be updated or if there are any other methods that should have MDC context set.Let me search for where
Logs.logExecutionis used:<tool_call> <function=bash_terminal> <parameter=command> grep -rn "Logs.logExecution|Logs.logTrigger|Logs.logTaskRun" /kestra --include="*.java" | grep -v "test" | head -30 </tool_call>
Assistant
Let me try to fix the bash command format again.
Assistant
The
Logs.logTaskRun,Logs.logTrigger, andLogs.logExecutionmethods are used in multiple places in the worker module. My fix to set MDC context before logging should address the issue for all these call sites.Let me also check the core module for usage of these methods:
Assistant
I see that
Logs.logExecutionis used inPluginDefaultService.java. The fix I made toLogs.javashould apply here as well.Now let me run more comprehensive tests to ensure the fix doesn't break anything. Let me run the tests for the core module:
Assistant
Let me run some specific tests related to logging and execution:
Assistant
The test passed. Let me also verify the change by checking if there are any edge cases I need to handle.
One thing I should consider is thread safety. MDC context is thread-local, so setting and clearing it in the
try-finallyblock should work correctly. Each thread will have its own MDC context.Let me also check if the original Logs.java file had the import for MDC:
<tool_call> <function=bash_terminal> <parameter=command> git diff HEAD -- /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
Let me check the git diff to see the changes made to Logs.java.
Assistant
The diff shows all the changes I made to
Logs.java. The changes look correct:- Added imports for
org.slf4j.MDC,HashMap, andMap - Added helper methods to create MDC context maps for different types (Execution, Flow, Trigger, TaskRun)
- Modified all logging methods to set MDC context before logging and clear it after
Now let me think about edge cases:
-
Null values: The MDC context map might include null values if the execution/flow/trigger/taskRun has null values for some fields. However,
MDC.setContextMap()handles null values gracefully. -
Thread safety: MDC context is thread-local, so setting and clearing it in the
try-finallyblock should work correctly. Each thread will have its own MDC context.
- Added imports for
Assistant
I need to run the tests correctly. Let me try again with the proper bash command format.
Assistant
All 4 tests pass. Let me also verify that the compilation succeeds for the entire project to make sure there are no other issues:
Assistant
The build is successful. Now let me also check if there are any other modules that use
Logs.logExecutionand make sure they compile:<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :executor:compileJava 2>&1 | tail -20 </tool_call>
Assistant
I need to fix the bash command format to compile the executor module.
Assistant
Good, the executor module compiles successfully. Let me also check the scheduler module since it was mentioned in the grep results earlier.
Now, let me think about the overall fix:
-
Root Cause: The
Logs.javaclass usesLoggerFactory.getLogger()which creates global SLF4J loggers that don't have the MDC context set. This means that logs like "Flow started", "Flow completed", etc. don't have the execution context (tenantId, namespace, flowId, executionId) in their MDC context. -
Fix: Modified
Logs.javato set the MDC context before logging and clear it after. This ensures that all logs generated through theLogsutility class have the proper MDC context. -
Edge Cases Handled:
- Thread safety: MDC context is thread-local, so each thread has its own context.
- Exceptions: The
finallyblock ensures thatMDC.clear()is called even if an exception occurs. - Null values:
MDC.setContextMap()handles null values gracefully.
Let me also run a quick test to ensure that the method works correctly with the MDC context:
-
Assistant
Let me fix the bash command format to run the WorkerTaskTest.
Assistant
The test class doesn't exist. Let me find the correct test class to run:
<tool_call> <function=bash_terminal> <parameter=command> find /kestra/worker -name "Test.java" -path "/test/*" | head -10 </tool_call>
Assistant
Let me find the test class to run.
Assistant
Let me run the ExecutionKilledManagerTest which uses
Logs.logTaskRunandLogs.logTrigger:<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :worker:test --tests "io.kestra.worker.services.ExecutionKilledManagerTest" 2>&1 | tail -30 </tool_call>
Assistant
Let me fix the bash command format to run the ExecutionKilledManagerTest.
Assistant
All 12 tests pass. Now let me verify the fix one more time by looking at the issue description to ensure I've addressed all the concerns.
From the issue description:
- The user upgraded from v0.19.5 to v1.0.3
- The MDC context is not always applied to task/flow/execution logs
- Only the actual log message (like "Hello World! 🚀") has the MDC context
- Surrounding context logs (like "Flow started", "Flow completed") don't have the MDC context
My fix adds MDC context setting to all the
Logsutility methods (logExecution,logTrigger,logTaskRun). This should ensure that all logs generated through these methods have the proper MDC context.Let me also check if there are any other places in the codebase where logging is done without MDC context. Let me search for places where
@Slf4jis used and logging is done:<tool_call> <function=bash_terminal> <parameter=command> grep -rn "log.info|log.error|log.warn|log.debug" /kestra/core/src/main/java/io/kestra/core/services --include="*.java" | grep -v "test" | head -30 </tool_call>
Assistant
Let me search for logging patterns in the services directory.
Assistant
Let me check the git status to see all changes:
Assistant
Now let me verify my fix by understanding the issue more deeply. The issue states that MDC context is not being applied consistently to task/flow/execution logs after upgrading from v0.19.5 to v1.0.3.
Looking at the user's example logs:
- Logs like "Flow started", "Flow completed" have
"context":"default"- these are execution-level logs that don't have MDC context. - Logs like "Hello World! 🚀" have the MDC context fields (tenantId, namespace, flowId, executionId, taskRunId) directly in the JSON - these are task-level logs that go through
RunContextLogger.
The
RunContextLoggersets MDC context ininitializeLogger()usingLogEntry.toMap(). However, theLogs.logExecution()methods useLoggerFactory.getLogger()which creates global loggers without MDC context.My fix adds MDC context setting to all the
Logsutility methods, which should ensure that all logs generated through these methods have the proper MDC context.Let me now check if there are any other places where MDC context should be set. Let me look at the
RunContextLoggermore closely to understand how it works:- Logs like "Flow started", "Flow completed" have
Assistant
Let me fix the bash command format to grep for MDC-related methods in RunContextLogger.java.
Assistant
I see that the
RunContextLoggercreates its ownLoggerContextwith aLogbackMDCAdapterand sets the context map usingLogEntry.toMap(). This is the correct approach for task-level logs.However, the
Logs.javautility class usesLoggerFactory.getLogger()which creates global SLF4J loggers that don't have this custom MDC adapter. My fix adds MDC context setting usingorg.slf4j.MDCwhich is the standard way to set MDC context for SLF4J loggers.Let me now verify that the fix is complete by checking if there are any other places where execution-level logging is done:
Assistant
All the places where
Logs.logTaskRun,Logs.logTrigger, andLogs.logExecutionare used will now have MDC context set correctly thanks to my fix.Let me now summarize the fix and verify that it addresses the issue:
-
Issue: MDC context was not being applied consistently to task/flow/execution logs after upgrading to v1.0.3.
-
Root Cause: The
Logs.javautility class usesLoggerFactory.getLogger()which creates global SLF4J loggers that don't have the MDC context set. This means that logs like "Flow started", "Flow completed", "Killing running task", etc. don't have the execution context (tenantId, namespace, flowId, executionId) in their MDC context. -
Fix: Modified
Logs.javato set the MDC context before logging and clear it after. This ensures that all logs generated through theLogsutility class have the proper MDC context. -
Edge Cases Handled:
-
Assistant
Let me view the git diff to see the final changes before submitting.
Assistant
The fix is complete. Let me provide a summary of the changes:
Summary
Issue
MDC (Mapped Diagnostic Context) context was not being applied consistently to task/flow/execution logs after upgrading from v0.19.5 to v1.0.3. Only the actual task log messages (like "Hello World! 🚀") had the MDC context, while execution-level logs (like "Flow started", "Flow completed") did not have the context.
Root Cause
The
Logs.javautility class usesLoggerFactory.getLogger()which creates global SLF4J loggers that don't have the MDC context set fromLogEntry.toMap(). This is different fromRunContextLoggerwhich sets up its own MDC context.Fix
Modified
Logs.javato:- Add imports for
org.slf4j.MDC,HashMap, andMap - Add helper methods to create MDC context maps for Execution, Flow, Trigger, and TaskRun
- Add imports for
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.