trace4cats / trace4cats-http4s Goto Github PK
View Code? Open in Web Editor NEWIntegration for Trace4Cats and Http4s
License: MIT License
Integration for Trace4Cats and Http4s
License: MIT License
I wrote a simple http4s using both of trace4cats
and cats.effect.kernel.Ref
, but I found the type parameter challenging to give.
package example
import cats.data.Kleisli
import cats.effect.kernel.Ref
import cats.effect.{Async, ExitCode, IO, IOApp, Resource}
import cats.syntax.all._
import com.comcast.ip4s._
import fs2.Stream
import org.http4s.Http
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.implicits._
import org.http4s.server.middleware.{GZip, Logger}
import org.http4s.server.{Router, Server}
import sttp.tapir.server.http4s.{Http4sServerInterpreter, Http4sServerOptions}
import trace4cats._
import trace4cats.http4s.common.Http4sRequestFilter
import trace4cats.http4s.server.syntax._
import trace4cats.jaeger.JaegerSpanCompleter
object Main extends IOApp {
type G[x] = Kleisli[IO, Span[IO], x]
def entryPoint[F[_]: Async](process: TraceProcess): Resource[F, EntryPoint[F]] =
JaegerSpanCompleter[F](process = process).map { completer =>
EntryPoint[F](SpanSampler.always, completer)
}
override def run(args: List[String]): IO[ExitCode] = {
for {
counterRef <- Ref[IO].of(0)
serverOptions: Http4sServerOptions[G] = Http4sServerOptions.default[G]
serverLogic: ServerLogic[G] = new ServerLogic[G](counterRef)
resource: Resource[IO, Server] = for {
ep <- entryPoint[IO](TraceProcess("http4s-demo"))
r: HttpRoutes[G] = Http4sServerInterpreter[G](serverOptions).toRoutes(serverLogic.all)
routes = r.inject(ep, requestFilter = Http4sRequestFilter.kubernetesPrometheus)
app: Http[IO, IO] = Router("/" -> routes).orNotFound
finalApp = Logger.httpApp(logHeaders = true, logBody = false)(GZip(app))
s <- EmberServerBuilder.default[IO].withHost(ipv4"0.0.0.0")
.withPort(port"8080").withHttpApp(finalApp).build >> Resource.eval(Async[IO].never)
} yield s
s <- Stream.resource(resource).drain.compile.drain.as(ExitCode.Success)
} yield s
}
}
Ref
needs IO
, but r
needs G
.
Http4s Client spans only have a couple attributes currently set on them.
It would be great to add a few more, such as:
Moreover, the span name itself does not adhere to OpenTelemetry's recommended scheme to ensure low cardinality:
There is a possible minimal implementation of tracing for a http4s client, which is to lift the context to one allowing tracing, and also forwards the tracing headers; but otherwise does not create a new span or decorate the current span.
Reasons?
Trace[F].span
etc. inside your application code. It's basically a nice building block for custom client tracing schemes.Example code - doesn't have a Ctx
parameter, but shows the general idea:
def liftTrace[F[_]: MonadCancelThrow, G[_]: MonadCancelThrow](
client: Client[F],
toHeaders: ToHeaders = ToHeaders.standard
)(implicit P: Provide[F, G, Span[F]]): Client[G] =
Client { (request: Request[G]) =>
Resource.eval(P.ask).flatMap { span =>
val traceHeaders = toHeaders
.fromContext(span.context)
.values
.map(Header.Raw.apply)
.toList
client
.run(
request
.transformHeaders(_ ++ Headers(traceHeaders))
.mapK(P.provideK(span))
)
.mapK(P.liftK)
.map(_.mapK(P.liftK))
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.