Search code examples
spring-bootspring-boot-actuator

Spring boot actuator - Implement custom metrics


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

Solution

  • 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