Code Monkey home page Code Monkey logo

trace4cats-http4s's Introduction

Trace4Cats

GitHub Workflow Status GitHub stable release GitHub latest release Maven Central early release Join the chat at https://gitter.im/trace4cats/community Scala Steward badge

⚠️ If you are upgrading to 0.14.0 please read the migration guide.

Yet another distributed tracing system, this time just for Scala. Heavily relies upon Cats and Cats Effect.

Compatible with OpenTelemetry and Jaeger, based on, and interoperates with Natchez.

Obligatory XKCD

For release information and changes see the releases page.

Motivation

It increasingly seems that Java tracing libraries are dependent on gRPC, which usually brings along lots of other dependencies. You may find Trace4Cats useful if you want to...

  • Reduce the number of dependencies in your application
  • Resolve a dependency conflict caused by a tracing implementation
  • Create a native-image using GraalVM

Highlights

Trace4Cats supports publishing spans to the following systems:

Instrumentation for trace propagation and continuation is available for the following libraries:

Unlike other tracing libraries, trace attributes are lazily evaluated. If a span is not sampled, no computation associated with calculating attribute values will be performed.

More information on how to use these can be found in the examples documentation.

Quickstart

For more see the documentation and more advanced examples.

Add the following dependencies to your build.sbt:

"io.janstenpickle" %% "trace4cats-core" % "0.14.0"
"io.janstenpickle" %% "trace4cats-avro-exporter" % "0.14.0"

Then run the collector in span logging mode:

echo "log-spans: true" > /tmp/collector.yaml
docker run -p7777:7777 -p7777:7777/udp -it \
  -v /tmp/collector.yaml:/tmp/collector.yaml \
  janstenpickle/trace4cats-collector-lite:0.14.0 \
  --config-file=/tmp/collector.yaml

Finally, run the following code to export some spans to the collector:

import cats.Monad
import cats.data.Kleisli
import cats.effect._
import cats.effect.std.Console
import cats.implicits._
import trace4cats._
import trace4cats.avro.AvroSpanCompleter

import scala.concurrent.duration._

object Trace4CatsQuickStart extends IOApp.Simple {
  def entryPoint[F[_]: Async](process: TraceProcess): Resource[F, EntryPoint[F]] =
    AvroSpanCompleter.udp[F](process, config = CompleterConfig(batchTimeout = 50.millis)).map { completer =>
      EntryPoint[F](SpanSampler.always[F], completer)
    }

  def runF[F[_]: Monad: Console: Trace]: F[Unit] =
    for {
      _ <- Trace[F].span("span1")(Console[F].println("trace this operation"))
      _ <- Trace[F].span("span2", SpanKind.Client)(Console[F].println("send some request"))
      _ <- Trace[F].span("span3", SpanKind.Client)(
        Trace[F].putAll("attribute1" -> "test", "attribute2" -> 200) >>
          Trace[F].setStatus(SpanStatus.Cancelled)
      )
    } yield ()

  def run: IO[Unit] =
    entryPoint[IO](TraceProcess("trace4cats")).use { ep =>
      ep.root("this is the root span").use { span =>
        runF[Kleisli[IO, Span[IO], *]].run(span)
      }
    }
}

Migrating to 0.14.0

Version 0.14.0 introduced a reworked module and package structure that reduced the number of dependencies and imports required to get started quickly. Effectively import trace4cats._ is all you should need to import throughout most of your codebase.

See the migration guide for information on how to migrate.

Repositories

Trace4Cats is separated into a few repositories:

Components

Trace4Cats is made up as both a set of libraries for integration in applications and standalone processes. For information on the libraries and interfaces see the design documentation.

The standalone components are the agent and the collector. To see how they work together, see the topologies documentation, for information on configuring and running the agent and collector see the components documentation.

The source code for these components is located in the trace4cats-components repository.

Documentation

SBT Dependencies

To use Trace4Cats within your application add the dependencies listed below as needed:

"io.janstenpickle" %% "trace4cats-core" % "0.14.0"
"io.janstenpickle" %% "trace4cats-rate-sampling" % "0.14.0"
"io.janstenpickle" %% "trace4cats-fs2" % "0.14.0"
"io.janstenpickle" %% "trace4cats-http4s-client" % "0.14.0"
"io.janstenpickle" %% "trace4cats-http4s-server" % "0.14.0"
"io.janstenpickle" %% "trace4cats-sttp-client3" % "0.14.0"
"io.janstenpickle" %% "trace4cats-sttp-tapir" % "0.14.0"
"io.janstenpickle" %% "trace4cats-natchez" % "0.14.0"
"io.janstenpickle" %% "trace4cats-avro-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-avro-kafka-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-avro-kafka-consumer" % "0.14.0"
"io.janstenpickle" %% "trace4cats-jaeger-thrift-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-opentelemetry-otlp-grpc-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-opentelemetry-otlp-http-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-opentelemetry-jaeger-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-stackdriver-grpc-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-stackdriver-http-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-datadog-http-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-newrelic-http-exporter" % "0.14.0"
"io.janstenpickle" %% "trace4cats-zipkin-http-exporter" % "0.14.0"

native-image Compatibility

The following span completers have been found to be compatible with native-image:

Contributing

This project supports the Scala Code of Conduct and aims that its channels (mailing list, Gitter, github, etc.) to be welcoming environments for everyone.

trace4cats-http4s's People

Contributors

catostrophe avatar dependabot[bot] avatar janstenpickle avatar mpichette-apple avatar scala-steward avatar subsunj avatar trace4cats-steward[bot] avatar ybasket avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

trace4cats-http4s's Issues

How to make both of`trace4cats` and `cats.effect.kernel.Ref` exist at the same time?

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.

Provide call that lifts http4s client into trace context without creating new span

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?

  • Firstly, it's a valid use case to not want the client to create a span. Maybe there are issues with forwarding trace data for this particular service, or the extra span adds unneeded extra information. However, you will still want to forward headers, so that downstream and upstream services can still be connected for the larger trace.
  • Secondly, the creation of span attributes can be something that users want to customize. E.g. if you want to match OpenTelemetry attribute names, amongst other things. By putting the monad lifting into one place, it makes adding custom behavior much simpler, as you can just use 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))
      }
    }

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.