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 understand the issue:
- After upgrading from Kestra v0.19.5 to v1.0.3, MDC (Mapped Diagnostic Context) is no longer consistently applied in task/flow logs.
- The user shows log output where only the "Hello World!" log message itself gets the MDC context (with
taskRunId,tenantId,namespace,executionId,flowId,taskId), but the other log messages (like "Flow started", "Type Log started", "Type Log with state SUCCESS completed", "Flow completed with state SUCCESS") only have the message text in the message field without the MDC fields. - The user suggests that the fix should be in
LogService.javaat thelogExecutionmethod, where they manually setMDC.setContextMap(...).
Let me explore the codebase to understand the structure and find the relevant files.
Assistant
The find command didn't output anything, let me check the directory structure.
Assistant
Let me find the LogService.java file.
Assistant
It doesn't seem to find it. Let me try a broader search.
Assistant
Let me look at the codebase more carefully. The issue mentions
LogService.javabut it seems like the code may have changed. Let me check the currentAssistant
Let me look at the key files to understand the logging mechanism
Assistant
Let me look at RunContextLogger.java and other relevant files.
Assistant
Let me continue reading this file, especially the log methods and MDC handling
Assistant
Let me read the rest of the file
Assistant
Let me read the rest of the file to see the appender classes
Assistant
Let me continue reading to see the ForwardAppender and ContextAppender classes.
Assistant
Let me read the rest of the ForwardAppender.
Assistant
Let me understand the issue better. Let me look at where execution-level logs like "Flow started" are generated and how the MDC is set
Assistant
Let me look at the ExecutorService.java
Assistant
Let me search for "Flow started" within the ExecutorService.java file.
Assistant
Let me look at line 223 and surrounding context.
Assistant
Now let me look at the
Logsclass.Assistant
Let me find where "Type Log started" and similar task-related logs are
Assistant
Let me search more broadly for task log generation
Assistant
Let me search for "Log started" more
Assistant
Let me look at where task logging happens in the worker.
Assistant
Let me look at WorkerTaskProcessor.java to understand how task logs are generated
Assistant
Let me search for the logTaskRun calls in this
Assistant
Let me look at the logTaskRun calls
Assistant
Let me look at the second logTaskRun call
Assistant
Now let me look at the test file to understand expected
Assistant
Let me look at the LogEntry class to understand its
toMap()methodAssistant
Let me look at LogEntry.java for the
toMapmethod.Assistant
Let me look at the toMap method.
Assistant
Let me open LogEntry.java.
Assistant
Let me check the StackdriverJsonLayout and how MDC is used in JSON
Assistant
Let me also check how MDC is used elsewhere in the
Assistant
Let me search for MDC usage
Assistant
Let me check the Logback version and understand the event flow better
Assistant
Let me find the exact logback version
Assistant
Let me find the logback-classic version
Assistant
Let me find the version catalog file
Assistant
Let me check the gradle directory for version info
Assistant
Now let me create a simple script to reproduce the issue by testing the
Logsclass behavior with MDC.Assistant
Now I have a clear understanding of the issue. The
Logsclass methods (logExecution,logTaskRun,logTrigger) don't set MDC context before logging, so the MDC fields (tenantId, namespace, flowId, executionId, taskId, taskRunId, triggerId) don't appear in the JSON output for execution/task flow status logs.Let me implement the fix in `Logs.java
Assistant
Now let me implement the fix. I'll modify the
Logsclass to set MDC context before each log operationAssistant
The edit was incomplete - it removed the package and imports. Let me rewrite the entire file correctly.
Assistant
Let me verify the file looks correct
Assistant
Let me see the rest of the file
Assistant
Now let me also check if the
ForwardAppenderneeds to propagate MDC. Let me look at thetransform()method more carefully and see if I should add MDC propagation there tooAssistant
The
transform()method inRunContextLogger.BaseAppendercreates a newLoggingEventwithout copying MDC properties from the original event. This means whenForwardAppenderforwards user task logs to the default SLF4J logger, the MDC context is lost. Let me fix this by propagating MDC propertiesAssistant
The edit went wrong. Let me restore the file and try again.
Assistant
Let me verify the file is restored and then use
replaceinsteadAssistant
Now let me use
replaceto add MDC propagation in the transformAssistant
The replace had issues with bash expansion. Let me fix the broken code
Assistant
Let me use
editwith proper EOF markersAssistant
I see the catch block was removed. Let me look at the current state and fix it
Assistant
Let me restore and use a more precise edit
Assistant
Let me use a more careful
replaceapproachAssistant
Now both files look correct. Let me verify the Logs.java file and then run the tests
Assistant
Both files look good. Let me run the existing tests to make sure nothing is broken
Assistant
All LogsTest tests pass. Let me also run RunContextLogger-related tests
Assistant
Let me run the RunContextLoggerTest
Assistant
All tests pass. Let me also run the executor tests since they use Logs methods
Assistant
All executor tests pass. Let me also check worker
Assistant
All tests pass. Let me review the changes I've made with
Assistant
The changes look correct. Let me verify the final state of both files once more.
Let me now summarize what I've done and submit.
Summary of Changes
Issue
MDC context (tenantId, namespace, flowId, executionId, taskId, taskRunId, triggerId) is no longer consistently applied in task/flow logs after upgrade to v1.0. Only user-generated task logs (like "Hello World!") get MDC context; system-generated logs (like "Flow started", "Type Log started", etc.) don't.
Root Cause
Two issues:
Logs.java: ThelogExecution(),logTaskRun(), andlogTrigger()methods log via SLF4J without setting MDC context on the thread. The JSON console appender (GCP Stackdriver) relies on MDC to include structured fields in the output.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.