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 understanding the issue. The problem is that MDC (Mapped Diagnostic Context) is not being consistently applied in task/flow logs after an upgrade to v1.0. Looking at the example, only the "Hello World!" log line has the MDC context (taskRunId, tenantId, namespace, executionId, flowId, taskId), while other log lines like "Flow started", "Type Log started", "Type Log with state SUCCESS completed", and "Flow completed with state SUCCESS" only have the context in the message text but not in the structured JSON fields.
The user pointed to the
LogService.javafile, specifically thelogExecutionmethod. Let me explore the repository structure and understand the issue better.Let me start by looking at the LogService class.
Assistant
It seems the find command didn't return any results. Let me try a different approach.
Assistant
Hmm, that's strange. Let me check the directory structure.
Assistant
The issue mentions the file is at
core/src/main/java/io/kestra/core/services/LogService.java. Let me look at that.Assistant
It seems find doesn't return output for some reason. Let me try using ls directly.
Assistant
Let me look at the core module structure.
Assistant
The
LogService.javadoesn't exist yet in this version of the repository. Let me search for the relevant logging code that the issue is referring toAssistant
Let me look at the
Logs.javafile, which seems to be the equivalent of theLogService.javamentioned inAssistant
Let me use the correct parameter name for the open command.
Assistant
Now I can see the issue clearly. The
Logs.javaclass formats log messages with context information embedded in the message string using SLF4J's{}placeholders, but it doesn't set the MDC (Mapped Diagnostic Context) context map. This means structured JSON log appenders don't see the context fields as separate JSON keys.Let me check how MDC is used elsewhere in the codebase
Assistant
Let me examine how MDC is used in the codebase
Assistant
Let me look at the LogEntry class to see the
toMapmethod and understand the MDC patternAssistant
Let me also look at how the Stackdriver JSON layout and other MDC-using code works
Assistant
Now let me check the Java version and how MDC is imported
Assistant
Let me check the gradle properties and build files.
Assistant
Let me check the build.gradle for Java version.
Assistant
Let me see the java configuration section more closely.
Assistant
Good, Java 25 -
Map.of()is available. Now let me look at how MDC is used elsewhere.Assistant
Let me look at how the
Logsmethods are called by the executor and worker to understand the full flowAssistant
Let me check where
Logs.logTaskRunisAssistant
Let me also look at the test file to understand the expected behavior.
Assistant
Now I have a clear understanding of the problem. Let me implement the fix. The
Logs.javaclass formats context information as part of the message string but doesn't set the MDC context map. This means structured JSON log appenders (like Stackdriver/Google Cloud Logging) can't see the context fields as separate JSON keys.Let me now edit the
Logs.javafile to add MDC contextAssistant
Let me use the correct open command format.
Assistant
Now I can see the full file. Let me add the MDC context setting. First, I'll add the necessary imports.
Assistant
Let me use the replace command to add the imports.
Assistant
Good, imports are updated. Now let me modify the
logExecution(FlowId flow, Logger logger, ...)method.Assistant
Now let me update the
logExecution(Execution execution, Logger logger, ...)method.Assistant
Now let me update the
logTriggermethod.Assistant
Now let me update the
logTaskRunmethod.Assistant
All four methods have been updated. Let me verify the full file.
Assistant
The file looks good. But let me check if the model classes can return null for any of these IDs, as
Map.of()doesn't allow null valuesAssistant
Let me check the TaskRun class fields to see if any can be null.
Assistant
All key TaskRun fields are
@NotNull. Let me verify the other model classes too.Assistant
Let me also check TriggerId and Execution for nullability.
Assistant
Let me check the Execution class fields.
Assistant
Let me check Execution class fields for nullability.
Assistant
Let me check the Execution tenantId field specifically
Assistant
I need to be careful with
Map.of()since it doesn't allow null values. Let me switch to usingHashMapfor safety, as some fields liketenantIdmay not be@NotNullin some models (e.g., Execution's tenantId field is not marked as@NotNull).Let me update the code to use HashMap instead.
Assistant
Let me use goto to navigate to line 9 then replace.
Assistant
Let me try the proper goto syntax.
Assistant
Now let me update all the
Map.ofcalls to useHashMapfor null safety. Let me update each method one by one.First, let me update the
logExecution(FlowId flow, Logger logger, ...)methodAssistant
Now update the
logExecution(Execution execution, Logger logger, ...)method.Assistant
Now update the
logTriggermethod.Assistant
Now update the last method:
logTaskRun.Assistant
All methods are updated. Let me verify the final file.
Assistant
I notice the
HashMapimport is missing. Let me add it.Assistant
Let me use the
--fileoption to make the replacement.Assistant
Let me scroll to show line 9 first.
Assistant
Let me use the edit command with --file to fix the import.
Assistant
Good, both imports are present now. Let me verify the full file content is correct.
Assistant
The code looks correct. Let me now try to compile and run the existing test to verify the changes.
Assistant
The build is successful. Now let me run the existing Logs tests.
Assistant
All 4 tests pass. The fix is working correctly. Let me now verify the changes are correct by reviewing the git
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.