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
Let me look at the structuremore closely.
Assistant
The issue mentions
LogService.java, but it seems tonot exist anymore. Let me search for it.Assistant
So
LogService.javawas renamed toLogs.javain theutilspackage. Let me find the new location.Assistant
Now let me understand the issue better. The MDC context (tenantId, namespace, flowId, executionId, taskRunId, taskId) is not being set before logging in the
Logsutility class. Let me see how MDC is currently used in the codebase.Assistant
Let me look at the LogEntry class to understand its
toMap()method.Assistant
Now let me look at how
Logs.logExecutionis called in the codebase.Assistant
Now let me see how the existing tests look like for the Logs class.
Assistant
Let me see how
ILoggingEventexposes MDC:Assistant
Let me look at the
ExecutorService.javato see the context.Assistant
Now let me look at the original LogService.java to understand the original logic:
Assistant
Let me check the git history for v0.19.5 to see how MDC was set.
Assistant
Let me check what v0.19.5 looked like:
Assistant
Let me check how the executor or worker set MDC in older versions:
Assistant
Let me look at the original code (pre-refactor). I need to find the olderWorker.java before this commit.
Assistant
Let me search for places that set MDC in the older code.
Assistant
Let me look at the more recent version of the
RunContextLoggerandExecutorService:Assistant
Now I understand the issue. In the old code, the executor used
flow.logger()which returned aRunContextLoggerthat had MDC set in its own context. In the new code,Logs.logExecution()uses a regular SLF4J logger which doesn't have MDC set.Let me check what the MDC keys look like in the working example to know what to set:
Assistant
Let me look at the existing test to understand how MDC is captured:
Assistant
Let me look at the base logback.xml to understand the MDC setup.
Assistant
Let me check the gcp.xml referenced in the issue:
Assistant
Now let me look at the structure of all tests in the project:
Assistant
Now let me write a test script to reproduce the issue first.
Assistant
Now I need to understand the issue more. Let me check the
FlowIdinterface to understand the full structure:Assistant
Let me first write a small reproduction script. Let me first check the build setup:
Assistant
Let me first see how to run existing tests to see if it works:
Assistant
Now let me first create a reproduction test file:
Assistant
Let me first run the existing LogsTest to make sure tests work:
Assistant
Now let me first reproduce the issue by adding a test that verifies the current behavior:
Assistant
The issue is confirmed. The MDC is empty for all log methods. Let me now implement the fix:
Assistant
Let me look at the TaskRun model to understand the structure:
Assistant
Let me look at how Execution has executionKind:
Assistant
Now let me look at what method to use to get the executionKind:
Assistant
Now I have enough understanding to implement the fix. Let me update the
Logs.javato set MDC context:Assistant
Wait, I need to checkif
ExecutionimplementsFlowId. Let me check that:Assistant
Executiondoesn't implementFlowId. Let me fix the implementation:Assistant
So Execution uses Lombok @Getter so all the fields have getters. Let me also look at the
FlowIdimportinExecution:Assistant
OK so Execution doesn't implement FlowId, but it has all the necessary fields. Let me also check the Flow interface tosee if it implements FlowId:
Assistant
Let me refactor my fix to be simpler:
Assistant
Wait, let me check the structure of
Execution:Assistant
Since
Executiondoesn't implementFlowId, let me fix that:Assistant
OK, so:
Execution.getTenantId()exists (Lombok)Execution.getNamespace()exists (Lombok)Execution.getFlowId()exists (Lombok) - returns flowIdExecution.getId()exists (Lombok) - returns execution ID (id)
So the only difference from FlowId is that
FlowId.getId()returns flowId, butExecution.getId()returns executionId. They both havegetFlowId(), butExecution.getFlowId()is explicit whileFlowId.getId()is the flow ID.Actually, looking at Flow:
Flow.getId()returns the flow's ID (inherited from FlowInterface.getId() -> FlowId.getId())Flow.getFlowId()doesn't exist directly, butFlow.getId()from FlowId is used
So in
Logs.java:- For
FlowId:flow.getId()= flow ID - For
Execution:execution.getId()= execution ID,execution.getFlowId()= flow ID
So I cannot pass
ExecutionasFlowIddirectly. Let me make this clearer by writing the code more explicitly.Let me revise the implementation:
Assistant
Now let me run the reproducer test:
Assistant
Let me fix the reproducer test to use the correct logger:
Assistant
Let me look at how to properly use the log annotation in my test:
Assistant
The existing LogsTest uses
@Slf4jwhich generates alogfield. Let me check why my reproducer test is failing differently:Assistant
The difference is the logger. The existing
LogsTest.logFlowuseslog(which is the class's own logger), not the ROOT logger. Let me change my test to use the class's own logger:Assistant
All my reproducer tests pass. Let me also verify the existing LogsTest still passes:
Assistant
All existing tests pass. Now let me also run other tests that might be affected by this change:
Assistant
Now let me check the broader test impact:
Assistant
Let me view the current Logs.java to verify my implementation:
Assistant
Let me also run other related tests to make sure nothing is broken:
Assistant
Let me run more relevant tests:
Assistant
All 197 tests in core/utils pass. Let me also run other related tests:
Assistant
The failing test
FilesServiceTest.testOutputFilesWithSpecialCharacters(Path)looks unrelated to my changes. Let me confirmby looking at it:Assistant
This test is about file handling, not related to my changes. Let me confirm by checking the actual error:
Assistant
This is unrelated to my changes - it's a locale issue with Unicode characters in file paths. Let me verify by reverting my changes:
Assistant
Confirmed - the test was already failing without my changes. Let me restore my changes:
Assistant
Good, my changes are restored. Let me check the file is in good state:
Assistant
Let me also run scheduler and worker tests:
Assistant
This is just
LogEntrynotLogsutility class. Let me look at tests that use the actualLogsclass:Assistant
Let me run a broader test to ensure I haven't broken anything:
Assistant
Let me look at the broader test results:
Assistant
Let me look at the output to see how many tests ran:
Assistant
Let me check the test report for completeness:
Assistant
All 97 tests in services passed. Let me also check the models tests:
Assistant
This is the same Unicode file system issue, unrelated to my changes. Let me confirm:
Assistant
Confirmed - this test was already failing without my changes. Let me delete my reproducer test now:
Assistant
Now let me verify by running the existing test:
Assistant
All 4 tests pass. Let me re-examine my implementation:
Assistant
Let me check how MDC behaves when empty:
Assistant
Let me look at the existing LogsTest to understand it better, and add tests to verify the MDC behavior:
Assistant
Let me check the version history more carefully:
Assistant
So in the version the user is using (v1.0.3), the logger prefixes are "execution.", "task.", "trigger.". In the current version (the codebase I'm working on), they are "executor.", "worker.", "scheduler.". These are different names but the same concept.
The user's example uses v1.0.3 logger names, but the MDC context should still be applied.
Wait, let me actually check the current version's git history to see what the logger prefix was when the user upgraded:
Assistant
Let me make sure the existing tests still pass:
Assistant
4 tests, 0 failures. Now let me write a temporary script to test the Logs class to confirm the fix works as expected:
Assistant
All 6 verification tests pass! Let me clean up the temporary verification test:
Assistant
Let me view the final Logs.java to make sure everything is in order:
Assistant
Let me check the final state of git:
Assistant
Let me verify the diff:
Assistant
Let me run the full LogsTest suite one more time to confirm everything is working:
Assistant
All 4 tests pass. Let me now check my fix against the test scenarios:
- Reproducer Test (deleted): Tests confirmed the MDC is set correctly for
logExecution,logTaskRun,logTrigger, andlogFlow. - MDC restoration: The MDC is properly restored after the log call.
- Edge cases:
- Concurrent logging from multiple threads: Safe due to MDC being thread-local
- Exception during logging: MDC is restored in finally block
- MDC was already set: Existing MDC keys are preserved
- MDC was empty: MDC is cleared after log call
Let me also verify my fix doesn't affect tests in other modules:
- Reproducer Test (deleted): Tests confirmed the MDC is set correctly for
Assistant
All 55 executor tests pass. Let me also run scheduler tests:
Assistant
All 78 scheduler tests pass. Let me also run worker tests:
Assistant
Let me make sure I'm not leaving any debug files:
Assistant
Only the Logs.java file is modified. Let me do a final review:
Assistant
Let me also check the entire test suite runs without breaking:
Assistant
All Log-related tests pass! Let me also verify the testsin other places:
Assistant
37 Log-related tests, 0 failures. All pass.
Let me make a final check on the fix:
Assistant
The fix is complete. Let me also verify the implementation handles all edge cases. Let me think through them once more:
Assistant
All 4 edge case tests pass:
shouldHandleNullTenantIdInFlow- handles null fields gracefullyshouldHandleExceptionInLogCall- MDC is restored even on exceptionshouldNotLeakMdcBetweenLogs- MDC doesn't leak between log callsshouldPreserveUserMdcKeys- User's MDC keys are preserved
Let me clean up the edge case test:
Assistant
Let me do a final check to ensure the repository is clean:
Assistant
Repository is clean - only the Logs.java file is modified.
Now let me also verify the diff one more time:
Assistant
The fix is complete and verified. Let me submit the changes now.
Summary
The issue was that after upgrading from Kestra v0.19.5 to v1.0, the MDC context (tenantId, namespace, flowId, executionId, taskRunId, taskId) was no longer applied to log messages emitted from the
Logsutility class. This regression occurred because theLogsutility class uses regular SLF4J loggers obtained viaLoggerFactory.getLogger(...), which unlikeRunContextLogger, doesn't have MDC context set.Fix
Modified
core/src/main/java/io/kestra/core/utils/Logs.javato:- Set MDC context before each log call (using thread-local MDC)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.