Git Product home page Git Product logo

brendanobra / akka-contextual-actor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ktonga/akka-contextual-actor

0.0 0.0 0.0 143 KB

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.

License: MIT License

Scala 100.00%

akka-contextual-actor's Introduction

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

akka-contextual-actor's People

Contributors

ktonga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.