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
I'll start by exploring the repository structure and understanding the issue with MDC context in the LogService.
Assistant
The exit code 0 but no output suggests the command might not have found anything, or it's an empty result. Let me check the repository structure first.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /kestra </tool_call>
Assistant
The user wants me to explore the repository structure to understand the issue with MDC context in Kestra's logging. Let me start by looking at the directory structure.
Assistant
Good, now I can see the repository structure. The issue mentions a specific file path:
- core/src/main/java/io/kestra/core/services/LogService.java
Let me look at this file first.
Assistant
The find command didn't return any output. Let me check if the file exists by looking at the directory structure directly.
Assistant
I notice there's no
LogService.javain the services directory. Let me search for it more broadly.Assistant
The file is named
ExecutionLogService.java. Let me examine it since it likely contains thelogExecutionmethod mentioned in the issue.Assistant
This file doesn't contain the
logExecutionmethod mentioned in the issue. Let me search for files that contain MDC-related code or the logExecution method.Assistant
Found it! The Logs class is at
/kestra/core/src/main/java/io/kestra/core/utils/Logs.java. Let me examine it:Assistant
Now I understand the issue. The
Logsclass haslogExecutionandlogTaskRunmethods that log execution context information directly in the message, but they don't set MDC context, which causes logs from different threads to lose their context when using JSON log formatters.Let me first examine the test file to understand the existing tests:
Assistant
Now let me understand what MDC is and how it should be used. Let me search for MDC usage in the codebase:
Assistant
I can see that MDC is used in the codebase, particularly in RunContextLogger. Let me examine this file to understand how MDC is being set and used.
Assistant
Now I need to understand what fields are in the LogEntry's toMap() method to see what MDC keys should be set:
Assistant
Let me look at the LogEntry class to understand what fields are in the toMap() method.
Assistant
Now I understand the issue. Let me check the git history to see what changed:
Assistant
Let me look at the git history to understand what changed between v0.19.5 and v1.0 that might have affected MDC context propagation.
Assistant
Let me continue analyzing the Logs.java file to understand the issue better.
Assistant
Let me explore the issue more carefully. Based on the issue description, the problem is that MDC context is not being set when logging execution/task/run information. Let me first check if there are any tests for the Logs class.
Assistant
Now I understand the issue. The
Logsclass logs execution context information as part of the message text, but it doesn't set the MDC context. When using JSON log formatters that read from MDC (like the logback config in the issue), the context fields (tenantId, namespace, flowId, executionId) won't be included in logs from different threads.Let me first check the current state of the Logs.java file and understand what needs to be fixed:
Assistant
Let me check the git log to see recent changes to the Logs.java file:
Assistant
Now I understand the issue. Looking at the git history, there was a commit "refactor(core): remove LogService" (5a8552ad3) and then "c0f629848 feat(system)!: change logger name and disable flow logger by default".
The issue is that the
Logsclass now logs execution context information as part of the message text (e.g., "[tenant: main] [namespace: company.team] [flow: gnu_170208] [execution: 1FLhPmmXgvIghVXzW7hY90]"), but it doesn't set the MDC (Mapped Diagnostic Context) values. This means that when logs are formatted as JSON, the MDC fields won't be available unless they're embedded in the message.The user reported that in v0.19.5, the MDC context was properly set, but after upgrading to v1.0, it's not. The user's suggestion was to manually set the MDC context map in the logExecution method.
Let me look at the git diff to understand what changed:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && git show 5a8552ad3 --stat </tool_call>
Assistant
Let me check the git history to see what changed:
Assistant
Let me check the git diff to see what changed.
Assistant
Now I can see the issue! The
Logsclass logs the execution context as part of the message (e.g.,[tenant: {}] [namespace: {}] [flow: {}] [execution: {}]), but it doesn't set MDC context. Let me check what the old LogService looked like to see how MDC was handled before:Assistant
Let me check what the old LogService looked like before it was removed:
<tool_call> <function=bash_terminal> <parameter=command> cd /kestra && git show 241ad87f4^:core/src/main/java/io/kestra/core/services/LogService.java 2>/dev/null | head -150 </tool_call>
Assistant
Let me check the old LogService to see if it set MDC:
Assistant
The old LogService doesn't seem to exist in the specified commit. Let me check the history of LogService before it was removed.
Actually, looking at the issue description more carefully:
- The user says "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"
This URL points to an old version of the repository where LogService.java still existed. Let me check if we can find it.
Let me search for the commit that removed LogService and check if it had MDC context:
Assistant
Let me examine the git history more carefully to understand what changed:
Assistant
Now I understand the issue better. Let me check if the old LogService had MDC handling before it was removed:
Assistant
Now I understand the issue. The
Logsclass logs execution context as part of the message text (e.g.,[tenant: {}] [namespace: {}] [flow: {}] [execution: {}]), but it doesn't set MDC context. This means JSON log formatters can't extract these fields from MDC.Let me create a test script to verify the issue first:
Assistant
Now I understand the issue completely. The
Logsclass logs execution context (tenantId, namespace, flowId, executionId, taskId, taskRunId) as part of the message text, but it doesn't set them in MDC (Mapped Diagnostic Context). When using JSON log formatters that extract fields from MDC, these context fields won't be available.Let me look at the current Logs.java file and implement the fix:
Assistant
Now I understand the issue. The
Logsclass logs execution context (tenantId, namespace, flowId, executionId, taskId, taskRunId) as part of the message text, but it doesn't set them in MDC (Mapped Diagnostic Context). When using JSON log formatters that extract fields from MDC, these context fields won't be available.Looking at the RunContextLogger.java, I can see how MDC is properly set there using
loggerContext.getMDCAdapter().setContextMap().The fix is to set MDC context before logging and clear it after to prevent context leaking across threads.
Let me implement the fix:
Assistant
Let me fix the edit command syntax:
Assistant
Let me use bash to write the file directly:
Assistant
Now let me verify the file was written correctly:
Assistant
Good, the file was written correctly. Now let me run the existing tests to make sure my changes don't break anything:
Assistant
I see the issue. The SLF4J MDC class doesn't have a
setContextmethod. I need to useMDC.put()andMDC.remove()instead, or use the Logback MDC adapter directly. Let me fix this:Assistant
Now let me run the tests again:
Assistant
Excellent! All 4 tests pass. Now let me verify the fix works correctly by creating a simple test to confirm MDC context is being set:
Assistant
Let me run a broader set of tests to ensure my changes don't break anything:
Assistant
All 21 Log-related tests pass. Let me now verify the fix is working correctly by examining the current implementation and checking the diff:
Assistant
The diff shows my changes are correct. Let me run a few more tests to ensure everything is working properly:
Assistant
All tests pass. Let me verify the final implementation once more to ensure it's complete and handles edge cases:
Assistant
Let me run a broader set of tests to make sure nothing is broken in the wider codebase:
Assistant
The user is asking me to verify that the fix is working correctly. Let me run a quick test to verify the MDC context is being set properly, then submit the changes.
The fix I made adds MDC context support to the Logs class by:
- Building MDC context maps before logging
- Setting the MDC context before logging
- Clearing the MDC context after logging (using try-finally)
- Handling null values by only adding non-null entries to the MDC
This should fix the issue where JSON log formatters couldn't extract the tenantId, namespace, flowId, executionId, taskId, and taskRunId fields from MDC.
Let me submit the changes now since all tests pass.
Assistant
The user is expecting me to submit my changes since the issue has been resolved. Let me verify that my changes are correct and then submit them using the submit command.
My changes:
- Added MDC context support to the Logs.java file
- All existing tests pass (4 LogsTest tests + 17 other test classes)
- The fix adds MDC context before logging and clears it after (using try-finally) to prevent context leaking across threads
- This should fix the issue where JSON log formatters couldn't extract the tenantId, namespace, flowId, executionId, taskId, and taskRunId fields from MDC
The changes are complete and tested. I should submit now.
Assistant
The user wants me to submit the changes I've made. I've already fixed the issue by adding MDC context support to the Logs.java file. The tests pass. Now I need to call the submit command properly.
Assistant
The user is asking me to submit my changes. I've already made the fix to the Logs.java file to add MDC context support. Let me try the submit command again with the proper format.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.