Trace4Cats示例Tapir端点

发布于 2025-01-28 03:37:39 字数 2094 浏览 4 评论 0原文

我正在学习不同的Scala图书馆,并进行了追踪。 Trace4Cats声称与Tapir端点集成在一起,我想在我的示例播放路由器中包含它,该路由器将Tapir与OpenAPI文档一起使用。

  1. 到目前为止,我已经提供了这些依赖性的追踪依赖性:
// 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. 我有一个带有Play Framework的SIRD路由器的Tapir示例,如Tapir Docs所建议。这是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)
}

我试图搜索有关如何与Tapir集成的Trace4Cats Docs,但我发现的只是其他示例,包括STTP,但我不确定Tapir的语法。我需要具有 trace4cats (或 natchez 的任何其他解决方案)的人的帮助。非常感谢帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2025-02-04 03:37:39

您的问题太广泛,无法给出一个确切的答案,但我建议您在Trace4Cats的GitHub存储库上查看测试和示例: https://github.com/trace.com/trace4cats/trace4cats/trace4cats4cats4cats4cats4cats4cats-sttp/tree/modaster/modules /sttp-tapir/src/test/scala/io/janstenpickle/trace4cats/sttp/tapir

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文