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 understand the issue:
- After upgrading from Kestra v0.19.5 to v1.0.3, MDC (Mapped Diagnostic Context) context is no longer consistently applied in task/flow logs.
- In the JSON log output, only the actual "Hello World!" log message gets the MDC context (tenantId, namespace, executionId, flowId, taskId), while other log messages (like "Flow started", "Type Log started", "Type Log with state SUCCESS completed", "Flow completed with state SUCCESS") only have the context in the message string but not as structured fields in the JSON.
The user found that manually setting the context map in
LogService.java'slogExecutionmethod fixes the issue.Let me explore the repository structure and understand the code.
Assistant
Let me look at the key files to understand the MDC usage.
Assistant
Let me run them separately.
Assistant
Now let me look at the
LogService.javafile:Assistant
It seems the find command didn't find it. Let me search more broadly.
Assistant
Let me look at the key files. The user referenced a
LogService.javafile but that doesn't exist. Let me look atLogs.java:Assistant
Now let me look at
LogEntryto understand thetoMap()Assistant
Let me look at LogEntry.java
Assistant
Let me also look at
StackdriverJsonLayout.javaand how the MDC is used in the JSON layoutAssistant
Let me look at how MDC is used in
RunContext.javaandDefaultRunContext.javaAssistant
Let me check git log for recent changes to
Logs.javaandRunContextLogger.javaAssistant
Let me look at the git log for changes to
Logs.java:Assistant
Let me look at the commit that removed LogService and the logger name change
Assistant
Let me look at that commit in
Assistant
Let me see the commit where
LogServicewasAssistant
Let me see the old
LogServicebefore it was renamed toLogsAssistant
Let me look at the logback version and understand the logging event behavior
Assistant
Let me check the gradle dependencies more carefully.
Assistant
Let me check the version catalog
Assistant
Let me check the version catalog in a different way.
Assistant
Let me look at
DefaultRunContext.javato see how MDC is managedAssistant
Let me look at the logback base configuration
Assistant
Let me check what MDC imports are available and what methods are used elsewhere
Assistant
Let me find and check the gcp.xml logback config
Assistant
Let me check the gcp.xml and the cli logback.xml to understand the full configuration.
Assistant
Let me check the git tags and current branch
Assistant
Let me check git tags
Assistant
Let me check the current branch
Assistant
Let me check the logback LoggingEvent to understand MDC behavior
Assistant
Let me check the logback version used
Assistant
Let me check logback's LoggingEvent behavior
Assistant
Assistant
Let me open the file:
Assistant
Let me check the SLF4J version available
Assistant
Now I understand the issue. Let me implement the fix. I'll modify the
Logs.javafile to set MDC context before logging, which will make the structured fields (tenantId, namespace, flowId, etc.) appear in JSON log outputs for all system logs.First, let me add the MDC import:
Assistant
Now let me modify the
logExecution(FlowId, Logger, ...)method to set MDC contextAssistant
Now let me modify the
logExecution(Execution, Logger, ...)method:Assistant
Now let me modify the
logTriggermethod:Assistant
Now let me modify the
logTaskRunmethod:Assistant
Let me verify the Java version and check if the code
Assistant
Let me verify that
TriggerIdhas the correct methodsAssistant
Let me verify the file compiles by running the tests for Logs
Assistant
Let me check the
TriggerContextclass since the test uses itAssistant
Let me verify that
Flowimplements `FlowIdAssistant
Let me check the class hierarchy
Assistant
Let me check the interface hierarchy
Assistant
Let me check the
FlowIdinterfaceAssistant
Let me try to compile and run the Logs test
Assistant
Good, it compiles! Let me now run the Logs test:
Assistant
The tests pass. Now let me think about edge cases - some values like
tenantIdcould be null. Let me update the code to handle null values safely. Let me check whatMDC.putCloseabledoes with null:Assistant
Let me check the SLF4J MDC source to understand null handling
Assistant
Let me check the SLF4J MDC implementation
Assistant
Let me check what
MDC.putdoes with null valuesAssistant
Let me check the
MDCCloseable.close()Assistant
Let me try differently
Assistant
Let me verify the fix works properly by checking all callers of Logs methods
Assistant
Let me run the tests more broadly to ensure nothing is
Assistant
Let me view the final file to review
Assistant
Let me verify the logback LoggingEvent constructor behavior
Assistant
Let me check the LoggingEvent
Assistant
Let me try a different approach to inspect the LoggingEvent constructor
Assistant
Let me check how MDC is handled in LoggingEvent
Assistant
Let me check the LoggingEvent constructor
Assistant
Let me try a different approach
Assistant
Let me check the
getMDCPropertyMapmethodAssistant
Let me verify the complete flow works. Let me check if there's a
prepareForDeferredProcessingthat captures MDC earlyAssistant
The fix is complete and tests pass. Let me now review what I've done:
-
Problem: MDC context (tenantId, namespace, flowId, executionId, taskId, taskRunId, triggerId) was not being set when logging via
Logs.javamethods (logExecution, logTaskRun, logTrigger). This meant structured JSON log outputs (like StackdriverJsonLayout) didn't include these fields for system log messages like "Flow started", "Flow completed", "Type Log started", etc. -
Root cause: The
Logsmethods logged directly via SLF4J loggers without setting MDC context. Only flow logs going throughRunContextLoggerhad MDC context set. -
Fix: Added
MDC.putCloseable()calls in allLogs.javamethods that perform actual logging, ensuring MDC context is set before the log call and automatically cleaned up after.
Let me verify the final state of the file and then submit.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.