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
Let me start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at the issue description. The issue says that:
- The
LogService.logExecutionwas atcore/src/main/java/io/kestra/core/services/LogService.java(line 45) - The current
Logs.javais atcore/src/main/java/io/kestra/core/utils/Logs.java
Let me check the git history to see if there was a
LogService.javathat was moved.- The
Assistant
Now let me look at the issue more carefully. The user's report says the MDC context is missing from log messages. The
Logs.javafile currently doesn't set the MDC context before logging. Let me search for places where logs are emitted and where MDC is being used in the codebase.Assistant
Now I understand the architecture better. Let me look at the existing tests to understand the expected behavior.
Assistant
Now let me look at the original
LogService.javato see what it was doing before being refactored.Assistant
Let me look at the previous version of
LogServiceto understand the MDC context fix.Assistant
Now let me look at the test for
LogServiceand the previous version to understand the original MDC context behavior:Assistant
Now let me look at the kestra v0.19.5 version of
LogService.javato see what MDC context behavior was set.Assistant
Let me check if there's a tag for v0.19.5:
Assistant
Let me check all the commits to
LogService.java:Assistant
Let me check if MDC was set anywhere else in the older code:
Assistant
Let me look at the specific commit the user mentioned in the issue. The user references line 45 of LogService. Let me check the v0.19 era LogService:
Assistant
Let me look at the v0.19.5 version of the LogService.java to understand the MDC behavior:
Assistant
Let me look at how MDC is used in the StackdriverJsonLayout:
Assistant
Let me look at
LogEntry.toMap():Assistant
Let me look at the test code that already exists to see what to test:
Assistant
Let me look at how the RunContextLogger sets MDC and what it puts there:
Assistant
Now I understand the issue. The
RunContextLoggersets MDC for its own logger context, but theLogs.javastatic methods don't set MDC. Let me check the existing logback base.xml:Assistant
Now I have a clear understanding. The
Logs.javadoesn't set MDC context. Let me look at how MDC is imported and used:Assistant
There's no usage of
org.slf4j.MDCin the code. Let me check if the SLF4J library is available:Assistant
Now let me check how the
Logs.javais used throughout the codebase:Assistant
Let me first set up a test to reproduce the issue. Let me look at the existing test:
Assistant
Now let me write a simple test script to verify the issue:
Assistant
Now let me make the fix. I'll modify
Logs.javato set the MDC context before logging:Assistant
Now let me implement the fix to
Logs.java:Assistant
Let me make the fix to Logs.java by setting the MDC context. I'll need to:
- Save current MDC state
- Set the MDC context for the log entry
- Perform the log
- Restore the previous MDC state in finally block
Let me first re-read the Logs.java to see the current state.
Assistant
Now I'll modify the
Logs.javato set the MDC context before logging. The approach is to:- Save the current MDC state
- Set the new MDC context for the log entry
- Perform the log
- Restore the previous MDC state in a finally block (to avoid leaking the context)
Assistant
Let me simplify the
restoreMdcfunction:Assistant
Now let me run the existing test to make sure it still passes:
Assistant
All existing tests pass. Now let me write a temporary test to verify the MDC context is correctly set.
Assistant
All 8 tests pass! Now let me clean up by removing the temporary test file:
Assistant
Now let me verify the final state of
Logs.java:Assistant
Let me check the trigger types:
Assistant
Now let me run all the related tests to make sure nothing is broken:
Assistant
All existing tests pass. Now let me look at how
logTriggeris being used in the codebase to see if there's anyTriggerContextusage:Assistant
Let me look at the diff to see exactly what was changed:
Assistant
Let me also run a broader set of related tests to make sure my changes don't break anything:
Assistant
All 191 tests pass. Let me check the worker and scheduler tests:
Assistant
All tests pass. Let me also check the executor tests:
Assistant
All tests pass! Let me verify the changes by looking at the final state of the
Logs.java:Assistant
Let me check the final diff:
Assistant
Let me also verify there's no test that already tests the MDC behavior that I might have broken:
Assistant
Let me check the DefaultRunContext:
Assistant
Let me also run the broader tests to make sure I don't break anything:
Assistant
Let me also look at the final
Logs.javaone more time to ensure everything is correct:Assistant
Let me also verify the
PluginDefaultServicetest:Assistant
Let me run all the related tests one more time to confirm everything is working:
Assistant
All tests pass. Let me now go through the checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
- ✅ Yes, the issue was that
Logs.javadid not set MDC context. I verified by looking at the code and writing tests.
- ✅ Yes, the issue was that
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- ✅ Yes, my fix adds the MDC context for execution, task, trigger, and flow logs. I tested this with a temporary test (now removed) that verified the MDC context is correctly set.
-
I have thought about edge cases and made sure my fix handles them as well.
- ✅ Yes, I have considered:
- Null values (using
putIfNotNull) - MDC context leakage (using try/finally to restore previous MDC)
- MDC context when no previous context exists (calling )
- Null values (using
- ✅ Yes, I have considered:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.