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 not being applied consistently in logs.
Assistant
Let me look at the LogService.java file mentioned in the issue:
Assistant
The exit code was 0 but no output was shown, which is strange. Let me try a different approach.
Assistant
The file might not exist with that exact name. Let me look for files containing "LogService" in their content or search more broadly.
Assistant
Let me search for LogService in the core module.
Assistant
The issue description mentions
LogService.javaat a specific URL pointing to line 45. Let me search for that file or thelogExecutionmethod mentioned.Assistant
Based on the issue description, the user mentions that in the
LogService.javafile at line 45, there's alogExecutionmethod. They manually added MDC context map setting to fix the issue. Let me look at theLogs.javafile in the utils folder, as this seems to be related to logging functionality.Assistant
Let me use the bash_terminal command to view the file instead.
Assistant
Now I understand the structure better. Let me look at how tasks emit their logs. The issue mentions that the task's own message ("Hello World!") doesn't get the MDC context. Let me explore the logging mechanism further:
Assistant
Now let me explore the task runner and how logs are emitted:
Assistant
Let me look at the
DefaultRunContext.javato understand when MDC is being reset:Assistant
Let me look at how the worker processes tasks and logs:
Assistant
Let me look at the WorkerJobProcessorFactory to understand how tasks are processed and how logs are set up.
Assistant
Now I see the issue. Looking at the code flow:
- When a task runs, the task uses
runContext.logger()to log messages (like "Hello World!") - The
LogService.logExecutionmethod (inLogs.java) logs execution-level messages with formatted context information - The problem is that these are two separate logging mechanisms:
Logs.logTaskRun()uses the server-side logger with formatted message prefixesRunContextLoggercreates a separate logger context with MDC set fromLogEntry.toMap()
The issue is that when a task (like the Log task) logs its message, it uses its own logger which is separate from the execution/task loggers. The MDC context needs to be properly propagated.
Let me look at the
RunContextLoggerFactoryand how theLogEntryis created and used:- When a task runs, the task uses
Assistant
Now let me look at the
LogEntrymodel to see whattoMap()returns:Assistant
Let me look at the Log task implementation to understand how it logs messages:
Assistant
Let me look for the Log task that would output "Hello World! 🚀":
Assistant
Let me look at the Log task implementation to understand how it logs messages.
Assistant
Assistant
Let me try that grep command again more carefully.
Assistant
Now let me understand the issue better. I see the problem - the
Logsutility class logs execution/task/trigger messages but doesn't set the MDC context, while task-internal logs (from theRunContextLogger) do set the MDC context viaLogEntry.toMap().Let me check how the MDC context flows through the system by looking at the
RunContextLogger.initializeLogger()method again:Assistant
Based on my analysis, I now understand the issue. When task-internal logs (via
runContext.logger()) are processed, the MDC context is properly set on the logger context's MDC adapter viaLogEntry.toMap(). However, the server-side logs (via theLogsutility class for execution/task/trigger events) don't set any MDC context, so they don't get the MDC fields in the JSON output.Let me verify my understanding by examining the logging flow more closely:
Assistant
Based on my analysis, I now understand the issue. The
Logsutility class (io.kestra.core.utils.Logs) logs execution/task/trigger messages using standard SLF4J, but it doesn't set the MDC context. This means that when the logs are processed by logback, the MDC fields (tenantId, namespace, flowId, executionId) are not available for JSON serialization.In contrast, task-internal logs (via
RunContextLogger) properly set the MDC context usingLogEntry.toMap(). The fix should make theLogsutility class also set the MDC context before logging.Let me first create a test to reproduce the issue:
Assistant
Now I have a clear understanding of the issue. Let me implement the fix:
The problem is in the
Logs.javautility class. When it logs execution/task/trigger messages, it doesn't set the MDC context, so the MDC fields (tenantId, namespace, flowId, executionId, etc.) are not available for JSON serialization by logback.The fix should set the MDC context before logging and clear it after logging.
Let me update the
Logs.javafile:Assistant
Now I understand the issue. The
Logsutility class doesn't set the MDC context before logging, so the MDC fields (tenantId, namespace, flowId, executionId) are not available for JSON serialization by logback.The fix should set the MDC context before logging and clear it after logging. I need to modify the
Logs.javafile to set the MDC context usingMDC.setContextMap()before logging and clear it after logging.Assistant
Now let me apply this fix to the actual file:
Assistant
Let me verify the changes were applied correctly:
Assistant
I apologize for the confusion. Let me check the current state and ensure the import is present as required.
Assistant
The
org.slf4j.MDCimport is present. Now let me run the existing tests to ensure they pass:Assistant
All 4 tests passed. Let me verify the git diff to see what changes were made:
Assistant
The changes look good. Let me verify the fix is complete by checking if there are any other related test files or integration tests I should run:
Assistant
Let me run more comprehensive tests to ensure the fix works correctly:
Assistant
The user wants me to run more comprehensive tests to ensure my fix works correctly. Let me run the tests for the modules that use the Logs utility class (executor, worker, scheduler).
Assistant
All worker tests passed. Now let me run the scheduler and executor tests:
Assistant
All tests have passed. Let me verify the final state of the changes:
Assistant
I have successfully implemented the fix for the MDC context issue. Let me summarize what was done:
Summary
Issue: After upgrading to Kestra v1.0, MDC context fields (tenantId, namespace, flowId, executionId) were not being consistently included in task/flow logs when using JSON log formatters like logback.
Root Cause: The
Logsutility class (io.kestra.core.utils.Logs) used by the executor, scheduler, and worker to log execution/task/trigger events was not setting the MDC context before logging. While task-internal logs (viaRunContextLogger) properly set MDC context viaLogEntry.toMap(), the server-side logs from theLogsutility class didn't.Fix: Modified the
Logs.javafile to set the MDC context before logging and clear it after logging using try-finally blocks. The fix:- Adds
org.slf4j.MDCimport
- Adds
Assistant
The fix looks good and all tests pass. Let me verify the complete solution one more time:
Assistant
The user wants me to submit my changes. I've already implemented the fix for the MDC context issue and verified that all tests pass. Let me submit the changes now.
Assistant
The submit function needs to be called correctly. Let me check the syntax again.
Assistant
The user is indicating that I should submit my changes since the fix is complete. I've already implemented the fix for the MDC context issue in Logs.java and all tests pass. I need to call the submit function properly.
Assistant
The user is telling me I should submit my changes. Let me call the submit function again - it seems like there was an issue with the function call format. I need to just call submit without any arguments.
Assistant
The user wants me to submit my changes. I've already implemented the fix for the MDC context issue in Logs.java and all tests pass. I need to call the submit function properly. Looking at the tool definitions, I see that submit is a special command that doesn't require parameters. Let me try calling it correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.