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
I’ll trace the current logging/MDC lifecycle and its tests first. Success means task and execution lifecycle records carry structured MDC fields without leaking context across reused threads, with focused tests and the relevant Gradle checks passing. I’ll favor a scoped MDC restoration mechanism over a bare
setContextMap, since worker/executor threads are pooled. - Assistant
rgisn’t installed in this checkout, so I’m switching to targetedfind/grepsearches. The worktree appears clean from the initial status check. - Assistant
The inconsistency is localized: flow-produced logs use a private logger context preloaded from
LogEntry, while lifecycle logs inLogsuse the application logger with no MDC. I’ll apply the same field names (tenantId,namespace,flowId,executionId,taskId,taskRunId,triggerId) only for the duration of each log call, then restore the caller’s prior MDC. This covers flow, execution, task-run, and trigger paths and avoids pooled-thread leakage. - Assistant
The implementation and regression assertions are in place, including restoration of a pre-existing caller MDC. I’m running the focused core test now; after that I’ll address formatting/static checks and inspect the final diff for unnecessary scope.
- Assistant
The focused Gradle invocation is still initializing/building with no new output (the daemon is active, not failed). While it continues, one design check is worth calling out: lifecycle context intentionally replaces the MDC during emission instead of merging it, so an execution-level record cannot accidentally inherit a stale
taskIdfrom its calling thread; the entire prior context is restored immediately afterward. - Assistant
Gradle has remained silent for over two minutes, which is unusual for this repository but still below the project’s typical first-configuration time. I’m continuing the mandatory test rather than substituting a compile-only check; if it exposes an environment stall, I’ll capture that separately from code failures.
- Assistant
The first test was canceled after Gradle itself stalled before task configuration; its daemon log contains no compilation/test failure. I found the build cache location and will retry offline with plain, verbose output so dependency resolution cannot hang and any missing cached dependency is reported deterministically.
- Assistant
The retry reached task creation and confirmed the environment limitation:
mockito-core:5.23.0is absent from the local cache, so Gradle cannot create:core:testoffline. That is unrelated to these tests. I’ll still run offline production compilation and Spotless checks, both of which should use the already cached toolchain, and I’ll inspect test-source compilation feasibility afterward. - Assistant
Production compilation passed. The broad Spotless task failed identically across all 24 Java modules with an internal formatter-state error, so it did not identify a source formatting violation. During review I found one test-harness detail to tighten: Logback events resolve MDC lazily, so the in-memory appender must prepare/snapshot each event while the scoped MDC is active. I’m correcting that before the final verification.
- Assistant
Both production and all core test sources now compile successfully offline, including the new assertions. The remaining inability is only test execution: the build’s Java agent configuration demands uncached Mockito 5.23.0 even though compilation can use the cached dependency graph. I’m checking whether the repository exposes a local version override; if not, I’ll avoid mutating build configuration solely to work around this environment.
- Assistant
Test execution then exposed a second uncached agent (
aspectjweaver), also resolved eagerly even though this unit test does not use instrumentation. I can safely keep the repository untouched and substitute only the temporary test JVM agents with locally cached compatible Java-agent jars; this validates the focused test without weakening or editing the project build. - Assistant
The focused test reached execution setup but the offline runtime classpath has several additional missing artifacts (Allure, JUnit launcher, JSON Smart, Objenesis), so further substitution would no longer be a faithful test environment. I stopped there and removed the temporary init script. The meaningful local verification remains strong: production compilation and the complete core test-source compilation both pass; I’m doing the final diff/status audit now.
- Assistant
All five
LogsTestmethods now pass against the compiled classes, including the new MDC field assertions and prior-context restoration assertion. I ran them directly from the compiled test class because the standard Gradle test launcher’s unrelated runtime artifacts are not cached offline. No temporary files or build overrides remain; only the two intended source files are modified.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.