Spring boot actuator - Implement custom metrics

核能气质少年 提交于 2021-01-28 05:02:22

问题


I would like to implement custom metric or statistics to my spring boot rest web service using actuator but i am not able to find simple tutorials. For example:

  • how to show how many times a certain controller was called and what exact parameter field was filled?
  • how can i create a metric that when its URL is called, it runs certain query and shows back a json with some result

回答1:


This seems like a good scenario for AOP (Aspect Oriented Programing) as this will allow you to separate this statistic logic from the business logic.

Have a look at Spring doc for more info about AOP and how to achieve that with Spring.

You can then define a pointcut on your controller and have a service for counting (and probably then storing) the data.

Refer below link AOP Example




回答2:


For point two the solution is to create an endpoint class (it can be or not a rest controller class). For example:

@Component
@RestControllerEndpoint(id = "pfm-statistics")
public class StatisticsEndpoint {
  @GetMapping(value = "/", produces = "application/vnd.openxmlformats- 
     officedocument.spreadsheetml.sheet")
  @ResponseBody
  public byte[] generateStatisticsAsExcel() {
 ...

Note that the ID is the path to be called from URL. We can create a simple endpoint too and just return a string if we want. In this case instead of @RestControllerEndpoint annotation we can use @Endpoint, as a side note, the id should always contain dash



来源:https://stackoverflow.com/questions/53633235/spring-boot-actuator-implement-custom-metrics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!