MDC context no longer applied consistently in task/flow logs after upgrade to v1.0…
- User
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
Now implementing the fix in
Logs.java: - Assistant
Now run the tests:
- Assistant
Waiting on the Gradle test run to complete.
- Assistant
Test file looks good. Now I'll wait for the Gradle run to finish.
- Assistant
The build is compiling (Gradle output is buffered). I'll wait for the background task to complete — it will notify when the test run finishes.
Summary of the fix so far:
- Root cause: After the v1.0 refactoring, server logs for executions/tasks/triggers (e.g. "Flow started", "Type Log ... completed") are emitted through the
Logsutility (core/src/main/java/io/kestra/core/utils/Logs.java), which used plain SLF4J loggers without populating the MDC. Only real flow logs go throughRunContextLogger, which sets the MDC on its own logger context (RunContextLogger.java:202) — which is why only "Hello World! 🚀" carriedtenantId/namespace/flowId/… fields in the JSON output. - Fix:
Logsnow sets the MDC (same keys asLogEntry.toMap():tenantId,namespace,flowId,executionId,taskId,taskRunId,triggerId) around each logging call, and restores the previous MDC state in afinallyblock so the context never leaks to unrelated logs on the same executor/worker/scheduler thread. Null values are skipped so never gets null entries.
- Root cause: After the v1.0 refactoring, server logs for executions/tasks/triggers (e.g. "Flow started", "Type Log ... completed") are emitted through the
- Assistant
I'll wait for the notification before verifying the results.
- Assistant
The test compiled and ran — results are on disk:
- Assistant
All 5 tests passed:
tests="5" failures="0" errors="0"(the Gradle process is still tearing down its build lifecycle, but the test results are final on disk).Done
Root cause: Since the v1.0 logging refactor, server logs about executions, task runs, and triggers ("Flow started", "Type Log … completed", …) are emitted through the
Logsutility class using plain SLF4J loggers — without populating the MDC. Only actual flow logs (e.g. "Hello World! 🚀") go throughRunContextLogger, which sets the MDC on its private logger context (). Since JSON layouts like emit as top-level fields, only the flow logs carried / / / / etc. - Assistant
The Gradle run completed with BUILD SUCCESSFUL (exit code 0) — the long runtime was compilation plus the Jacoco report, not the tests themselves. This confirms the earlier result: all 5
LogsTesttests pass, 0 failures.The fix is complete:
core/src/main/java/io/kestra/core/utils/Logs.java— server logs for executions, task runs, and triggers now set the MDC context (tenantId,namespace,flowId,executionId,taskId,taskRunId,triggerId— same keys asLogEntry.toMap(), matching what flow logs already emit) and restore the previous MDC state after logging so nothing leaks between log calls on shared executor/worker/scheduler threads.core/src/test/java/io/kestra/core/utils/LogsTest.java— tests assert the MDC content per log type, null-tenant handling, and MDC restoration.
With this change, JSON layouts that include the MDC (like the
CONSOLE_JSON_OUTStackdriver layout from the issue) will again emit the execution context fields on "Flow started" / "Flow completed" / task lifecycle logs, restoring the v0.19.5 monitoring behavior.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.