All Projects → ktonga → akka-contextual-actor

ktonga / akka-contextual-actor

Licence: MIT license
A really small library (just a few classes) which lets you trace your actors messages transparently propagating a common context together with your messages and adding the specified values to the MDC of the underlying logging framework.

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to akka-contextual-actor

endless
Scala library to describe sharded and event sourced entities using tagless-final algebras
Stars: ✭ 70 (+311.76%)
Mutual labels:  actors, akka, akka-actors
Akkadotnet Code Samples
Akka.NET professional reference code samples
Stars: ✭ 451 (+2552.94%)
Mutual labels:  actors, akka
Akkatecture
a cqrs and event sourcing framework for dotnet core using akka.net
Stars: ✭ 414 (+2335.29%)
Mutual labels:  actors, akka
Akka.Persistence.MongoDB
MongoDB support for Akka.Persistence
Stars: ✭ 30 (+76.47%)
Mutual labels:  actors, akka
protoactor-go
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
Stars: ✭ 4,138 (+24241.18%)
Mutual labels:  actors, akka
Robots
Actor system for Rust
Stars: ✭ 294 (+1629.41%)
Mutual labels:  actors, akka
Nact
nact ⇒ node.js + actors ⇒ your services have never been so µ
Stars: ✭ 848 (+4888.24%)
Mutual labels:  actors, akka
Akka
Examples and explanations of how Akka toolkit works
Stars: ✭ 20 (+17.65%)
Mutual labels:  akka, akka-actors
Akka Monitoring
Monitoring system instrumentation for Akka.NET actor systems
Stars: ✭ 105 (+517.65%)
Mutual labels:  actors, akka
Actors
Evaluation of API and performance of different actor libraries
Stars: ✭ 125 (+635.29%)
Mutual labels:  actors, akka
Tacks
Real-time multiplayer sailing game, in your browser
Stars: ✭ 134 (+688.24%)
Mutual labels:  actors, akka
akka-cqrs-activator
Issue tracker PoC application written in Scala (Akka) and JavaScript (React) that demonstrates event sourcing and CQRS
Stars: ✭ 33 (+94.12%)
Mutual labels:  actors, akka
nact
nact ⇒ node.js + actors ⇒ your services have never been so µ
Stars: ✭ 1,003 (+5800%)
Mutual labels:  actors, akka
Protoactor Go
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
Stars: ✭ 3,934 (+23041.18%)
Mutual labels:  actors, akka
MuezzinAPI
A web server application for Islamic prayer times
Stars: ✭ 33 (+94.12%)
Mutual labels:  actors, akka
Akka Essentials
Java/Scala Examples from the book - Akka Essentials
Stars: ✭ 700 (+4017.65%)
Mutual labels:  actors, akka
khermes
A distributed fake data generator based in Akka.
Stars: ✭ 94 (+452.94%)
Mutual labels:  akka, akka-actors
Protoactor Dotnet
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
Stars: ✭ 1,070 (+6194.12%)
Mutual labels:  actors, akka
Akka Guide
🌴 A chinese guide of Akka, based on Java.
Stars: ✭ 217 (+1176.47%)
Mutual labels:  actors, akka
protoactor-python
Proto Actor - Ultra fast distributed actors
Stars: ✭ 78 (+358.82%)
Mutual labels:  actors, akka

akka-contextual-actor

A really small library (just a few classes) which lets you trace your actors messages transparently propagating a common context together with your messages and adding the specified values to the MDC of the underling logging framework.

Usage

Download the latest release and copy it into your project's lib folder (I promise to publish it in a repository soon) Also you will need to add the Typesafe Snapshots Repository since it depends on new stuff available in Akka 2.3-M2

resolvers += "Typesafe Snapshot Repository" at "http://repo.typesafe.com/typesafe/snapshots/"

libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.3-M2",
    "com.typesafe.akka"   %% "akka-slf4j"       % "2.3-M2",
    "ch.qos.logback"      % "logback-classic"  % "1.0.13"
)

Getting started

ActorRef operations

In order to enable tracing within an Actor you just need to mixin with TracingActor You will now have the contextualized operations analogous to the regular ActorRef operations

  • tellWithCxt or !+
  • forwardWithCxt or >+

Any message you tell or forward from a TracingActor to another TracingActor with be wrapped with the current message context implicitly picked up and unwrapped for processing it in the target actor.

class MyTracedActor extends Actor with TracingActor {
  ...
    anotherTracingActorRef !+ SomeMessage
    anotherTracingActorRef >+ ForwardedMessage
  ...
}

It is also possible to enable tracing outside actors (a Web Controller or Main object for instance), you need to import com.github.ktonga.akka.contextual.actor.Implicits._ and the ActorRefs will get contextualized operations. Then you have to create an implicit context which will be picked up.

class MyController {
  import Implicits._
  ...
  def processRequest(params: Params) = {
    implicit val ctx: Option[MsgCtx] = Some(MsgCtx(Map("requestId" -> params.reqId)))
    businessTracingActorRef !+ StartRequest
  }
}

Akka patterns

In a similar way to regular operations, you have the contextualized version for the common Akka patterns. You can import ask or pipe from package com.github.ktonga.akka.contextual.pattern, and you will have the following pattern operators:

  • askWithCxt or ?+
  • pipeWithCxt or |+

You will be able to propagate the message context to collaborating actors using the already known patterns.

Logging with MDC

Finally you will need to configure SLF4J logging

Add the dependency

"com.typesafe.akka"   %% "akka-slf4j"       % "2.3-M2",

Add the logger in your application.config

akka {
    loggers = ["akka.event.slf4j.Slf4jLogger"]
}

Now you can include message context attributes in your appender pattern

<pattern>%X{akkaTimestamp} [%X{sourceThread}] [%X{requestId}] %-5level %X{akkaSource} - %msg%n</pattern>

See Akka logging documentations for further information http://doc.akka.io/docs/akka/snapshot/scala/logging.html#SLF4J

Sample Application

Try it out with the sample application

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].