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
I’ll inspect
LogService, its call sites, MDC utilities, and existing tests to identify the regression and the expected scoping/cleanup behavior before changing code. - Assistant
The linked path does not match this checkout’s module layout, so I’ll locate the class and inspect the repository structure first.
- Assistant
ripgrepis unavailable, and this branch containsExecutionLogServicerather thanLogService. I’ll usegrepto inspect that implementation, all related logging services, and MDC handling. - Assistant
The logging methods were moved to
io.kestra.core.utils.Logs; I’ll inspect that class, its tests, structured appenders, and the removal commit to understand intended compatibility and avoid leaking MDC between pooled threads. - Assistant
The utility currently only embeds identifiers in the message; it never populates the thread MDC. A safe fix should enrich every server-log variant (flow, execution, trigger, task run), omit null identifiers, and restore the caller’s exact MDC in
finallyso pooled threads and nested contexts are not contaminated. I’ll verify canonical field names fromLogEntryand inspect dependencies/API conventions before implementing a scoped helper. - Assistant
The canonical keys are camel-cased (
taskRunId, etc.). I’ll now implement a small private scoped-MDC function inLogs, wrapping each actual logger call. It will merge unrelated caller MDC entries, explicitly replace/remove Kestra identifier keys (preventing stale IDs when nullable), and restore or clear the original map afterward. - Assistant
History contains prior MDC-related fixes, so before editing I’ll inspect those patches; they may reveal why global
MDCstopped working and whether logging against custom logger contexts requires a different adapter. - Assistant
Prior history confirms custom run-context logs use their own logger-context MDC, while these lifecycle logs use the root SLF4J context. I’ll apply the scoped root-MDC enrichment to all four utility paths, using the same identifier schema as
LogEntry. - Assistant
Python is not installed, so no file was changed. I’ll rewrite this small class directly with the intended implementation, preserving its existing logger names and message formatting.
- Assistant
The source now enriches and safely restores MDC. I’ll run the focused existing
LogsTest; this also compiles the changed class under the project’s configured Java/style toolchain. - Assistant
The focused suite passes. Because existing tests only count events and do not assert MDC, I’ll reproduce the reported behavior independently with a temporary (outside-repository) Java harness. It will capture Logback event MDC for execution and task-run lifecycle logs and verify restoration of a pre-existing caller context, including nullable identifiers.
- Assistant
The temporary harness did not run because the external Gradle init script addressed the project through the wrong API, leaving an empty classpath; this did not affect repository code. I’ll correct the script to use
rootProject.project(':core')and rerun the harness. - Assistant
The independent reproduction now confirms both reported event types carry structured MDC and that nullable IDs are omitted rather than causing errors or inheriting stale values; the original caller MDC is restored afterward. I’ll run the project’s formatting/static check for the changed module, then review the final diff and repository status.
- Assistant
The module-wide Spotless check is blocked by 196 pre-existing formatting violations in unrelated files. I’ll determine whether
Logs.javaitself appears in the violation report and use Spotless’s per-file target option if supported, without applying changes across the repository. - Assistant
The first targeted attempt was skipped because Spotless requires an absolute hook path. I’ll rerun it correctly against only the changed source file.
- Assistant
The changed file is clean under the formatter. I’ll perform a final status/diff review, ensure no build artifacts or temporary files entered the repository, and verify that only the intended source file changed.
- Assistant
The final review shows exactly one intended source file changed. To satisfy the reproduction checklist rigorously, I’ll temporarily compile the unmodified
Logsimplementation and run the same external harness, confirm it fails because event MDC lacks the execution fields, then immediately restore/recompile the fixed source. No test or additional repository file will be changed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.