Add UID to Slf4j MDC (#31039)
This UID can be used to distinguish logging from different ActorSystem incarnations even when they have the same host and port. This UID already existed in akka-remote, but to avoid introducing a dependency between akka-slf4j and akka-remote this change pulls it up to the ActorSystem.
This commit is contained in:
parent
4fb7bd9bfa
commit
4292a81138
5 changed files with 16 additions and 7 deletions
|
|
@ -0,0 +1,2 @@
|
|||
# marked DoNotInherit, so fine to add methods
|
||||
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.actor.ExtendedActorSystem.uid")
|
||||
|
|
@ -775,6 +775,11 @@ abstract class ExtendedActorSystem extends ActorSystem {
|
|||
*/
|
||||
private[akka] def printTree: String
|
||||
|
||||
/**
|
||||
* INTERNAL API: random uid assigned at ActorSystem startup
|
||||
*/
|
||||
@InternalApi private[akka] def uid: Long
|
||||
|
||||
/**
|
||||
* INTERNAL API: final step of `terminate()`
|
||||
*/
|
||||
|
|
@ -800,6 +805,8 @@ private[akka] class ActorSystemImpl(
|
|||
setup: ActorSystemSetup)
|
||||
extends ExtendedActorSystem {
|
||||
|
||||
val uid: Long = ThreadLocalRandom.current.nextLong()
|
||||
|
||||
if (!name.matches("""^[a-zA-Z0-9][a-zA-Z0-9-_]*$"""))
|
||||
throw new IllegalArgumentException(
|
||||
"invalid ActorSystem name [" + name +
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
|
||||
package akka.remote
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import akka.actor.ClassicActorSystemProvider
|
||||
import akka.actor.ExtendedActorSystem
|
||||
|
|
@ -35,13 +33,11 @@ class AddressUidExtension(val system: ExtendedActorSystem) extends Extension {
|
|||
|
||||
private def arteryEnabled = system.provider.asInstanceOf[RemoteActorRefProvider].remoteSettings.Artery.Enabled
|
||||
|
||||
val longAddressUid: Long = {
|
||||
val tlr = ThreadLocalRandom.current
|
||||
if (arteryEnabled) tlr.nextLong()
|
||||
val longAddressUid: Long =
|
||||
if (arteryEnabled) system.uid
|
||||
// with the old remoting we need to make toInt.toLong return the same number
|
||||
// to keep wire compatibility
|
||||
else tlr.nextInt().toLong
|
||||
}
|
||||
else system.uid.toInt.toLong
|
||||
|
||||
// private because GenJavaDoc fails on deprecated annotated lazy val
|
||||
private lazy val _addressUid: Int = {
|
||||
|
|
|
|||
|
|
@ -60,8 +60,10 @@ class Slf4jLogger extends Actor with SLF4JLogging with RequiresMessageQueue[Logg
|
|||
val mdcAkkaSourceAttributeName = "akkaSource"
|
||||
val mdcAkkaTimestamp = "akkaTimestamp"
|
||||
val mdcAkkaAddressAttributeName = "akkaAddress"
|
||||
val mdcAkkaUidAttributeName = "akkaUid"
|
||||
|
||||
private def akkaAddress = context.system.asInstanceOf[ExtendedActorSystem].provider.addressString
|
||||
private val akkaUid: String = context.system.asInstanceOf[ExtendedActorSystem].uid.toString
|
||||
|
||||
def receive = {
|
||||
|
||||
|
|
@ -122,6 +124,7 @@ class Slf4jLogger extends Actor with SLF4JLogging with RequiresMessageQueue[Logg
|
|||
MDC.put(mdcAkkaTimestamp, formatTimestamp(logEvent.timestamp))
|
||||
MDC.put(mdcActorSystemAttributeName, context.system.name)
|
||||
MDC.put(mdcAkkaAddressAttributeName, akkaAddress)
|
||||
MDC.put(mdcAkkaUidAttributeName, akkaUid)
|
||||
logEvent.mdc.foreach { case (k, v) => MDC.put(k, String.valueOf(v)) }
|
||||
|
||||
try logStatement
|
||||
|
|
|
|||
|
|
@ -93,6 +93,7 @@ class Slf4jLoggerSpec extends AkkaSpec(Slf4jLoggerSpec.config) with BeforeAndAft
|
|||
val s = outputString
|
||||
s should include("akkaSource=akka://Slf4jLoggerSpec/user/logProducer")
|
||||
s should include("akkaAddress=akka://Slf4jLoggerSpec")
|
||||
s should include("akkaUid=")
|
||||
s should include("level=[ERROR]")
|
||||
s should include("logger=[akka.event.slf4j.Slf4jLoggerSpec$LogProducer]")
|
||||
(s should include).regex(sourceThreadRegex)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue