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<?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 start by exploring the repository structure and understanding the issue.
Assistant
Now let me look at the LogsTest to understand expected behavior:
Assistant
Now let me find where MDC context is set (e.g., the "taskRunId", "tenantId" fields in the JSON output):
Assistant
Now I understand the issue. The
RunContextLoggerhas aForwardAppenderthat forwards logs to a logger with nameflow.{tenantId}.{namespace}.{flowId}(via theloggerName). TheRunContextLogger.initializeLogger()creates a separateLoggerContextwith anLogbackMDCAdapterand sets the context map from thelogEntry.toMap().So when a task logs (e.g., "Hello World!"), the MDC is set on the local logger context's MDC adapter, and when forwarded to the server log (root logger), the MDC context map is carried along. That's why the "Hello World!" log gets the MDC context.
But for the logs created by
Logs.logExecution,Logs.logTaskRun, etc., they directly use the root logger (or named loggers in the root context) without setting MDC context. So those logs don't have the MDC context.The issue suggests setting the MDC context map in the
logExecutionmethod. The original code referenced in the issue isLogService.javawhich no longer exists - it's been refactored toLogs.java.Let me look at how the MDC context map should be set. The issue is that the utility class methods set the context in the message itself (as etc.) but not in the MDC.
Assistant
So the JSON layout reads from
event.getMDCPropertyMap()(the MDC). This confirms that the MDC context is what produces the JSON fields liketaskRunId,tenantId, etc.Now let me understand the flow:
- For the "Hello World!" log: It comes from the RunContextLogger, which sets the MDC context map on its own LogbackMDCAdapter. When the ForwardAppender forwards the event to the server logger, the event carries the MDC context map, so it's in the JSON output.
- For the
Logs.logExecution/logTaskRun/logTriggerlogs: These are logged directly on server loggers without setting MDC, so they don't have the MDC context.
The fix: In the
Logsutility class methods, set the MDC context map before logging and clear it afterward. The issue suggests usingMDC.setContextMap.Let me look at what
LogEntry.toMap()returns to understand what keys are used:Assistant
Now let me check the git history to understand what changed from v0.19.5:
Assistant
So even in v0.19.5, the
LogServicedidn't set the MDC context map. Let me look more deeply at what was different. Let me check the v0.19.5 version of RunContextLogger and related logging:Assistant
Let me look at the git history of the RunContextLogger to understand the MDC forwarding:
Assistant
Let me check the logback version and how LoggingEvent captures MDC:
Assistant
Logback 1.5.32. Let me verify how LoggingEvent captures MDC in this version:
Assistant
The constructor doesn't capture MDC at construction. Let me check the
getMDCPropertyMap()method to see when it captures the MDC.Assistant
Now let me look at the callers of the
Logsutility methods to understand the contexts:Assistant
Let me look at the TaskRun and Execution models to understand available fields:
Assistant
Let me write a reproduction test to confirm the issue:
Assistant
The reproduction confirms the issue: MDC is
{}for execution and taskRun logs. Now let me implement the fix inLogs.java:Assistant
Now let me run my reproduction test to confirm the fix:
Assistant
Both tests pass. Let me extend the reproduction test to verify MDC restoration (no leak) and trigger logs:
Assistant
All 4 tests pass. Now let me run the existing
LogsTestto make sure I didn't break anything:Assistant
The existing LogsTest passes. Now let me check the RunContextLogger tests and other tests that might be affected by MDC changes:
Assistant
All RunContextLogger tests pass. Let me check formatting with spotless since the repo uses it:
Assistant
The spotless violations are pre-existing (195 other files). Let me check whether my specific file has any violations by running spotlessApply and checking the diff on my file:
Assistant
Let me check git status to see what was modified (spotlessApply may have touched other files):
Assistant
The spotlessApply modified many unrelated files (pre-existing violations auto-fixed). I should revert those and keep only my intended change. Let me revert everything except Logs.java:
Assistant
The spotlessApply changed many unrelated files. I need to revert all those and keep only my Logs.java change. Let me do this cleanly:
Assistant
Now only
Logs.javais modified. Let me verify the final diff and that the file still compiles/passes tests:Assistant
I notice I inadvertently changed a javadoc comment for
logTrigger. Let me check the original to decide whether to keep it:Assistant
To keep the diff focused strictly on the MDC fix, let me revert that unintended javadoc change:
Assistant
Now let me verify the diff is clean and re-run the tests to confirm everything still works:
Assistant
Now let me re-create the temporary test to verify the fix once more, plus run the existing LogsTest:
Assistant
All tests pass. Now let me remove the temporary test file and confirm the working tree is clean:
Assistant
The working tree is clean except for
Logs.java. Let me do a final compile check and run the RunContextLoggerTest to ensure no regression:Assistant
Everything compiles and tests pass. Let me view the final file to confirm correctness:
Assistant
The fix looks complete and correct. Let me do a final review of the diff that will be submitted:
Assistant
The fix is complete and focused. Let me provide a summary and checklist 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.