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
I'll start by exploring the repository structure and understanding the codebase, then locate the
LogService.javafile mentioned in the issue.Assistant
Let me find the LogService.java file in the core directory.
Assistant
Let me search more broadly:
Assistant
I don't see a
LogService.javafile. Let me search for it or look for similar logging related files:Assistant
Let me search for any file containing "LogService":
Assistant
So the issue is about MDC (Mapped Diagnostic Context) not being applied consistently in task/flow logs. The user mentioned that they found a workaround by manually setting the contextMap in LogService.java, but I can't find that file. Let me search for where the logging methods like
logExecutionare defined.Assistant
Let me search for where the actual logging with MDC context happens:
Assistant
Let me continue investigating the MDC-related files to understand the logging issue.
Assistant
Now I can see part of the issue. Let me also check the
DefaultRunContext.javafile:Assistant
Now I see the issue. The
RunContextLoggersets MDC context in itsinitializeLogger()method, but theForwardAppenderforwards logs to the root logger without propagating the MDC context. Let me also check where execution-level logs are generated (like "Flow started", "Task started"):Assistant
Let me continue investigating where execution-level logs are generated and how they use logging.
Assistant
I can see that the code is using
Logs.logExecution()to log execution-level messages. Let me find theLogsclass to understand how it logs.Assistant
I can see the Logs class is at
/kestra/core/src/main/java/io/kestra/core/utils/Logs.java. Let me examine it to understand how it logs execution-level messages and where the MDC context should be set.Assistant
Now I can see the issue clearly! The
Logsutility class logs execution-level messages (like "Flow started", "Flow completed") with context info in the message itself, but it doesn't set the MDC context. Only theRunContextLogger(used for task-level logging) sets the MDC context.Let me check the LogEntry model to understand what MDC fields should be set:
Assistant
Now I understand the issue. The
Logsutility class logs execution-level messages but doesn't set the MDC context. TheRunContextLoggerdoes set MDC context ininitializeLogger(), but theLogsclass doesn't.Let me look at the LogEntry.toMap() to see what fields should be in the MDC context.
Assistant
Now I have a clear picture of the issue:
- The
Logsutility class is used for logging execution-level messages (like "Flow started", "Task started/completed", "Flow completed") - The
Logsclass logs messages but does NOT set MDC context - The
RunContextLoggersets MDC context for task-level logging, but only when the logger is initialized - When logs are forwarded via the
ForwardAppender, the MDC context is not propagated to the forwarded logs
Looking at the issue description, the user suggests adding
MDC.setContextMap()in thelogExecutionmethod. Let me implement this fix in theLogsclass.First, let me check the imports needed for MDC:
- The
Assistant
Now I understand the issue better. The
Logsutility class uses standard SLF4J loggers (fromLoggerFactory.getLogger()), which don't have MDC context set. I need to modify theLogsclass to set the MDC context before logging and clear it after.Let me create the fix for the
Logs.javafile:Assistant
The open command doesn't work the way I tried. Let me look at the usage more carefully - it seems like the tool expects specific formatting. Let me try using the bash terminal to view and edit the file instead.
Assistant
Now I understand the issue. The
Logsclass is used for execution-level logging but doesn't set MDC context. When using certain log appenders (like GCP JSON), the contextual information needs to be in MDC to be included in the log output.Let me create the fix by modifying the
Logsclass to set MDC context before logging and clear it after:Assistant
Let me use the correct syntax for the edit command:
Assistant
The user is pointing out that I didn't provide proper reasoning before my tool call. Let me fix this and continue with proper reasoning.
I've already modified the Logs.java file to add MDC context. Now I need to verify the changes were applied correctly and then look for any tests to run.
Assistant
Good. The fix has been applied. Now let me verify the changes look correct and check if there are any existing tests for the Logs class.
Assistant
I can see there are existing tests for the Logs class. The tests don't specifically check for MDC context, but they test that logging works correctly. Let me run the existing tests to make sure my changes don't break anything.
Assistant
I see the issue -
Map.of()doesn't accept null values, but some fields liketenantIdcan be null. I need to handle null values properly when building the MDC context map. Let me fix this by adding a helper method that filters out null values.Assistant
I've fixed the null pointer exception by creating a helper method
toMdcContext()that filters out null values before building the MDC context map. This is important because some fields liketenantIdcan be null. Now let me run the tests again to verify the fix.Assistant
All tests are passing now. Let me verify the fix is correct by viewing the final file and then run a broader test to make sure I haven't broken anything else.
Assistant
The fix looks good. Now let me run a broader set of tests to make sure I haven't broken anything else in the codebase. Let me also check if there are any other places where Logs class is used that might be affected.
Assistant
Let me look for tests that might exercise the Logs class or execution-related logging to verify the fix doesn't break anything.
Assistant
The tests passed. Now let me verify the git diff to see the changes I made and make sure everything is correct before submitting.
Assistant
Now let me run a few more tests to make sure the changes don't break anything else in the system:
Assistant
Good, all tests pass. Let me now run a few more related tests to ensure nothing is broken.
Assistant
Let me try running a smaller subset of tests to validate the fix:
Assistant
All tests pass. Let me now create a simple test script to verify that the MDC context is properly set when using the Logs class.
Assistant
Now let me verify the changes work correctly by reviewing the final code:
Assistant
Now let me verify that the fix addresses the issue described. The issue states that MDC context is not applied consistently in task/flow logs. My fix:
- Adds MDC context setting in all logging methods (
logExecution,logTrigger,logTaskRun) - Uses a helper method
toMdcContextthat filters out null values (important for tenantId which can be null) - Properly clears MDC context after logging using try-finally to ensure thread safety
Let me verify the change summary one more time and then submit:
- Adds MDC context setting in all logging methods (
Assistant
Now let me verify once more that all the existing tests pass for the Logs class:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.