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 inspect the logging service, its call sites, and existing tests first. The likely fix must scope MDC values safely (including null tenant IDs) and restore the previous thread context rather than leaking execution metadata across reused worker threads.
- Assistant
The linked path has moved in this checkout, so I’ll locate
LogServiceand identify the module layout before examining implementation and tests. - Assistant
rgis unavailable and the initialfindfound no matching filename, suggesting the service may have been renamed or split. I’ll usegrepand inspect source trees for the public logging methods and MDC usage. - Assistant
The service is now
io.kestra.core.utils.Logs, with a focusedLogsTest. I’ll inspect both plus model MDC helpers to understand intended field names and whether task-run logging already has the correct scoping pattern we can reuse. - Assistant
Logscurrently prefixes all server messages but sets no MDC, while task plugin output gets MDC throughRunContextLogger; this exactly explains the reported JSON difference. I’ll inspect that logger’s context map and project history/dependencies to preserve established MDC key conventions and choose an SLF4J-compatible scoped API. - Assistant
The task logger’s MDC keys come from
LogEntry.toMap(). I’ll compare that exact map and the pre-refactorLogServicehistory; this should reveal whether the regression was introduced when the utility replaced the injectable service and what coverage is expected for flow, execution, trigger, and task-run logs. - Assistant
The canonical keys are
tenantId,namespace,flowId,taskId,executionId,taskRunId, andtriggerId, with nulls omitted. The regression affects everyLogsentry point, not only execution logs. Before implementing, I’ll search all refs for an existing upstream fix and inspect SLF4J’s available MDC API/version so the implementation is both consistent and leak-free. - Assistant
No upstream fix is present in local refs. I’ll implement a small internal scoped MDC helper that merges metadata into any existing context and restores it in
finally. Merging is important: replacing the entire map would discard unrelated application MDC such as tracing IDs; restoration prevents metadata leaking between executions on pooled threads. Each log method will build context throughLogEntryfactories where available or explicit null-safe maps. - Assistant
I’ll now update
Logsonly (no test modifications), applying MDC at the point each event is emitted. The helper will omit null values (notably optional tenant IDs), overlay Kestra keys on existing MDC for the event, and restore the exact prior context even if logging throws. - Assistant
Python is not installed, so no change was made. I’ll apply the same focused patch with the standard
patchutility and then inspect the resulting diff. - Assistant
apply_patchis also unavailable in this minimal image. I’ll rewrite the single small source file via a here-document, preserving all existing behavior except the MDC wrapping, then run formatting checks and compilation. - Assistant
The diff is clean and covers all four logging paths. I’ll run the focused
LogsTestnow; its in-memory Logback events reproduce the relevant emission path and will also expose compilation/style issues. - Assistant
The focused suite passes. Because existing assertions only count events and logger names, I’ll additionally reproduce the reported condition with a temporary (outside-repository) Java harness: capture a Logback event, assert its MDC contains flow metadata, assert a pre-existing tracing key is retained, and assert the caller’s MDC is restored afterward. Then I’ll run the repository’s formatting check.
- Assistant
The temporary reproduction succeeded: the emitted event contained
namespace,flowId, and the pre-existingtraceId, omitted the null tenant, and restored the caller context. The module-wide Spotless check is blocked by 195 pre-existing formatting violations unrelated to this change; the output did not flagLogs.java. I’ll now inspect repository status and run the narrower compile plus diff sanity checks, ensuring no generated or temporary files are included. - Assistant
Compilation and diff checks pass, and only
Logs.javais modified. I’ll make one final targeted formatting diagnosis by capturing the complete Spotless report and checking whether this file appears; this distinguishes repository baseline failures from any violation introduced here.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.