对于Tapir和ZIO中的Prometheus度量有一个问题。我有一个简单的代码:
val metrics = PrometheusMetrics.default[Task]()
val options: ZioHttpServerOptions[Any] = ZioHttpServerOptions
.customiseInterceptors
.metricsInterceptor(metrics.metricsInterceptor())
.options当我调用localhost:8080/metrics时,它是正确的,我看到了度量标准。
但是,当我添加默认错误处理程序时:
val metrics = PrometheusMetrics.default[Task]()
def failureResponse(msg: String): ValuedEndpointOutput[_]=
ValuedEndpointOutput(jsonBody[MyFailure], MyFailure(msg))
val options: ZioHttpServerOptions[Any] = ZioHttpServerOptions
.customiseInterceptors
.metricsInterceptor(metrics.metricsInterceptor())
.defaultHandlers(failureResponse, notFoundWhenRejected = true)
.options它不起作用。我现在看到的不是度量,而是在localhost:8080/metrics请求期间捕获的错误(404)。老实说我不知道为什么。是否有可能以某种方式修复它,并将错误处理程序与度量拦截器一起保存?
编辑:度量端点:
def metricsEndpoint = ZioHttpInterpreter(options).toHttp(metrics.metricsEndpoint)发布于 2022-09-20 18:33:05
这个问题很可能是由于将“主”端点和度量端点分别解释为ZIO的Http值造成的。
请考虑以下几点:
val mainHttp = ZioHttpInterpreter(options).toHttp(mainEndpoints)
val metricsHttp = ZioHttpInterpreter(options).toHttp(metricsEndpoints)
Server.start(8080, mainHttp <> metricsHttp)如果使用notFoundWhenRejected = true选项,则当请求/metrics传入时,它首先由mainHttp处理。但是,该值不知道如何处理该请求--因此被拒绝。但是,由于我们指定了上述选项,拒绝将变成404,因此答案。
该选项的默认值是false。在这种情况下,/metrics请求将被mainHttp值拒绝,但这不会转换为404响应,而是继续使用metricsHttp进行处理。
让/metrics和notFoundWhenRejected = true选项同时工作的正确解决方案是一次解释所有端点。然后,只有当没有一个端点(主端点和度量端点)与请求匹配时,404才会返回:
val http = ZioHttpInterpreter(options)
.toHttp(mainEndpoints ++ metricsEndpoints)
Server.start(8080, http)https://stackoverflow.com/questions/73786005
复制相似问题