From e8e62e2ca2a0747a2e8fa6b54d205badae988058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Andr=C3=A9n?= Date: Mon, 4 Jan 2021 15:04:14 +0100 Subject: [PATCH] Make sure static mdc from withMdc set during actor startup (#29894) * Make sure static mdc from withMdc set during actor startup #29893 * Actually assert mdc is included --- .../akka/actor/typed/scaladsl/ActorLoggingSpec.scala | 6 ++++-- .../actor/typed/internal/WithMdcBehaviorInterceptor.scala | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala index 1cef1367b3..4d37edee07 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala @@ -329,7 +329,7 @@ class ActorLoggingSpec extends ScalaTestWithActorTestKit(""" "provide the MDC values in the log" in { val behaviors = Behaviors.withMdc[Protocol]( Map("static" -> "1"), - // FIXME why u no infer the type here Scala?? + // why u no infer the type here Scala?? (message: Protocol) => if (message.transactionId == 1) Map("txId" -> message.transactionId.toString, "first" -> "true") @@ -348,7 +348,9 @@ class ActorLoggingSpec extends ScalaTestWithActorTestKit(""" .info("Starting") // not counting for example "akkaSource", but it shouldn't have any other entries .withCustom(logEvent => - logEvent.mdc.keysIterator.forall(entry => entry.startsWith("akka") || entry == "sourceActorSystem")) + logEvent.mdc.keysIterator.forall(entry => + entry.startsWith("akka") || entry == "sourceActorSystem" || entry == "static") && + logEvent.mdc("static") == "1") .expect { spawn(behaviors) } diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/WithMdcBehaviorInterceptor.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/WithMdcBehaviorInterceptor.scala index 76effd7d69..633136318a 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/WithMdcBehaviorInterceptor.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/WithMdcBehaviorInterceptor.scala @@ -62,8 +62,12 @@ import akka.annotation.InternalApi case b => b } } - - loop(target.start(ctx)) + try { + setMdcValues(Map.empty) + loop(target.start(ctx)) + } finally { + MDC.clear() + } } // in the normal case, a new withMDC replaces the previous one