Skip to content

Ensure JobClusterActor's logs always contain the clusterName - #870

Open
sileshidev-lab wants to merge 1 commit into
Netflix:masterfrom
sileshidev-lab:jobclusteractor-log-cluster-name
Open

Ensure JobClusterActor's logs always contain the clusterName#870
sileshidev-lab wants to merge 1 commit into
Netflix:masterfrom
sileshidev-lab:jobclusteractor-log-cluster-name

Conversation

@sileshidev-lab

Copy link
Copy Markdown

Summary

Fixes #385 — log lines emitted from JobClusterActor didn't consistently include the cluster name, making it hard to trace a specific cluster's behavior in logs (~256 log call sites in this class, added ad-hoc over time, many without name).

Approach: rather than manually retrofitting every existing call site (tedious, easy to miss, and doesn't guard against new call sites forgetting it later), this overrides aroundReceive — the single method every message this actor processes passes through — to set the cluster name in SLF4J's MDC before delegating to the actual handler, and clear it in a finally block afterward. This makes the cluster name automatically available to every log line emitted anywhere during that message's handling, for any logging backend that renders MDC values (structured/JSON encoders, or a pattern layout with %X{clusterName}).

Verified AbstractActorWithTimers.aroundReceive (via the Timers trait) already delegates further up the chain to AbstractActor.aroundReceive, so calling super.aroundReceive(receive, msg) from the override preserves existing timer bookkeeping — confirmed by inspecting the actual Akka 2.6.15 akka-actor jar's bytecode rather than assuming the API shape.

Test plan

  • New test testAroundReceivePutsClusterNameInMdcDuringMessageHandlingAndClearsItAfter in JobClusterAkkaTest. Since MDC is thread-local, it exercises aroundReceive directly via TestActorRef (bypassing the mailbox/dispatcher) with a receive handler that captures the MDC value synchronously during "processing," then asserts it matches the cluster name and is cleared afterward.
  • akkaTest task (this class's actual test task — this project splits *AkkaTest classes into a separate Gradle task from test): 58 passed (57 pre-existing + 1 new), 0 regressions
  • test task for the module: 358 passed, 0 regressions
  • compileJava clean, no new warnings on the modified file (verified against a stashed baseline)

Log lines emitted from JobClusterActor didn't consistently include
the cluster name, making it hard to trace behavior of a specific
cluster in the logs. Manually passing `name` into all ~256 log call
sites in this class would be tedious and error-prone (easy to miss
new call sites going forward).

Instead, override aroundReceive (which every message this actor
processes passes through) to set the cluster name in SLF4J's MDC
before delegating to the actual message handler, and clear it
afterward. This makes the cluster name automatically available to
every log line emitted while handling a message, for logging
backends that render MDC values (structured/JSON encoders, or a
pattern layout with %X{clusterName}).

Added a test that exercises aroundReceive directly via TestActorRef
and asserts MDC contains the cluster name during message handling
and is cleared afterward.

Fixes Netflix#385
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ensure JobClusterActor's logs always contain the clusterName for easy debugging

1 participant