我应该使用哪个HTTP状态代码进行运行状况检查失败? [英] Which HTTP status code should I use for a health-check failure?

查看:167
本文介绍了我应该使用哪个HTTP状态代码进行运行状况检查失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个 / _ status / 端点,它会对我们数据库中的数据进行一些健全性检查。

I'm implementing a /_status/ endpoint which does some sanity checks on data in our database.

例如,我们正在收集测量结果,如果最新测量结果超过一小时,状态将变为不良。

For example, we are collecting measurements and the status should go "bad" if the latest measurement is over an hour old.

我想在此网址指向Pingdom以利用他们的警报基础设施,并在出现问题时告诉我们。

I would like to point Pingdom at this URL to leverage their alerting infrastructure and tell us when something's wrong.

在良好状态下,我将提供服务具有HTTP 200 OK状态的HTML页面。但是,对于坏,适当的HTTP状态代码是什么?或者更正确的是不通过状态代码传递这些信息,而是通过HTML内容传递?

On a "good" status I will serve an HTML page with an HTTP 200 OK status. But what would an appropriate HTTP status code be for "bad"? Or would it be more correct not to convey this information via status code, but via HTML content instead?

谢谢!

推荐答案

我们刚刚在小组中进行了类似的讨论。为了我们的目的,我们决定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.

这篇关于我应该使用哪个HTTP状态代码进行运行状况检查失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆