问题
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