0

I need to determine how many times my GET /users endpoint is called during the last hour. My first thought was to use a counter and increment it everytime there is a call and rest every 3600 seconds.

Then I found that spring actuator can help provide this if filter using the URI :

localhost:8080/actuator/metrics/http.server.requests?tag=uri:/users

Response:

{
    "name": "http.server.requests",
    "description": null,
    "baseUnit": "seconds",
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 3
        },
        {
            "statistic": "TOTAL_TIME",
            "value": 0.21817219999999998
        }
        
}

Here I called /users 3 times.

My question is, how do I retrieve those values in my code? Can anyone point me to an example? Thanks

ErEcTuS
  • 777
  • 1
  • 14
  • 33
  • What version of Boot are you using? You can just auto wire the `MeterRegistry` and ask it. But it might not know stats for a whole hour. You likely need to register your own meter registry to track over an hour. – Boris the Spider Oct 04 '21 at 21:56
  • Thanks Boris for the answer. I am using spring 2.0.4. I see, I will have to create my own metric then. I didn't find many examples out there. Do you think it is better to use a simple method like a counter instead of using spring boot actuator? – ErEcTuS Oct 04 '21 at 22:02
  • Depends whether you want it fast and correct or easy I suppose. – Boris the Spider Oct 04 '21 at 22:14
  • Here is an example to get count based on the endpoint using the spring boot actuator. https://stackoverflow.com/a/55354671/10961238 If you want it to be based on duration then I think you need to create a custom impl. – Romil Patel Oct 05 '21 at 04:20

0 Answers0