0

I am trying to do MDC propagation with Kamon like shown in this documentation But it does not seem to work like they say

Play framework - 2.5

kamon-core - 0.6.2

kamon-play-25 - 0.6.2

My logback pattern:

<pattern>%d{HH:mm:ss.SSS} [%thread] [%level] [%traceToken]- %logger{36}\(%L\) %X{X-ApplicationId} - %message%n%xException</pattern>

I have created a filter:

class AccessLoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter with LazyLogging {
val ApplicationIdKey = AvailableToMdc("X-ApplicationId")
def apply(next: (RequestHeader) => Future[Result])(request: RequestHeader): Future[Result] = {
TraceLocal.storeForMdc("X-ApplicationId", request.id.toString)
logger.error("first Location")
withMdc {
  logger.error("Second location")
  next(request)
}}}

And added it like so:

class MyFilters @Inject() (accessLoggingFilter: AccessLoggingFilter) extends DefaultHttpFilters(accessLoggingFilter)

Now when i do an http call to the server i get the following output:

c.v.i.utils.AccessLoggingFilter(24)  - first Location
c.v.i.utils.AccessLoggingFilter(26) 1 - Second location

And all log prints afterwards do not show the '1' X-ApplicationId

Cant figure out what i am doing wrong.

Gleeb
  • 10,773
  • 26
  • 92
  • 135

1 Answers1

1

Here a complete(almost) example:

build.sbt:

name := "kamon-play-example"

version := "1.0"

scalaVersion := "2.11.7"

val kamonVersion = "0.6.2"

val resolutionRepos = Seq("Kamon Repository Snapshots" at "http://snapshots.kamon.io")

val dependencies = Seq(
  "io.kamon"    %% "kamon-play-25"        % kamonVersion,
  "io.kamon"    %% "kamon-log-reporter"   % kamonVersion
)

lazy val root = (project in file(".")).enablePlugins(PlayScala)
                                      .settings(resolvers ++= resolutionRepos)
                                      .settings(libraryDependencies ++= dependencies)

basic filter:

class TraceLocalFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter {
  val logger = Logger(this.getClass)
  val TraceLocalStorageKey = "MyTraceLocalStorageKey"

  val userAgentHeader = "User-Agent"

  //this value will be available in the MDC at the moment to call  to Logger.*()s
  val UserAgentHeaderAvailableToMDC = AvailableToMdc(userAgentHeader)

  override def apply(next: (RequestHeader) ⇒ Future[Result])(header: RequestHeader): Future[Result] = {

    def onResult(result:Result) = {
      val traceLocalContainer = TraceLocal.retrieve(TraceLocalKey).getOrElse(TraceLocalContainer("unknown","unknown"))
      result.withHeaders(TraceLocalStorageKey -> traceLocalContainer.traceToken)
    }

    //update the TraceLocalStorage
    TraceLocal.store(TraceLocalKey)(TraceLocalContainer(header.headers.get(TraceLocalStorageKey).getOrElse("unknown"), "unknown"))
    TraceLocal.store(UserAgentHeaderAvailableToMDC)(header.headers.get(userAgentHeader).getOrElse("unknown"))

    //call the action
    next(header).map(onResult)
  }
}

we need add the filter:

class Filters @Inject() (traceLocalFilter: TraceLocalFilter)  extends HttpFilters {
  val filters = Seq(traceLocalFilter)
}

a really simple controller and action:

class KamonPlayExample @Inject() (kamon: Kamon) extends Controller {

  def sayHello = Action.async {
    Future {
      logger.info("Say hello to Kamon")
      Ok("Say hello to Kamon")
    }
  }
}

in logback.xml add the following pattern:

    <pattern>%date{HH:mm:ss.SSS} %-5level [%traceToken][%X{User-Agent}] [%thread] %logger{55} - %msg%n</pattern>

add the sbt-aspectj-runner plugin in order to run the application with Aspectjweaver in DEV mode:

addSbtPlugin("io.kamon" % "aspectj-play-runner" % "0.1.3")

run the application with aspectj-runner:run and make some curls:

  curl -i -H 'X-Trace-Token:kamon-test' -H 'User-Agent:Super-User-Agent' -X GET "http://localhost:9000/helloKamon"

  curl -i -H 'X-Trace-Token:kamon-test'-X GET "http://localhost:9000/helloKamon"

in the console:

15:09:16.027 INFO  [kamon-test][Super-User-Agent] [application-akka.actor.default-dispatcher-8] controllers.KamonPlayExample - Say hello to Kamon

15:09:24.034 INFO  [kamon-test][curl/7.47.1] [application-akka.actor.default-dispatcher-8] controllers.KamonPlayExample - Say hello to Kamon

hope you help.

Diego Parra
  • 131
  • 3