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 problem is that after upgrading from v0.19.5 to v1.0.3, the MDC (Mapped Diagnostic Context) context is no longer consistently applied in task/flow logs. The user mentions that in v0.19.5, the logs included MDC context information (tenant, namespace, flow, execution), but in v1.0.3, this context is missing from most logs - only the actual task output logs get this context.
The user has identified a potential fix in the LogService.java file at line 45, where they suggest manually setting the contextMap in the
logExecutionmethod.Let me start by exploring the repository structure and finding the relevant files.
Assistant
There's no LogService.java in this directory. The user mentioned
LogService.javaat line 45, but in the current codebase (v1.0), this might be named differently or located elsewhere. Let me search for files that might contain log execution methods and MDC context handling.<tool_call> <function=bash_terminal> <parameter=command> grep -r "logExecution" /kestra --include="*.java" | head -30 </tool_call>
Assistant
Let me search for logExecution in the codebase.
Assistant
Good, I found the Logs.java file. Let me examine it to understand the current implementation and see what needs to be fixed.
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
I need to use bash_terminal to run the command properly.
Assistant
Now I understand the issue. The current implementation uses a hardcoded message prefix with the tenant, namespace, flow, execution, etc. However, it doesn't use MDC (Mapped Diagnostic Context) to set these values in the logging context. This means that loggers configured with logback might not consistently apply the MDC context to all logs from the same thread.
The user mentioned that in v0.19.5, MDC context was being applied consistently. Looking at the issue description, the example shows that only some logs have the MDC context, while others use
context: "default".Let me check if there's any MDC usage in the codebase.
<tool_call> <function=bash_terminal> <parameter=command> grep -r "MDC" /kestra --include="*.java" | grep -v "target/" | head -40 </tool_call>
Assistant
I need to use bash_terminal correctly to run grep command.
Assistant
Let me look at the RunContextLogger to understand how MDC is currently being used:
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/runners/RunContextLogger.java </tool_call>
Assistant
I need to use bash_terminal correctly to view the file.
Assistant
Now I understand how the RunContextLogger works. It sets up MDC context when creating the logger. However, the issue is that when using
Logs.javamethods likelogExecution,logTaskRun, etc., they are not setting MDC context - they're just building a formatted message with the context information embedded.The problem the user is describing is that when they use a custom logback configuration, the MDC context is not consistently applied to all logs. This is because the Logs.java class just logs directly with the message prefix but doesn't set MDC, so only logs that go through RunContextLogger (which sets MDC) get the MDC context.
Let me check how
logTaskRunis being used to see where the inconsistency is:<tool_call> <function=bash_terminal> <parameter=command> grep -r "logTaskRun" /kestra --include="*.java" | grep -v "target/" | head -20 </tool_call>
Assistant
The command syntax seems to not be working correctly with bash_terminal. Let me try a different approach to run grep.
Assistant
This is interesting. Let me search for where Tasks are logging - specifically the loggers used by tasks.
Let me look for the Task run context and see how tasks are getting their logger.
Assistant
Let me check where the logger is used when tasks are running.
Let me search for how tasks get their loggers.
Assistant
Now let me understand the issue better. The problem is that
Logs.logExecution()andLogs.logTaskRun()are using formatted messages but not setting MDC context. Let me look at the git history to see if there were any changes between v0.19.5 and v1.0.3:Assistant
I see there was a change: "refactor(core): remove LogService". Let me check what the old LogService looked like to understand how MDC was being used before:
Assistant
Let me check what the old LogService looked like before the refactoring to understand how MDC was handled:
Assistant
The old LogService.java didn't seem to use MDC either. The issue is that the user is seeing MDC context being set in v0.19.5, but not in v1.0.3. Let me look at what changed between versions. Let me check how the logs are actually being emitted and look at the logs in the RunContextLogger which does set MDC.
Let me check what's different. The user mentioned that only "Hello World!" log gets the MDC context, but not the "Flow started" or "Flow completed" logs.
Looking at the issue description example:
Flow started- has MDC-like context in the message butcontext:"default"Type Log started- has MDC-like context in the message butcontext:"default"Hello World!- does NOT have the MDC-like prefix but the data is in separate fields (this is the RunContextLogger log)Type Log with state SUCCESS completed- has MDC-like context in the message but
Assistant
I need to run the command using bash_terminal correctly.
Assistant
Now I understand the issue. Looking at the Logs.java file and the test, I can see that:
-
The current implementation in Logs.java just formats the message with the context info (tenant, namespace, flow, execution, taskrun) as part of the message string.
-
The issue is that this doesn't use MDC (Mapped Diagnostic Context), so when using logback with a JSON layout, the MDC context isn't set properly.
The user mentioned that only the "Hello World!" log gets proper MDC context - that's because that log goes through RunContextLogger which properly sets up MDC.
The fix should be to:
- Set MDC context before logging using the
Logs.logExecution(),Logs.logTaskRun(),Logs.logTrigger()methods - Make sure to clear the MDC context after logging to avoid cross-contamination
However, I need to be careful about thread safety. Since MDC is thread-local, I need to ensure that:
-
Assistant
I need to view the LogEntry.java file. Let me use bash_terminal to run cat.
Assistant
Now I understand the issue. The
Logs.javautility class formats the message with context info but doesn't set MDC (Mapped Diagnostic Context). TheRunContextLoggerproperly sets MDC, which is why only "Hello World!" logs show proper MDC context while others havecontext:"default".The fix should be to set MDC context before logging and restore it after. Let me modify the
Logs.javafile to add MDC support:Assistant
Assistant
The command syntax isn't working. Let me try a different approach - I'll use the bash_terminal tool with the command parameter properly.
Assistant
Now I need to modify this file to add MDC support. Let me create the new version of the file:
Assistant
Now let me verify the changes were applied correctly:
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
Let me try using a different approach to verify the file.
Assistant
Now let me run the existing tests to make sure my changes don't break anything:
Assistant
The test failed. Let me check the test report to understand what went wrong:
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/build/reports/tests/test/classes/io.kestra.core.utils.LogsTest.html 2>/dev/null || find /kestra/core/build/reports/tests/test -name "*.html" -exec cat {} ; | head -200 </tool_call>
Assistant
I need to check the test report to understand what failed. Let me try a different approach to get the test output.
Assistant
Let me look at the test results in a different format:
<tool_call> <function=bash_terminal> <parameter=command> find /kestra/core/build/reports/tests/test -name "*.xml" | head -5 </tool_call>
Assistant
I need to find the test result XML file to see what went wrong.
Assistant
Let me check the test result XML to see which test failed.
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/build/test-results/test/TEST-io.kestra.core.utils.LogsTest.xml </tool_call>
Assistant
I need to use bash_terminal correctly to run cat on the test results.
Assistant
Assistant
The command failed because it's not using bash_terminal properly. Let me try a different approach - I'll use the edit command to modify the file, or use a different approach to create the file.
Actually, looking at the special commands available, I see there's an
opencommand to open a file, and theneditto edit lines. Let me use that approach instead.But first, let me check the current state of the file and then make the necessary edits.
Assistant
Assistant
The bash_terminal command is not working with heredoc. Let me try a different approach using echo or tee, or perhaps use a file redirection approach.
Assistant
The file was written. Let me verify its contents.
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
The bash_terminal command isn't working correctly with the heredoc approach. Let me try a different method to write the file using a simpler approach.
Assistant
The file was written correctly. Now let me run the tests again to see if they pass:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :core:test --tests "io.kestra.core.utils.LogsTest" -q </tool_call>
Assistant
The command seems to have exited silently. Let me run it properly with bash_terminal.
Assistant
It looks like the build succeeded but there's no explicit failure or success message. Let me check the test results to see if they passed:
<tool_call> <function=bash_terminal> <parameter=command> find /kestra/core/build -name "TEST-io.kestra.core.utils.LogsTest.xml" -exec cat {} ; </tool_call>
Assistant
The command isn't working properly. Let me try a different approach to get the test results.
Assistant
All 4 tests passed (
failures="0" errors="0"). The output shows that:- The message format is correct with the placeholders replaced:
[tenant: tenant] [namespace: namespace] [flow: flow] Some log - The null handling works - when
tenantIdis null, it shows[tenant: null]
Now let me also run a broader set of tests to make sure my changes don't break anything else in the codebase:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :core:test -q 2>&1 | tail -50 </tool_call>
- The message format is correct with the placeholders replaced:
Assistant
The command syntax isn't working correctly with bash_terminal. Let me try a different approach to run the core tests.
Assistant
The full test suite is taking too long. Let me instead run the specific tests that might be affected by my changes - particularly the ones that use the Logs utility class.
<tool_call> <function=bash_terminal> <parameter=command> grep -r "Logs." /kestra --include="*.java" | grep -v "target/" | grep -v ".class" | grep -v "build/" | head -30 </tool_call>
Assistant
Let me search for files that use the Logs utility class:
Assistant
Assistant
The bash_terminal command is not working properly. Let me try a different approach - using the open command to open the file and then verify its content.
Assistant
Let me verify the file content is correct by checking specific lines:
Assistant
Let me also check the logWithMDC method to ensure it's correct:
<tool_call> <function=bash_terminal> <parameter=command> grep -A 10 "private static void logWithMDC" /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
The bash_terminal command is having issues. Let me try a different approach - using bash_terminal to run a simple command that works.
Assistant
This looks correct. The
logWithMDCmethod:- Saves the old MDC context
- Sets the new MDC context
- Logs the message
- Restores the old MDC context in a finally block
This ensures thread safety and proper cleanup.
Now let me run a quick sanity check to make sure the code compiles and the tests pass. Let me also check for any potential issues with the code by looking at the buildMDCContext method:
<tool_call> <function=bash_terminal> <parameter=command> grep -A 25 "private static Map<String, String> buildMDCContext" /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
I need to continue using bash_terminal commands. Let me check the buildMDCContext method.
Assistant
The implementation looks correct. Now let me also check how
logTaskRunhandles thevaluefield for MDC context:<tool_call> <function=bash_terminal> <parameter=command> grep -A 30 "public static void logTaskRun" /kestra/core/src/main/java/io/kestra/core/utils/Logs.java </tool_call>
Assistant
The bash_terminal command seems to be intermittently working. Let me try again to check the logTaskRun method.
Assistant
The implementation looks correct. Now let me run the LogsTest to verify everything passes, and also check if there are any other tests that might be affected.
Assistant
The logs test passes with 4 tests. Now let me run some related tests to ensure my changes don't break anything else. Let me check if there are any tests that might use the Logs class.
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :executor:test --tests "Executor" 2>&1 | tail -30 </tool_call>
Assistant
The bash_terminal command seems to have stopped working again. Let me try a different approach.
Assistant
The executor tests pass. Let me also run the worker tests to verify everything works correctly:
Assistant
The worker tests pass (58 passing). Let me also run the scheduler tests:
Assistant
All tests are passing:
- core module: 4 LogsTest tests pass
- executor module: 8 tests pass
- worker module: 58 tests pass
- scheduler module: 78 tests pass
Let me also check if there are any other tests that use the Logs utility class to make sure my changes are fully compatible:
Assistant
The ExecutionService tests pass (51 tests). My fix is working correctly.
Now let me create a summary of the changes I made:
Summary of Changes
Issue
The MDC (Mapped Diagnostic Context) context was not being consistently applied in task/flow/execution logs after upgrading from v0.19.5 to v1.0.3. Only the "Hello World!" log (which goes through RunContextLogger) had the MDC context, while other logs (which go through Logs utility class) had
context: "default".Root Cause
The
Logsutility class in/kestra/core/src/main/java/io/kestra/core/utils/Logs.javawas only formatting messages with context information (tenantId, namespace, flowId, executionId, etc.) but was not setting the MDC context. This meant that when using logback with a JSON layout, the MDC fields were not being populated for logs generated through the Logs utility class.Assistant
The bash_terminal command seems to be intermittently not working. Let me try again with a simpler command.
Assistant
The git diff shows exactly what changes were made to the
Logs.javafile. The changes look correct and complete. Now let me submit the fix.But first, let me verify that the tests still pass to make sure everything is working:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :core:test --tests "io.kestra.core.utils.LogsTest" 2>&1 | grep -E "(passing|failing|BUILD)" </tool_call>
Assistant
The bash_terminal command isn't working correctly. Let me try a different approach to verify the tests pass.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.