How to calculate the throughput of an endpoint in Spring Boot?

94 Views Asked by At

I'm trying to calculate throughput of http endpoint. Firstly, we need the number of http requests count for an endpoint. Do I need to store the calculated count when the server gets restarted as well?

How can I formulate the logic for calculation of throughput and the best way to go ahead?

2

There are 2 best solutions below

0
Artur May On

There are some possible scenarios if you don't want to use additional tools for that purpose.

If you're interested only in counting the throughput of an HTTP endpoint and the number of users is not too big, you can save this number by logging it in a dedicated file:

 @GetMapping("/endpointName")
public String handleRequest() {
    // Increment the counter for the given endpoint name
    endpointCalls.logAndIncrementCounter(Controller.class.getName(), "endpointName");
    
    // Perform other processing here
    
    return "Request processed successfully";
}

Here is the logic that will increment the count of endpoint calls and rewrite the log file. You can modify this code to save the data wherever you prefer - in a database, cache, or by other means of logging:

@Component
public class EndpointCalls {

    private static final Logger LOG = LoggerFactory.getLogger(EndpointCalls.class);

    private static Map<String, Integer> counterMap = new HashMap<>();

    public void logAndIncrementCounter(String className,String endpointName) {


        // Increment the counter for the given endpoint name
        counterMap.put(className+"."+endpointName, counterMap.getOrDefault(className+"."+endpointName, 0) + 1);

        // Get the Tomcat base directory
        String catalinaHome = System.getProperty("catalina.home");

        // Construct the log file path
        String logFilePath = catalinaHome + "/logs/endpointCalls.log";

        // Clear the file content and log the new map
        try (PrintWriter writer = new PrintWriter(new FileWriter(logFilePath, false))) {
            writer.print(""); // Clear the file content
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Log the method call
        String mapAsJson = new Gson().toJson(counterMap);
        LOG.debug("Endpoint called: {}", mapAsJson);
    }

}
0
Noki Nori On

If you able to use other spring libraries I suggest to look at sprint-boot-actuator.

implementation 'org.springframework.boot:spring-boot-starter-actuator'

application.yaml

management:
  server:
   port: 9090
  endpoints:
   web:
     exposure:
     include: health, metrics, prometheus

Then server will expose metrics in a format that can be scraped by a Prometheus server like Grafana.

Can be retrieved by url http://localhost:9090/actuator/prometheus in prometheus format.

Or http://localhost:9090/actuator/metrics just for observability.

The endpoints metrics will be available in format http.server.requests.

To get the RPS of an particular metric you should do the calculation http.server.requests.seconds.sum / http.server.requests.seconds.count

More in doc.