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, its MDC utilities, and nearby tests first. Then I’ll reproduce the missing context with the narrowest existing test setup, implement context restoration safely (including null fields and avoiding MDC leakage), and run the relevant test suite.
- Assistant
The issue’s linked path does not match this checkout’s module layout, so I’ll locate the class and identify the current project structure before inspecting implementation details.
- Assistant
This checkout has renamed
LogServicetoExecutionLogService, and ripgrep is unavailable. I’ll inspect that class, all call sites, MDC helpers, and its tests usinggrep/find. - Assistant
The active implementation is now the static
Logsutility. I’ll inspect it and its tests, plus logging encoder configuration, to determine expected key names and whether SLF4J’s scoped MDC API can preserve pre-existing thread context. - Assistant
The regression is visible in source: server-generated execution/task/trigger logs only format identifiers into the message and never attach MDC. I’ll inspect the removed pre-refactor service and related historical utility versions to restore the established behavior rather than inventing a new schema.
- Assistant
The old service also lacked explicit MDC, implying v0.19 inherited ambient worker/executor context that no longer survives the v1 threading/refactor. The robust fix belongs at each logging boundary. I’ll identify the canonical MDC field set from
LogEntryand the flow/task logging pipeline, then implement scoped enrichment for flow, execution, trigger, and task-run overloads—including overloads with caller-provided loggers. - Assistant
Canonical keys come from
LogEntry.toMap():tenantId,namespace,flowId,taskId,executionId,taskRunId, andtriggerId. I’ll add a private scoped helper that merges these identifiers over any existing MDC, emits exactly one event, then restores the original map infinally. This avoids both losing unrelated request context and leaking execution identifiers on pooled threads; null identifiers will be omitted. - Assistant
Python is not installed in this environment, so no changes were made. I’ll apply the same focused source edit using the repository’s
editcommand, replacing the utility as a whole to keep formatting deterministic. - Assistant
The implementation is in place and handles incomplete task runs without
Map.ofnull failures. I’ll now compile and run the focusedLogsTest; this will also expose formatting/static-analysis issues configured on the module. - Assistant
The focused suite passes. Because existing tests only assert logger names/counts, I’ll perform a temporary out-of-tree runtime probe against the compiled classes to verify the actual
ILoggingEventMDC map, ambient-context preservation/restoration, and null-field omission. The probe will not alter repository tests or tracked files. - Assistant
The runtime probe confirms the reported failure is fixed: execution/task events carry identifiers, stale identifier values are overridden only for the event, ambient keys survive, the thread MDC is restored afterward, and nullable identifiers are omitted. I’ll run the project’s formatter/check task and inspect the final diff for unnecessary or generated changes.
- Assistant
The module-wide Spotless check is already failing on 195 pre-existing source files, so it cannot serve as a clean repository-wide gate. I’ll nevertheless use Spotless to format the changed file precisely: save its formatted output, restore all tracked files, then reapply only the formatted
Logs.java. Afterward I’ll confirm that this file no longer appears among formatter violations. - Assistant
To satisfy the regression checklist rigorously (rather than relying only on source inspection and the report), I’ll run an out-of-tree A/B probe: temporarily compile the original tracked implementation and confirm its event lacks execution MDC, then restore/compile the fix and confirm identifiers are present. A shell trap will restore the fixed source even if the probe fails.
- Assistant
Final review and checklist:
- If an error was reported, I successfully reproduced it. The out-of-tree A/B probe against the original implementation captured only
{ambient=keep}and no execution identifiers. - If an error was reported, I confirmed the fix resolves it. The same probe against the fixed implementation captured
tenantId,namespace,flowId, andexecutionId, while retaining the ambient key. - I thought about edge cases and handled them. Context is merged for one log event and restored in
finally, stale execution keys are overridden only during emission, unrelated MDC is preserved, empty MDC is cleared afterward, and null model fields are omitted. Flow, execution, trigger, and task-run logs are all covered. - I ran affected existing tests and confirmed they pass. passed all 4 tests; the final source also passed . Spotless formatting was applied to the changed file. The module-wide Spotless check remains blocked by 195 unrelated pre-existing violations.
- If an error was reported, I successfully reproduced it. The out-of-tree A/B probe against the original implementation captured only
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.