且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该使用哪个HTTP状态代码进行运行状况检查失败?

更新时间:2023-10-20 20:02:16

我们刚刚在小组中进行了类似的讨论。为了我们的目的,我们决定HTTP响应代码应该报告服务器成功或失败以满足请求。对于GET,这将意味着您是否可以使用所请求的资源进行响应。在这种情况下,请求的资源是健康报告,因此只要您成功返回,它就应该是200响应。

We just had a similar discussion in our group. We decided for our purposes that the HTTP response codes should be reporting on your server's success or failure to honor the request. For a GET, this would mean whether or not you can respond with the requested resource. In this case, the requested resource is a health report, so as long as you're returning that successfully, it should be a 200 response.

我们正在返回JSON对于我们的健康检查,将***isHealthy字段设置为true或false。我们的负载均衡器和其他监视器将解析JSON并使用此字段来确定系统是否健康。

We're returning JSON for our health check, with a top-level "isHealthy" field set to true or false. Our load balancer and other monitors will parse the JSON and use this field to determine if the system is healthy or not.

如果您不想解析JSON中的JSON监视器,您可以尝试使用自定义响应标头来指示系统的二进制运行状况,例如 System-Health:true System-Health:false 。你可能有更好的运气获得可以检查的监视器。

If you don't want to parse JSON in your monitors, you could try putting a custom response header to indicate binary health of the system, e.g., System-Health: true or System-Health: false. You might have better luck getting monitors which can check that.

如果你真的想使用响应代码,我会推荐一个附加的端点称为健康的东西,健康时返回204 No Content,不健康时返回404 Not Found。在这种情况下,URL定义的资源象征性地是系统的运行状况,因此如果它是健康的,您可以返回成功的响应。如果它不健康,那么它的健康就无法找到,因此就是404.

If you really want to use a response code, I would recommend an additional endpoint called something like "health" which returns a "204 No Content" when healthy, and a "404 Not Found" when not healthy. In this case, the resource defined by the URL is, symbolically, the health of your system, and so if it's healthy, you can return a successful response. If it's unhealthy, then it's health can't be found, hence the 404.