Trace4Cats example for Tapir endpoints

477 views Asked by At

I am learning about different Scala libraries and I got to tracing. Trace4Cats claims integration with Tapir endpoints and I want to include it inside my example Play SIRD router that uses Tapir with OpenAPI documentation.

  1. So far I've included these dependencies for tracing:
// Tracing
libraryDependencies += "io.janstenpickle" %% "trace4cats-core" % trace4CatsVersion
libraryDependencies += "io.janstenpickle" %% "trace4cats-inject" % trace4CatsVersion
libraryDependencies += "io.janstenpickle" %% "trace4cats-avro-exporter" % trace4CatsVersion
libraryDependencies += "io.janstenpickle" %% "trace4cats-sttp-tapir" % trace4CatsVersion
libraryDependencies += "io.janstenpickle" %% "trace4cats-datadog-http-exporter" % trace4CatsVersion
  1. I have a working Tapir example with Play Framework's SIRD router, as suggested by Tapir docs. Here is the ApiRouter:
@Singleton
class ApiRouter @Inject() (implicit mat: Materializer) extends SimpleRouter {
  // Interpreter
  private val interpreter = PlayServerInterpreter()

  // Controller logic
  def countCharacters(s: String): Future[Either[Unit, Int]] =
    Future(Right[Unit, Int](s.length))

  // Endpoint
  val countCharactersEndpoint: PublicEndpoint[String, Unit, Int, Any] =
    endpoint
      .tag("Example API")
      .in("count")
      .in(query[String]("string"))
      .out(plainBody[Int])
      .errorOut(
        statusCode(StatusCode.NotFound)
      )

  // Route
  val countCharactersRoutes: Routes =
    interpreter.toRoutes(countCharactersEndpoint.serverLogic(countCharacters))

  // OpenAPI
  private val openApiDocs: OpenAPI = OpenAPIDocsInterpreter().toOpenAPI(
    List(countCharactersEndpoint),
    "Tapir Play Sample",
    "1.0.0"
  )

  // Doc will be on /docs
  private val openApiRoute: Routes = interpreter.toRoutes(SwaggerUI[Future](openApiDocs.toYaml))

  // Router
  override def routes: Routes =
    openApiRoute
      .orElse(countCharactersRoutes)
}

I have tried to search Trace4Cats docs on how to integrate it with Tapir, but all I have found is other examples, including STTP, but I'm not sure of the syntax for Tapir. I need help from someone that has experience with Trace4Cats (or Natchez or any other solution that can work here). Help is greatly appreciated.

1

There are 1 answers

0
Gaël J On

Your question is too broad to give a precise answer but I would recommend you to look at the tests and examples on the GitHub repository of trace4cats: https://github.com/trace4cats/trace4cats-sttp/tree/master/modules/sttp-tapir/src/test/scala/io/janstenpickle/trace4cats/sttp/tapir