简体   繁体   English

如何获取SpringBoot REST API服务的响应时间?

[英]How to get response time for SpringBoot REST API services?

Right now, I have a REST API built in SpringBoot.现在,我有一个 REST API 内置在 SpringBoot 中。 I also have a SpringBoot actuator to monitor the health of the app.我还有一个 SpringBoot 执行器来监控应用程序的运行状况。 I have some healthcheck services that either return an empty response with status code 200 or an error if one occurs.我有一些健康检查服务,它们要么返回状态代码为 200 的空响应,要么在发生错误时返回错误。 Now I would like to also be able to get the response time of the service together (or at least separately somehow) with the status.现在我还希望能够将服务的响应时间与状态一起(或至少以某种方式分开)。 I can already see it in "/actuator/httptrace/" in the following form:我已经可以在“/actuator/httptrace/”中以下列形式看到它:

response: {
   status: 200,
   ...
}
timeTaken: 107

So ideally, I would like to be able to get exactly the same thing when I make a request to the api endpoint (just the code and timeTaken).所以理想情况下,当我向 api 端点发出请求时,我希望能够得到完全相同的东西(只是代码和 timeTaken)。 Any help would be appreciated.任何帮助,将不胜感激。

Spring Boot provides metrics for you that will give you this information. Spring Boot 为您提供了可以提供此信息的指标。 There's no need to do it manually.无需手动执行。

See the documentation here:请参阅此处的文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-spring-mvc https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-spring-mvc

Right now, I have a REST API built in SpringBoot.现在,我在 SpringBoot 中构建了一个 REST API。 I also have a SpringBoot actuator to monitor the health of the app.我还有一个 SpringBoot 执行器来监控应用程序的运行状况。 I have some healthcheck services that either return an empty response with status code 200 or an error if one occurs.我有一些健康检查服务,它们要么返回状态代码为 200 的空响应,要么返回错误(如果发生)。 Now I would like to also be able to get the response time of the service together (or at least separately somehow) with the status.现在我还希望能够将服务的响应时间与状态一起(或至少以某种方式分开)一起获得。 I can already see it in "/actuator/httptrace/" in the following form:我已经可以在“/actuator/httptrace/”中以如下形式看到它:

response: {
   status: 200,
   ...
}
timeTaken: 107

So ideally, I would like to be able to get exactly the same thing when I make a request to the api endpoint (just the code and timeTaken).所以理想情况下,当我向 api 端点发出请求(只是代码和 timeTaken)时,我希望能够得到完全相同的东西。 Any help would be appreciated.任何帮助,将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM