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 start by understanding the issue:
- After upgrading from Kestra v0.19.5 to v1.0.3, MDC (Mapped Diagnostic Context) logging context is no longer consistently applied in task/flow logs.
- In the example, only the "Hello World" log message gets the MDC context (with
taskRunId,tenantId,namespace,executionId,flowId,taskId), while other log messages only have the message text with context info in brackets. - The user found that manually setting
MDC.setContextMap(...)in thelogExecutionmethod inLogService.javafixes the issue.
Let me explore the repository structure and the relevant file.
Assistant
The
findcommand didn't return a file path. Let me search more broadly.Assistant
Let me look at the directory structure.
Assistant
Let me find the LogService.java file.
Assistant
Hmm, the find command finds no file. Let me check if the core directory has the right structure.
Assistant
Let me explore the core source directory.
Assistant
Let me search for LogService more
Assistant
Let me look at
ExecutionLogService.javaand search forlogExecutionmethod.Assistant
Let me look at the
Logs.javafile, which likely contains the relevant logging methods.Assistant
Now I can see the issue. The
Logs.javaclass formats messages with text prefixes like[tenant: {}] ...but never sets these values in the MDC context. Let me check where MDC is used in the codebase.Assistant
Let me look at
RunContextLogger.javato understand how MDC is set for user logs (which explains why the "Hello World" message has MDC contextAssistant
Let me check the
LogEntryclass to understand the MDC keys used.Assistant
Let me look at the
toMapmethod inLogEntry.java.Assistant
Let me check how MDC is used in other files.
Assistant
Now let me look at who calls these
Logsmethods to understand the usage pattern betterAssistant
Let me look at the test file for
Logs:Assistant
Now I have a complete understanding of the issue. Let me summarize:
-
RunContextLogger.javasets MDC context when initializing the logger (vialoggerContext.getMDCAdapter().setContextMap(this.logEntry.toMap())), which is why user log messages (like "Hello World") have MDC context in JSON output. -
Logs.java— the utility class used for system logging (flow start, task run completion, etc.) — only formats messages with text prefixes like[tenant: main] [namespace: company.team] ...but never sets MDC context. -
The JSON layout in
StackdriverJsonLayoutusesevent.getMDCPropertyMap()to include MDC properties, so without MDC being set, the system log messages don't get these structured fields.
The fix: Add MDC context setting in all
Logs.javamethods that perform logging-
Assistant
Let me open the file properly.
Assistant
The open command requires just the file path, not with line number as a separate argument maybe. Let me try:
Assistant
Now I have a clear view of the file. Let me implement the fix. I need to:
- Add
import org.slf4j.MDCandimport java.util.Map - Add MDC context setting in each logging method using try/finally to restore previous MDC state
Let me start with the imports
- Add
Assistant
Let me use the edit command correctly
Assistant
Good. Now let me fix the import order (java.util.Map should come before io.kestra imports) and then modify each method. Let me fix the import order first
Assistant
Now let me modify each of the 4 methods that do the actual logging. First,
logExecution(FlowId flow, Logger logger, ...)Assistant
Now update the second method - `logExecution(Execution execution, Logger logger, ...
Assistant
Now update the trigger method
Assistant
Now update the last method -
logTaskRun:Assistant
Let me verify the full file looks
Assistant
Now let me fix the minor import formatting (add blank line between java.util and io.kestra imports) and then run tests.
Assistant
I see there's a duplicate import. Let me fix the imports
Assistant
Good. Now let me run the existing tests to make sure everything compiles and passes.
Assistant
All 4 tests pass. Let me also check the
ExecutionLogService.javato see if it needs similar changesAssistant
Now let me run the broader set of tests related to the changes
Assistant
Let me run the full core module tests to be
Assistant
The full test suite timed out. Let me just verify compilation and run the specific tests that are most
Assistant
Code compiles successfully. Let me view the final diff of my
Assistant
The diff looks perfect. Let me now verify that the broader project compiles by also compiling the modules that depend on `Logs.java
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.