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 start by exploring the repository structure and understanding the issue.
Assistant
I'll start by exploring the LogService.java file mentioned in the issue.
Assistant
Now I see the
Logs.javafile. The issue is about MDC context not being applied consistently. The originalLogService.logExecutionhad a method that set MDC context. Now theLogsutility class doesn't set MDC context at all.Let me look at the issue more carefully. The example shows:
- Flow started log:
[tenant: main] [namespace: company.team] [flow: gnu_170208] [execution: 1FLhPmmXgvIghVXzW7hY90] Flow started— has the prefix in the message but no MDC context - "Hello World!" log: has MDC context fields (taskRunId, tenantId, namespace, executionId, flowId, taskId)
- Other logs: have prefix in message but no MDC context
So the issue is that the MDC context is not being set when logging via
Logs.logExecution,Logs.logTaskRun, etc. The user wants the MDC context to be set so that structured logging (like JSON) includes the context fields.The user's suggested fix was to add
MDC.setContextMap(...)in thelogExecutionmethod. Let me look at how the originalLogServicelooked and how the RunContextLogger sets the MDC context (since the "Hello World!" log has the MDC context).Let me look at the RunContextLogger to see how it sets MDC context.
- Flow started log:
Assistant
Now let me look at the git history to understand what changed with the LogService.
Assistant
Now let me look at the logback configuration files to understand how MDC is consumed.
Assistant
Let me look at the StackdriverJsonLayout to understand how MDC context is consumed in the JSON output.
Assistant
Let me look at the LogEntry class and its
toMap()method.Assistant
Now let me look at the existing LogsTest to understand the expected behavior and tests.
Assistant
Let me look at how MDC is used in the RunContext and elsewhere.
Assistant
Let me check the v0.19.5 version of LogService to understand the original behavior.
Assistant
No tags. Let me check the git log for the original LogService before the refactor, specifically looking at the version that the issue references. Let me search the git history for MDC in the LogService.
Assistant
Let me look at the commit
5d15e2081which mentions log timestamp precision and MDC. Andc9095bd87reset the MDC when we clean the run context.Let me look at how the LogService was before the refactor that removed the MDC. Let me check the original
LogService.javabefore commit5a8552ad3(refactor: remove LogService). I'll look at the parent commit's version of LogService.Assistant
Let me look at the breaking change commit that changed logger names.
Assistant
Now let me understand how the executor logs "Flow started". Let me look at how the Logs methods are called.
Assistant
Let me look at the FlowId and TriggerId interfaces to understand the available fields.
Assistant
Let me check the TaskRun model to see available fields including executionKind.
Assistant
Now I have a clear understanding of the issue. Let me look at the current
Logs.javaonce more and check the imports, then I'll implement the fix.Assistant
Now I understand the issue completely. The
Logsutility methods (used by executor, scheduler, worker for "Flow started", "Type Log started", etc.) log directly to the global logger without setting the MDC context. Meanwhile, theRunContextLogger(used for "Hello World!" flow logs) sets the MDC context vialogEntry.toMap(). This is why only the flow log gets the MDC context fields in structured (JSON) output.Let me reproduce the issue first by writing a small test to confirm the MDC is not set.
Assistant
Let me create a temporary test to confirm the issue (MDC is empty for
Logs.logExecution), then implement the fix.Assistant
The test confirms the issue: the MDC is empty
{}for bothLogs.logExecutionandLogs.logTaskRun. Now let me implement the fix inLogs.java.Assistant
Now let me verify the fix compiles and the MDC is set correctly.
Assistant
The fix works. Now let me verify the existing
LogsTeststill passes, and check that MDC doesn't leak (is restored after the log call).Assistant
Existing tests pass. Now let me verify the MDC is properly restored (not leaked) after the log call by adding a leak check to my temporary test.
Assistant
All tests pass, including the leak-prevention tests. Let me clean up the temporary test file and search for any other tests that might be affected by the MDC change.
Assistant
No tests assert on MDC property maps (the grep found nothing, exit code 1 just means no matches). Let me run the broader logging-related tests to ensure nothing breaks.
Assistant
All tests pass. Now let me verify the code formatting passes the spotless check (the repo applies spotless).
Assistant
The spotless check shows violations in 195+ other files (pre-existing). Let me check whether my specific file
Logs.javahas any violations.Assistant
My file
Logs.javais not in the spotless violation list. Let me confirm the violations are pre-existing by stashing my change and re-running.Assistant
Confirmed — the spotless violations are pre-existing (the build failed even without my change). My change doesn't introduce any new violations.
Let me review the final state of my change.
Assistant
Let me check the
ExecutionKindand whetherexecution.getKind()could be null, to decide if I should include it for consistency with theRunContextLogger.Assistant
The
ExecutionKindcan be null (NORMAL isn't persisted). My implementation correctly filters nulls. The fields I include (tenantId, namespace, flowId, executionId, taskId, taskRunId, triggerId) match exactly the keys produced byLogEntry.toMap()(used byRunContextLogger), ensuring consistency with the "Hello World!" flow log.Let me run a final compilation check across the modules that use
Logs(executor, worker, scheduler) to ensure nothing breaks.Assistant
Let me force a clean recompile of core to be 100% sure the change compiles cleanly.
Assistant
The core module compiles cleanly. Let me verify the temp test file is removed and confirm only my intended change remains.
Assistant
My change is clean — only
Logs.javais modified, with no leftover temp files. Let me do a final review of the complete implementation.Assistant
The implementation is complete and clean. Let me run the full
LogsTestone final time to confirm everything is green before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.