Skip to content

Commit

Permalink
Merge pull request #5 from gvolpe/upgrade-http-app
Browse files Browse the repository at this point in the history
Adapting middleware to work with HttpApp instead of HttpRoutes
  • Loading branch information
gvolpe authored Jul 30, 2018
2 parents 5480ca3 + 4f5081e commit e0ae5ae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name := """https-tracer-root"""

organization in ThisBuild := "com.github.gvolpe"

version in ThisBuild := "1.0-M1"
version in ThisBuild := "1.0-M2"

crossScalaVersions in ThisBuild := Seq("2.11.12", "2.12.6")

Expand Down
22 changes: 11 additions & 11 deletions core/src/main/scala/com/github/gvolpe/tracer/Tracer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package com.github.gvolpe.tracer

import cats.Applicative
import cats.data.{Kleisli, OptionT}
import cats.data.Kleisli
import cats.effect.Sync
import cats.syntax.all._
import com.gilt.timeuuid.TimeUuid
import org.http4s.syntax.StringSyntax
import org.http4s.{Header, HttpRoutes, Request, Response}
import org.http4s.{Header, HttpApp, Request}

/**
* `org.http4s.server.HttpMiddleware` that either tries to get a Trace-Id from the headers or otherwise
Expand Down Expand Up @@ -50,22 +50,22 @@ object Tracer extends StringSyntax {
type KFX[F[_], A] = Kleisli[F, TraceId, A]

// format: off
def apply[F[_]](service: HttpRoutes[F], headerName: String = DefaultTraceIdHeader)
(implicit F: Sync[F], L: TracerLog[KFX[F, ?]]): HttpRoutes[F] =
Kleisli[OptionT[F, ?], Request[F], Response[F]] { req =>
def apply[F[_]](http: HttpApp[F], headerName: String = DefaultTraceIdHeader)
(implicit F: Sync[F], L: TracerLog[KFX[F, ?]]): HttpApp[F] =
Kleisli { req =>
val createId: F[(Request[F], TraceId)] =
for {
id <- F.delay(TraceId(TimeUuid().toString))
tr <- F.delay(req.putHeaders(Header(TraceIdHeader, id.value)))
} yield (tr, id)

for {
_ <- OptionT.liftF(F.delay(TraceIdHeader = headerName))
mi <- OptionT.liftF(getTraceId(req))
(tr, id) <- mi.fold(OptionT.liftF(createId)){ id => OptionT.liftF((req, id).pure[F]) }
_ <- OptionT.liftF(L.info[Tracer.type](s"$req").run(id))
rs <- service(tr).map(_.putHeaders(Header(TraceIdHeader, id.value)))
_ <- OptionT.liftF(L.info[Tracer.type](s"$rs").run(id))
_ <- F.delay(TraceIdHeader = headerName)
mi <- getTraceId(req)
(tr, id) <- mi.fold(createId){ id => (req, id).pure[F] }
_ <- L.info[Tracer.type](s"$req").run(id)
rs <- http(tr).map(_.putHeaders(Header(TraceIdHeader, id.value)))
_ <- L.info[Tracer.type](s"$rs").run(id)
} yield rs
}

Expand Down
7 changes: 4 additions & 3 deletions examples/src/main/scala/com/github/gvolpe/tracer/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import com.github.gvolpe.tracer.interpreter.UserTracerInterpreter
import com.github.gvolpe.tracer.instances.tracerlog._
import com.github.gvolpe.tracer.repository.UserTracerRepository
import com.github.gvolpe.tracer.repository.algebra.UserRepository
import org.http4s.HttpRoutes
import org.http4s.{HttpApp, HttpRoutes}
import org.http4s.implicits._

class Module[F[_]: Sync] {

Expand All @@ -37,7 +38,7 @@ class Module[F[_]: Sync] {
private val httpRoutes: HttpRoutes[F] =
new UserRoutes[F](service).routes

val routes: HttpRoutes[F] =
Tracer(httpRoutes, headerName = "Flow-Id") // Header name is optional, default to "Trace-Id"
val httpApp: HttpApp[F] =
Tracer(httpRoutes.orNotFound, headerName = "Flow-Id") // Header name is optional, default to "Trace-Id"

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.gvolpe.tracer

import cats.data.OptionT
import cats.effect.{ConcurrentEffect, IO}
import fs2.StreamApp.ExitCode
import fs2.{Stream, StreamApp}
Expand All @@ -32,7 +33,7 @@ class HttpServer[F[_]: ConcurrentEffect] extends StreamApp[F] {
ctx <- Stream(new Module[F])
exitCode <- BlazeBuilder[F]
.bindHttp(8080, "0.0.0.0")
.mountService(ctx.routes)
.mountService(ctx.httpApp.mapF(OptionT.liftF(_)))
.serve
} yield exitCode

Expand Down
7 changes: 4 additions & 3 deletions site/src/main/tut/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class UserProgram[F[_]](repo: UserRepository[F])(implicit F: MonadError[F, Throw

#### User Tracer Interpreter

And an `interpreter` that just adds the tracing log part to it, by following a `tagless final` design:
And an `interpreter` that just adds the tracing log part to it, by following a `tagless final` encoding:

```tut:book:silent
import com.github.gvolpe.tracer.Tracer.KFX
Expand Down Expand Up @@ -80,6 +80,7 @@ import io.circe.generic.auto._
import io.circe.generic.extras.encoding.UnwrappedEncoder
import org.http4s._
import org.http4s.circe._
import org.http4s.implicits._
class UserRoutes[F[_]: Sync](userService: UserAlgebra[KFX[F, ?]]) extends Http4sTracerDsl[F] {
Expand Down Expand Up @@ -107,8 +108,8 @@ val userService: UserAlgebra[KFX[IO, ?]] = null
```tut:book:silent
import com.github.gvolpe.tracer.instances.tracerlog._
val userRoutes: HttpService[IO] = new UserRoutes[IO](userService).routes
val routes: HttpService[IO] = Tracer(userRoutes, headerName = "MyAppId") // Customizable Header name, default "Trace-Id"
val routes: HttpRoutes[IO] = new UserRoutes[IO](userService).routes
val httpApp: HttpApp[IO] = Tracer(routes.orNotFound, headerName = "MyAppId") // Customizable Header name, default "Trace-Id"
```

Notice that an implicit instance of `TracerLog[F]` is needed for `Tracer.apply`. You can either provide your own or just use the default one that uses a `org.slf4j.Logger` instance to log the trace of your application.
Expand Down

0 comments on commit e0ae5ae

Please sign in to comment.