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
I'll start by exploring the repository structure and understanding the issue.
Assistant
Now let me look at the ExecutionLogService and understand how the task logs get the MDC context.
Assistant
Let me find where the MDC context is set for task logs (the "Hello World" ones that DO get context).
Assistant
Let me look at the git history to understand what changed between versions.
Assistant
Let me look at how these logging methods were called before and whether MDC was set in older versions.
Assistant
So in the older version (v0.19.5), the
LogService.logExecutiondidn't set MDC either. Let me check the v0.19.5 version. The issue says the behavior was different in v0.19.5 - the MDC context was applied consistently.Let me check the older version. Let me look at the v0.19.5 tag.
Assistant
No tags. Let me look at the
LogEntry.toMap()method to understand the MDC keys used for task logs.Assistant
Now let me find all callers of the
Logsutility methods to understand the usage context.Assistant
Now let me look at the
TriggerIdclass to understand its fields.Assistant
Let me check the
Executionclass forexecutionKindand how LogEntry builds from execution.Assistant
Let me look at the
LogEntry.ofstatic methods to potentially reuse them for MDC context.Assistant
Let me check the
ExecutionKindenum.Assistant
Let me check the Logback version used and how
LoggingEvent.getMDCPropertyMapworks.Assistant
Let me check the
LoggingEvent.getMDCPropertyMapimplementation in Logback 1.5.32.Assistant
Let me check the repo's logback configuration files.
Assistant
Let me look at the
StackdriverJsonLayoutto see how it accesses MDC.Assistant
Let me check the
KestraLogFilterto ensure setting MDC won't interfere.Assistant
Let me check the git history to see if MDC was ever used in the execution logging in older versions.
Assistant
Let me find where "Flow started" is logged in the current code.
Assistant
Now I have a complete understanding. Let me implement the fix by modifying the
Logs.javautility class to set the MDC context before logging and restore it afterward. Let me rewrite the file.Assistant
Now let me rewrite the file with the MDC fix:
Assistant
Let me verify the file is correct:
Assistant
Now let me verify the code compiles. Let me check the build system.
Assistant
Compilation succeeded. Now let me write a temporary test to verify the MDC context is applied correctly.
Assistant
All 5 temporary tests pass. Now let me run the existing
LogsTestto ensure I didn't break anything.Assistant
All existing tests pass. Now let me clean up my temporary test file.
Assistant
Let me verify the formatting passes the spotless check that the project uses.
Assistant
The spotless check fails on 194 pre-existing files (not mine). Let me check the spotless configuration to ensure my file's import ordering is correct.
Assistant
The import order convention is: java, javax, org, com, io.kestra, then everything else. Let me check if my
Logs.javafile specifically has any spotless violations.Assistant
No spotless violations for my
Logs.javafile. Let me confirm by checking the full spotless output doesn't mention my file.Assistant
Let me check if there's any global MDC being set elsewhere (e.g., filters) that I should preserve via merging rather than replacing.
Assistant
Let me verify the
LoggingEventconstructor sets theloggerContextfrom the logger, to confirm my understanding of how the "Hello World" log gets MDC.Assistant
Let me decompile the constructor to see if it sets loggerContext. Let me use a different approach - use cfr or just check the bytecode for the constructor that takes (String, Logger, Level, String, Throwable, Object[]).
Let me search for the constructor signature in the bytecode.
Assistant
This confirms my understanding: the
LoggingEventcaptures MDC fromlogger.getLoggerContext().getMDCAdapter(). For global loggers, this is the thread-local MDC adapter, which my fix now sets correctly.Let me run the related
RunContextLoggertests to ensure nothing is broken.Assistant
All tests pass. Let me review the final diff to confirm only my intended change is present.
Assistant
I notice an unintended Javadoc change in the diff (the trigger logger name comment). Let me verify the original to decide whether to keep it.
Assistant
The original Javadoc said
'trigger.'but the actual logger uses the'scheduler.'prefix. This was an unintended change on my part. To keep my diff strictly focused on the MDC fix, I'll revert that Javadoc line back to the original.Assistant
Now let me verify the final diff is clean and focused only on the MDC fix:
Assistant
The diff is now clean and focused only on the MDC fix. Let me do a final verification - recompile and re-run the tests to confirm everything is in order.
Assistant
All 9 tests pass. Let me do a final check to ensure no temporary files are left behind and the working tree only contains my intended change.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.