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 to understand the codebase and locate the relevant files mentioned in the issue. Let's first check the current directory and list files.
- Assistant
Let me find the LogService.java file mentioned in the issue.
- Assistant
Hmm, it seems LogService.java doesn't exist. Let me check the exact path mentioned in the issue:
core/src/main/java/io/kestra/core/services/LogService.java. Let's see what files are in that directory. - Assistant
The issue mentions
LogService.javaatcore/src/main/java/io/kestra/core/services/LogService.java. Let me search more broadly. - Assistant
Let me look at the relevant files. The issue mentions
LogService.javabut the repo might have been renamed. Let me checkLogs.javaandExecutionLogService.java. - Assistant
Now I can see the issue. The
Logs.javautility class doesn't set MDC context before logging. Let me look at how MDC is used elsewhere in the codebase to understand the pattern. - Assistant
Let me look at the RunContextLogger to understand how MDC is typically handled:
- Assistant
Let me look at the LogEntry class to understand the toMap method:
- Assistant
Let me find the
toMapmethod: - Assistant
Let me see the toMap method to understand what keys are used.
- Assistant
Now let me understand the full picture. Let me look at where
Logs.logExecutionandLogs.logTaskRunare called from, and how MDC is managed: - Assistant
Let me also check where execution-level logs are called in the executor:
- Assistant
Now let me look at the StackdriverJsonLayout to understand how MDC is used in logging:
- Assistant
Now I have a clear understanding of the issue. Let me look at the Logs.java file in detail and plan the fix:
- Assistant
Let me check the existing tests to understand the expected behavior:
- Assistant
Let me check the FlowId and related classes:
- Assistant
Let me check FlowId interface to understand what methods are available:
- Assistant
Now I understand the issue. Let me create a reproduction script to confirm the problem:
- Assistant
Now I understand the issue clearly. Let me look at the
TriggerIdclass to understand its structure: - Assistant
Now I understand the full picture. The issue is that when using external logging appenders (like JSON format), the MDC context (tenantId, namespace, flowId, executionId, etc.) is not being set before the log messages are written. The
Logsutility class doesn't set MDC context, so external appenders don't include the contextual information.Let me now implement the fix:
- Assistant
Now let me implement the fix. I need to modify the
Logs.javafile to set MDC context before logging and clean it up after: - Assistant
Now let me apply the fix to the actual file:
- Assistant
Now let me apply the fix. First, let me add the necessary imports:
- Assistant
Now let me update the first
logExecutionmethod to set MDC context: - Assistant
Now let me update the second
logExecutionmethod (with Execution parameter): - Assistant
Now let me update the
logTriggermethod: - Assistant
Now let me update the
logTaskRunmethod: - Assistant
Now let me add the helper methods for setting MDC context. I'll add them before the logger methods:
- Assistant
I see there's a duplicate line. Let me fix that:
- Assistant
I still see a duplicate line. Let me remove it:
- Assistant
Let me verify the final file looks correct:
- Assistant
The file looks good. Now let me add a blank line between the methods for better readability:
- Assistant
Good. Now let me run the existing tests to make sure they pass. Let me check what build tool is used:
- Assistant
Let me run the existing test to make sure it passes:
- Assistant
All existing tests pass. Now let me check if there's a
LogRecordMapperor similar that might also need to be aware of MDC: - Assistant
Let me also check the LogExporter to understand the complete logging pipeline:
- Assistant
Now let me also check the
RunContextLoggerto understand how MDC is handled for task-level logs, and verify our fix is consistent: - Assistant
Good. The
RunContextLoggeruses a dedicatedLogbackMDCAdapterper logger context and sets MDC viasetContextMap(this.logEntry.toMap()). Our fix for theLogsutility class follows the same pattern but uses the global SLF4J MDC.Let me also check if there are any other files that might need similar fixes. Let me search for other places where execution/trigger/task logging happens: focus on the specific patterns mentioned in the issue:Let me also check if there are any other logging patterns in the executor service that might be affected:
- Assistant
Let me check how the executor service creates loggers:
- Assistant
The fix looks good. The
Logsutility class is used throughout the codebase for server-side logging. Let me now also checkExecutionLogServiceto see if it also needs MDC context: - Assistant
Good,
ExecutionLogServiceis for fetching logs, not producing them. Now let me review the final diff to make sure everything looks clean: - Assistant
The diff looks clean and correct. Let me also add a verification to the test to ensure MDC is properly set during logging. First, let me check if there's a test that we can run to verify our changes don't break anything:
- Assistant
All tests pass. Now let me write a quick test script to verify the MDC is being set correctly:
- Assistant
Let me verify the complete final state of the file:
- Assistant
The code looks clean and correct. Let me also check if there's a
logExecutionoverload that takes aFlowIdand aLogger- let me make sure it's not missing MDC: - Assistant
Good, that method already has MDC support. Let me run the test one more time to confirm everything passes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.