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<?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:
json{"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"} Assistant
I'll start by exploring the repository structure and understanding the issue.
Assistant
I'll start by exploring the repository structure and understanding the issue.
Assistant
Let me find the file first.
Assistant
Let me find the file first.
Assistant
No results. Let me check the repository structure.
Assistant
Let me search for the LogService file with a broader pattern.
Assistant
The file doesn't exist at that location. Let me search for the methods mentioned in the issue.
Assistant
Now I understand the structure. The
Logs.javautility class is the equivalent of theLogService.javamentioned in the issue. Let me look at how MDC is used elsewhere to understand the pattern.Assistant
Now let me understand the
LogEntrymodel to know the MDC field names.Assistant
Now let me look at the existing test for Logs to understand expectations.
Assistant
Let me look at the logback configuration files and the StackdriverJsonLayout.
Assistant
Now I have a clear understanding of the issue. The
Logs.javautility class puts context info into the message string (e.g.,[tenant: main] [namespace: ...]) but does NOT set the MDC context map. TheStackdriverJsonLayout(GCP JSON layout) reads MDC properties viaevent.getMDCPropertyMap()to produce structured fields liketaskRunId,tenantId, etc. TheRunContextLogger(used for task logs like "Hello World!") sets MDC viaLogEntry.toMap(), which is why only that log line gets the structured MDC fields.Let me verify by looking at how the methods are called and confirm the
includeMDCdefault.Assistant
Now let me implement the fix. I'll modify
Logs.javato set the MDC context map before logging and restore it afterward, so structured JSON appenders can pick up the fields.Assistant
Now I'll rewrite the file to set the MDC context map consistently for all log methods.
Assistant
Let me verify the
TaskRun.attemptNumber()method exists and check theLogEntrybuilder.Assistant
The
toMap()method doesn't includeattemptNumber, so I'll remove that from the builder to keep things clean. Let me update that.Assistant
Now let me compile the core module to make sure the changes compile correctly.
Assistant
The core module compiles successfully (only pre-existing warnings). Now let me write a test script to verify the MDC context is actually applied. Let me first check the existing
LogsTestto extend it conceptually with a verification, but since I can't modify tests, I'll write a temporary test.Assistant
Let me see which test failed.
Assistant
The code works for the real use cases (executor/worker/scheduler loggers). The
logFlowMdctest failed only because my test used a logger"test.flow"whose level isn't configured to INFO. Let me fix the test to use a properly configured logger like the originalLogsTestdoes.Assistant
All 5 verification tests pass, confirming:
- MDC context is applied for execution logs (
tenantId,namespace,flowId,executionId) - MDC context is applied for task run logs (adds
taskId,taskRunId) - MDC context is applied for trigger logs (adds
triggerId) - MDC context is applied for flow logs
- MDC context does not leak after the log call
Now let me run the existing
LogsTestto make sure I didn't break anything.- MDC context is applied for execution logs (
Assistant
The existing
LogsTestpasses. Let me check if there are other places that log execution context using the[tenant:prefix pattern that might also need the MDC treatment, to ensure full consistency.Assistant
That
FlowServicedebug log is a minor edge case not part of the reported issue (which is specifically about the centralLogs/LogServiceserver-logging utility). My fix targets the centralLogs.javautility used for all task/flow/execution/trigger server logs.Let me remove the temporary test file and verify the final state of my changes.
Assistant
The diff looks clean. Let me compile the dependent modules that use
Logs(worker, executor, scheduler, webserver) to ensure nothing breaks.Assistant
All modules compile successfully (only pre-existing deprecation warnings). Let me run the full existing
LogsTestone more time along with a broader check of related test packages to confirm no regressions.Assistant
All tests pass. Let me verify there are no stray files left and confirm the final git status.
Assistant
The only change is to
Logs.java, and the temporary test file has been removed. Let me do a final review of the complete file.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.