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 I need to understand the structure. The issue is that the
Logs.javadoesn't set MDC context. Let me find the LogService.java mentioned in the issue (line45).Assistant
Now let me look at the issue more carefully. The issue mentions
https://github.com/kestra-io/kestra/blob/eb22d3f6ee5b6b92f8a12f03948e7517a02e39cc/core/src/main/java/io/kestra/core/services/LogService.java#L45. This file doesn't exist anymore but there's aLogs.javautility class that handles thelogExecutionmethod. Let me find the commitreferenced.Assistant
Now I have a good understanding of the code. Let me look at the LogService history more carefully to understand the changes that caused the issue.
Assistant
Let me look at the previous version of the LogService to see what was different.
Assistant
Let me look at the existing logback configuration:
Assistant
Now I understand. The
StackdriverJsonLayoutputs MDC properties into the JSON output. The MDC needs to be set for proper context propagation. Let me explore the Logs.java further to understand all the relevant methods.Assistant
Let me check the SLF4J version being used:
Assistant
Let me check if
addKeyValuewould work with the existing logback configuration:Assistant
Now let me look at how
JsonLayouthandlesKeyValuePair:Assistant
The logback-json-classic jar is not in the local cache. Let me see if I can download it frommaven:
Assistant
Let me look at how RunContextLogger handles MDC to understand the patterns:
Assistant
Let me look at the existing LogServiceTest if it exists:
Assistant
Let me look at the StackdriverJsonLayout carefully to understand what it includes:
Assistant
Let me look at where
Logs.logExecutionis being called:Assistant
Let me look at how the worker logs the "Hello World" message to see why MDC is set there:
Assistant
Let me check
LogEntry.of()and its toMap() method:Assistant
Let me see if there's a way to look at the actual logging happening to understand the issue better. Let me first reproduce the issue.
Assistant
Hmm, there's no
MDCimport. Let me see if SLF4J's MDC is being used via other imports.Assistant
Now let me create a small reproduction script to verify my understanding of the issue first:
Assistant
Let me look at the ForwardAppender and how MDC is propagated:
Assistant
Now I have a clear understanding. Let me plan and implement the fix:
- Modify
Logs.javato set MDC context before logging and restore after - The MDC should include
tenantId,namespace,flowId, and additional fields based on the type (executionId, taskId, taskRunId, triggerId) - Use try-finally to ensure restoration on exceptions
- Modify
Assistant
Now let me implement the fix. I'll modify
Logs.javato set MDC context before logging:Assistant
Now I'll write the new Logs.java with MDC context set:
Assistant
Let me verify the file:
Assistant
Now let me verify the code compiles by attempting to compile the core module:
Assistant
The code compiles successfully. Let me run the existing tests for Logs.java to make sure they still pass:
Assistant
All 4 existing tests pass. Now let me write a script to reproduce the original issue and confirm my fix works:
Assistant
Let me create a temporary test file to verify my fix works correctly:
Assistant
There's a compilation error. Let me find out what's wrong:
Assistant
Let me check the test logback config:
Assistant
Let me fix the test to use a logger that allows INFO level:
Assistant
All 7 tests pass. Now let me verify the existing tests for
LogsTest.javastill pass:Assistant
Let me look at how the original code handled null values and check edge cases:
Assistant
Let me update the implementation to filter out null values, consistent with
LogEntry.toMap():Assistant
Let me verify the tests still pass with the updated implementation:
Assistant
All 11 tests pass. Now let me also add a test case for null values:
Assistant
Wait, I need to remove the test file I added. The instructions say I should NOT add new tests:
Assistant
Now let me verify the existing tests still pass:
Assistant
Now let me create a temporary standalone script to verify the fix works correctly with the user's setup:
Assistant
Let me run the existing tests to verify everything is working:
Assistant
All 191 tests in the
io.kestra.core.utilspackage pass. Let me also run a broader test suite to make sure I haven't broken anything:Assistant
This failure is unrelated to my change. It's about file system encoding for special characters (Thai characters in filenames). Let me verify this is a pre-existing failure:
Assistant
The failure is pre-existing and unrelated to my change. Let me restore my changes:
Assistant
Let me test the change with the user's actual setup by creating a temporary test:
Assistant
Let me write a more focused reproduction test that uses the user's actual logback config:
Assistant
All 4 tests pass with proper MDC context. Now let me clean up the test files:
Assistant
Let me run all the Logs-related tests one more time:
Assistant
The build is successful but uses cached results. Let me force a rerun to confirm thetests pass:
Assistant
All 4 tests pass. Let me also run the executor tests since they use Logs.java heavily:
Assistant
All tests pass! Let me view the final Logs.java file to make sure everything is correct:
Assistant
Let me also verify that I have considered all edge cases. Let me look at the test once more:
Assistant
Let me verify the final diff:
Assistant
Let me do a final verification of the fix:
Assistant
All 4 existing LogsTest tests pass. Let me also verify the final file:
Assistant
Let me also run the Spotless format check to make sure the code style is correct:
Assistant
Let me check if my Logs.java has any style violations:
Assistant
Let me also verify my fix with edge cases:
Assistant
All 3 edge case tests pass. Let me clean up the test file (since the instructions say not to add new tests):
Assistant
Let me also remove the /tmp logback test files I made:
Assistant
Let me run the final tests one more time to make sure everything is in order:
Assistant
Now let me address the checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.