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<?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:
json{"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"} Assistant
Let me understand the issue:
- After upgrading from kestra v0.19.5 to v1.0.3, MDC (Mapped Diagnostic Context) context is no longer applied consistently in task/flow logs
- The user expects MDC context to be set on all log messages (including task/flow logs), but only some logs have the context
- The user has a workaround where they manually set the MDC context using
MDC.setContextMap()in the LogService.java file
Let me start by exploring the repository structure and understanding the codebase.
Assistant
Let me explore the LogService.java file mentioned in the issue.
Assistant
There is no
LogService.javain the services directory. Let me search for classes that might contain thelogExecutionmethod or similar.<tool_call> <function=bash_terminal> <parameter=command> find /kestra -type f -name "*.java" -exec grep -l "logExecution" {} ; 2>/dev/null </tool_call>
Assistant
The previous command had an issue. Let me run the search differently.
Assistant
Now let me look at the Logs.java file that seems to be the key one mentioned in the issue.
Assistant
The open command requires a different syntax. Let me use the correct command.
Assistant
Now I can see the Logs.java file. The issue is that when logging execution and task run information, the MDC (Mapped Diagnostic Context) is not being set on the logger, which means logback/log4j cannot automatically inject the MDC context into the log messages.
In the issue, the user mentions that in v0.19.5, the MDC context was automatically applied, but in v1.0 it's not. The user's workaround is to manually set the MDC context using
MDC.setContextMap().The problem is that the current implementation doesn't set the MDC context before logging. Let me check if there are any tests for this file.
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/src/test/java/io/kestra/core/utils/LogsTest.java </tool_call>
Assistant
I need to use bash_terminal command with proper syntax to read the file.
Assistant
Now let me understand the issue better. The problem is that the MDC context is not being set when logging execution and task run logs. Looking at the example from the issue:
code{"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! 🚀"}This log doesn't have MDC context like
[tenant: main] [namespace: company.team]...in the message, but it has the fields in the JSON directly.Looking at the Logs.java file, the issue is that the methods don't set the MDC context before logging. To fix this, I need to add MDC context setting before the log calls.
Let me check how the user's workaround was done and implement it properly. The fix should:
- Set MDC context before logging
Assistant
Let me run a proper command to search for MDC usage in the test files.
Assistant
No results for MDC in test files. Let me search for MDC in the entire codebase.
Assistant
Let me look at the RunContextLogger to see how MDC is handled.
Assistant
Now I understand the issue better. The RunContextLogger sets the MDC context in
initializeLogger()method, and then logs are emitted via its appenders. However, when the Logs.java class is used to log execution and task run events directly, it doesn't use RunContextLogger, it uses the standard LoggerFactory.getLogger().The problem is that when using standard Logger from SLF4J, the MDC context needs to be set explicitly using
MDC.setContextMap()before logging. Looking at the user's issue, they mention that the MDC context should be set to allow logback to include the context in the log output.Let me check the current Logs.java implementation and fix it by adding MDC context setting.
<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 should be used to execute commands, not to read files using cat. Let me use the proper command syntax.
Assistant
Assistant
The command I tried didn't work properly. Let me use a different approach to fix the Logs.java file - I'll use the edit command to make the changes.
Assistant
The file was copied. Let me verify the content was written correctly.
<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 is for executing shell commands. Let me try using the correct syntax to read the file.
Assistant
Now let me verify the changes work by running the existing tests.
Assistant
The project name is different. Let me find the correct project name.
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew projects 2>&1 | grep -i core </tool_call>
Assistant
The previous command was executed. Let me run a proper bash_terminal command to check the gradle projects.
Assistant
The project name is
:core, not:kestra-core. Let me run the tests with the correct project name.Assistant
There are compilation errors. Let me see the full error message.
Assistant
I need to add the import for
Map. Let me fix the Logs.java file.Assistant
Let me verify the file and run the tests again.
Assistant
Good, the import is added. Let me run the tests now.
Assistant
The build succeeded (no compilation errors) but there are failing tests. Let me check what tests failed.
<tool_call> <function=bash_terminal> <parameter=command> cat /kestra/core/build/reports/tests/test/index.html 2>/dev/null | grep -A 5 "failed" </tool_call>
Assistant
I need to use bash_terminal to run a command. Let me check the test output more carefully.
Assistant
No results. Let me check the test output differently.
Assistant
Let me check the actual test failure details in the subdirectory.
Assistant
No output. Let me look at the test output in a different format.
Assistant
Let me see the full failure details.
<tool_call> <function=bash_terminal> <parameter=command> grep -A 30 "logTaskRun" /kestra/core/build/test-results/test/TEST-io.kestra.core.utils.LogsTest.xml | head -35 </tool_call>
Assistant
The previous command failed with "The command exited with a non-zero exit code (1)". I need to run a bash command properly using bash_terminal.
Assistant
I can see the issue. The
logTaskRun()test fails because it creates a TaskRun without atenantId, andMap.of()doesn't allow null values. I need to handle null values in the MDC context properly.Let me fix the Logs.java file to handle null values:
Assistant
Now let me run the tests again to see if they pass.
Assistant
All 4 tests are passing now. Let me verify the fix by looking at the full Logs.java file to ensure it's correct.
<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 run a bash command properly.
Assistant
The fix looks good. Now let me run more tests to make sure I haven't broken anything else. Let me run all tests related to logging to make sure they pass.
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :core:test --tests "Log" 2>&1 | tail -50 </tool_call>
Assistant
The previous command seems to have failed. Let me try running a different test command to verify the logging tests pass.
Assistant
All 37 tests passed. Let me also run tests related to execution and task logs to make sure the changes work correctly throughout the codebase.
Assistant
All 10 tests passed. Now let me also check if there are any other tests that use the Logs class to make sure the fix doesn't break anything.
Assistant
All tests in the executor module passed. Let me also run tests in the scheduler module.
Assistant
All 78 tests passed in the scheduler module. Let me also run tests in the worker module.
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && ./gradlew :worker:test 2>&1 | tail -20 </tool_call>
Assistant
The previous command failed. I need to run the proper bash command again.
Assistant
All 58 tests in the worker module passed. Now let me verify the fix is complete and submit it. Let me first create a summary of what was done:
Summary of Changes
The issue was that MDC (Mapped Diagnostic Context) context was no longer applied consistently in task/flow logs after upgrading from kestra v0.19.5 to v1.0. The user expected MDC context (tenantId, namespace, flowId, executionId, taskId, taskRunId) to be automatically available in log messages.
Assistant
The bash_terminal command failed. I need to use the correct syntax to run a git diff command.
Assistant
The diff shows the changes I made. Let me verify the changes are correct and submit them.
Assistant
The changes look correct. Let me now submit the solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.