kkrs / http4s-tracer

End to end tracing system for Http4s

Home Page:https://gvolpe.github.io/http4s-tracer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

http4s-tracer

Build Status Gitter Chat Maven Central

It provides an HttpMiddleware that adds a Trace-Id header (name can be customized) with a unique UUID value and gives you an implicit TracerLog for any F[_]: Sync] that also logs the http request and http response with it.

Quite useful to trace the flow of your application starting out at each request. For example, given a UsersHttpRoutes, UserAlgebra and UserRepository you'll get an activity log like the following when trying to create a user:

4:13:48.985 [scala-execution-context-global-17] INFO com.github.gvolpe.tracer.Tracer$ - TraceId(02594e59-4b21-4d0a-aad5-5866a632fbb5) >> Request(method=POST, uri=/users, headers=Headers(HOST: localhost:8080, content-type: application/json, content-length: 30))
14:13:49.282 [scala-execution-context-global-17] INFO com.github.gvolpe.tracer.algebra$UserAlgebra - TraceId(02594e59-4b21-4d0a-aad5-5866a632fbb5) >> About to persist user: modersky
14:13:49.290 [scala-execution-context-global-17] INFO com.github.gvolpe.tracer.repository.algebra$UserRepository - TraceId(02594e59-4b21-4d0a-aad5-5866a632fbb5) >> Find user by username: modersky
14:13:49.298 [scala-execution-context-global-17] INFO com.github.gvolpe.tracer.repository.algebra$UserRepository - TraceId(02594e59-4b21-4d0a-aad5-5866a632fbb5) >> Persisting user: modersky
14:13:49.315 [scala-execution-context-global-17] INFO com.github.gvolpe.tracer.Tracer$ - TraceId(02594e59-4b21-4d0a-aad5-5866a632fbb5) >> Response(status=201, headers=Headers(Content-Length: 0, Trace-Id: 02594e59-4b21-4d0a-aad5-5866a632fbb5))

In a normal application, you will have thousands of requests and tracing the call chain in a failure scenario will be invaluable.

Dependencies

Add this to your build.sbt:

libraryDependencies += "com.github.gvolpe" %% "http4s-tracer" % Version

http4s-tracer has the following dependencies:

Dependency Version
cats 1.1.1
cats-effect 0.10.1
fs2 0.10.4
gfc-timeuuid 0.0.8
http4s 0.18.12

Credits

This is an idea I first heard from Eric Torreborre, also described in his Haskell setup by defining a newtype RIO (a.k.a. Reader IO Monad) and inspired bt the RIO Monad described by Michael Snoyman the year before. However, this implementation is adapted to work nicely with Http4s while abstracting over the effect type using Cats Effect and where the main type is defined as type KFX[F[_], A] = Kleisli[F, TraceId, A], a bit simpler than how RIO is actually defined because it's also doing less (just carrying a TraceId around).

About

End to end tracing system for Http4s

https://gvolpe.github.io/http4s-tracer/


Languages

Language:Scala 100.0%