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 the logging service, related MDC helpers, and existing tests first. Then I’ll identify all execution/task/flow logging paths so the fix applies context safely without leaking MDC between pooled threads.
- Assistant
The linked path has moved in this checkout, so I’ll locate the service and module layout before inspecting its implementation.
- Assistant
This branch names the class
ExecutionLogService; ripgrep is unavailable, so I’ll usegrepand inspect that class plus its callers and tests. - Assistant
The former logging service behavior appears to have moved into
core.utils.Logs. I’ll inspect it, its tests, and the framework’s structured logging/MDC integration to understand the expected context keys and lifecycle. - Assistant
Logscurrently adds identifiers only to the rendered message, while task plugin logs use a dedicated logger context populated fromLogEntry.toMap(). I’ll inspect that map and the current dependency/API versions, then implement scoped MDC restoration (rather thansetContextMapwithout cleanup, which would leak or overwrite unrelated caller context). - Assistant
The canonical keys are confirmed. I’ll inspect the pre-refactor
LogServiceand recent history to determine whether the regression was an accidental omission and preserve all prior overload semantics, including flow and trigger logs. - Assistant
The regression is in every server-side helper: execution, flow, task-run, and trigger events all lack structured context. I’ll add a common scoped helper that merges identifiers into the current MDC for exactly one log call, omits null identifiers, and restores the complete prior MDC in
finally. This avoids both pooled-thread leakage and destruction of unrelated tracing context. - Assistant
Python is not installed, so no change was made. I’ll rewrite the small utility directly. I’ll also use a null-filtering context builder for all model types, since tenant IDs may legitimately be absent in some deployments/tests and
Map.ofwould throw on null. - Assistant
The focused tests pass, including the nullable-tenant task-run case. The combined command failed only because Gradle 9.4 detected a pre-existing task-ordering issue between Spotless and
copyGradleProperties; compilation and all four tests succeeded. I’ll run formatting and tests as separate invocations, then inspect the formatted diff. - Assistant
Spotless applied repository-wide formatting because this checkout’s baseline differs from the formatter version, touching many unrelated files (including tests). I’ll immediately discard all those formatter-only changes while preserving only
Logs.java, then verify the working tree contains exactly the intended source edit. - Assistant
The tree is clean except the intended file. To verify the reported behavior directly without adding/modifying repository tests, I’ll compile a temporary external Java harness. It will capture Logback events and assert execution/task structured identifiers are present, null identifiers are omitted, and pre-existing MDC values are restored after each helper call.
- Assistant
The source compiles, but the external harness picked Gradle’s instrumented transformed SLF4J jar ahead of the normal dependency and therefore failed during harness startup—not in project code. I’ll build a narrow classpath from the normal module cache (excluding Gradle transforms) and rerun the same harness.
- Assistant
The direct harness now passes and shows the exact expected JSON-source MDC maps, including preservation of an ambient
traceIdand omission of a null tenant. I’ll rerun the repository’s focused tests against the restored, single-file working tree and perform final status/diff checks before submitting. - Assistant
For the checklist’s reproduction item, I have validated the corrected output but have not yet captured the failure from the unmodified implementation. I’ll do a controlled before/after run using the same external harness: temporarily compile the original file and confirm its assertion fails due to an empty execution MDC, then restore/recompile the fix and confirm it passes. No test or repository file will remain changed beyond the intended source edit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.