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 call paths, and existing tests first. Then I’ll reproduce the missing MDC fields with the narrowest relevant test, implement scoped MDC handling (avoiding context leakage across pooled threads), and run the affected test suite.
- Assistant
The issue’s linked path no longer matches this checkout, so I’ll locate the current module/file layout before tracing the implementation.
- Assistant
This branch has renamed the class to
ExecutionLogService, and ripgrep is unavailable. I’ll inspect that service plus all direct references and logging/MDC utilities usinggrep. - Assistant
The active logging entry point is now the static
Logsutility. I’ll inspect it, its tests, andRunContextLoggerto understand intended field names and whether the current behavior protects pre-existing MDC state. - Assistant
The reproduction is visible in code: only
RunContextLoggerinstalls metadata into its private logger context; the server-sideLogsmethods emit prefixed text without MDC. The fix should cover every server logging overload (flow, execution, trigger, task run), use the establishedLogEntrykey names, and restore the caller’s MDC afterward so pooled threads cannot retain another execution’s metadata. I’ll verify model nullability and project SLF4J version before implementing a small scoped helper. - Assistant
I’ll inspect
LogEntry.toMap()and dependency constraints now; model searches returned no declarations because these are likely records/interfaces in different modules or generated via annotations. - Assistant
LogEntry.toMap()confirms the canonical keys and already filters null values. I’ll inspect its factory overloads; reusing them will prevent metadata mapping from diverging between persisted flow logs and server logs. - Assistant
The history includes the refactor that removed
LogService; I’ll compare its predecessor to identify the exact regression and preserve prior semantics rather than inventing new behavior. - Assistant
The prior implementation also lacked MDC, confirming this is a behavioral regression relative to the older release rather than just the recent class rename. I’ll add scoped MDC at the four actual emission points. The scope will merge metadata into any existing context (preserving fields such as
"context":"default"), override stale execution keys for the event, omit null values, and restore the exact prior thread context infinally. - Assistant
Python is not installed in this environment, so no file was changed. I’ll apply the same focused edit using the repository editor command, replacing the complete small utility to avoid brittle shell substitutions.
- Assistant
One edge case needs tightening before testing: simply merging can preserve stale managed keys (for example
taskIdfrom an outer task scope on an execution-only event). I’ll clear all Kestra execution MDC keys from the temporary map before adding the event’s own fields, while still preserving unrelated MDC such as tracing/context identifiers. - Assistant
The focused test suite passes, but its current assertions only check counts/logger names. To explicitly reproduce and verify the reported behavior without changing repository tests, I’ll compile a temporary external harness against the built classes. It will capture Logback events, assert execution/task MDC fields, verify preservation of an unrelated
"context"key, and verify restoration/no leakage after logging. - Assistant
The external reproduction passes with the exact structured fields from the report and confirms stale fields do not contaminate events or leak afterward. I’ll now run the project formatting/static checks for the affected module and inspect the final diff for unnecessary changes.
- Assistant
Spotless reformatted the modified file successfully, but the module-wide check fails on 194 pre-existing files unrelated to this change. I’ll determine whether the build supports a file-target property; if not, I’ll use Spotless’s per-file diagnostic output to confirm
Logs.javais absent from violations, then run compilation/tests once more after formatting. - Assistant
Implementation and verification are complete.
Checklist:
- Reproduced the reported error: Code tracing showed server-side
Logsemissions had no MDC setup, unlike flow logger emissions. A temporary external Logback harness captured the affected execution/task event paths and represented the reported missing-field scenario. - Confirmed the fix resolves it: The harness now captures
tenantId,namespace,flowId,executionId,taskId, andtaskRunIdas applicable. It also confirmed unrelated MDC is preserved and prior MDC is restored after emission. - Considered edge cases: Null metadata is omitted; stale task/execution keys cannot contaminate a different event; existing non-Kestra MDC survives; and
finallyrestoration prevents pooled-thread leakage even if logging throws.
- Reproduced the reported error: Code tracing showed server-side
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.