为了分析我们成员服务器的性能问题,尝试使用下面提到的文档:https://http4s.org/v0.23/middleware/获取prometheus上的服务器指标。
build.sbt
"org.http4s" %% "http4s-ember-server" % "0.23.1",
"org.http4s" %% "http4s-blaze-client" % "0.23.1",
"org.http4s" %% "http4s-dsl" % "0.23.1",
"org.http4s" %% "http4s-prometheus-metrics" % "0.23.1"示例代码:
import cats.effect._
import org.http4s.metrics.prometheus.Prometheus
import io.prometheus.client.CollectorRegistry
import org.http4s.server.middleware.Metrics
// build router
val testRoute: Resource[IO, HttpRoutes[IO]] = Prometheus
.metricsOps[IO](registry, "server")
.map(ops =>
Metrics[IO](ops)( {
val dsl = new Http4sDsl[IO] {}
import dsl._
HttpRoutes.of[IO] {
case req @ POST -> Root / "test" =>
OK("test Ok!")
case _: Exception =>
InternalServerError()
}
)
)
)
// build server with the router
EmberServerBuilder
.default[IO]
.withHost(Host.fromString("0.0.0.0").get)
.withPort(Port.fromInt(8080).get)
.withHttpApp(testRoute.orNotFound)
.build.withHttpApp(testRoute.orNotFound)处获得编译错误,这是预期的,但不确定解决方案。这是一个没有Prometheus和Metrics wrapping.HttpRoutes[IO]更改为在包装测试路由时发生的Resource[IO, HttpRoutes[IO]]。)
我是函数式编程方法的新手(对于Scala、Cats lib和http4s也是如此),所有这些泛型似乎都太混乱了。
发布于 2022-02-18 17:59:21
尝试:
// build server with the router
testRoute.flatMap { route =>
EmberServerBuilder
.default[IO]
.withHost(Host.fromString("0.0.0.0").get)
.withPort(Port.fromInt(8080).get)
.withHttpApp(route.orNotFound)
.build
}您需要编写来自prometheus的Resource[IO, HttpRoutes[IO]]和来自http4s的Resource[IO, Server]。
https://stackoverflow.com/questions/71075080
复制相似问题