Bravo
Bravo

Reputation: 9057

how to find how many requests per second , my spring boot app received?

We are using spring boot and with it we have only one rest service , we are receiving requests for that from a single client , but with so many calls , because of that 1 in every 12 requests are failing , how to find how many requests per second my spring boot receiving , is their any way to monitor this metric in production ?

can some one please help us ?

Upvotes: 7

Views: 11813

Answers (4)

Indra Basak
Indra Basak

Reputation: 7394

You can use Dropwizard metrics along with Spring AOP to create metrics for your REST endpoints. I have a working library here which you can use.

Please also see my answer here.

Upvotes: 2

surya
surya

Reputation: 2749

You have different ways to track this , either use spring actuator but a lot of places there is a security issue so you may not be able to use it in production, but you can use a spring filter or an interceptor as given below, where you can intercept a request and after processing , you can check if there was any exception and handle it appropriately.

 @Configuration
 public class MyConfiguration extends WebMvcConfigurerAdapter {


@Override
public void addInterceptors (InterceptorRegistry registry) {
    registry.addInterceptor(new TestInterceptor());

}
}

And your interceptor is as given below , where you count number of users or check any exception and handle it appropriatly

public class TestInterceptor implements HandlerInterceptor  {
AtomicInteger numOfUsers = new AtomicInteger();

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
     HandlerMethod handlerMethod = (HandlerMethod) handler;
     // do something



    return super.preHandle(httpServletRequest, httpServletResponse, handler);
}

 @Override
public void postHandle( HttpServletRequest request, HttpServletResponse response,
        Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("---method executed---");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {
    System.out.println("---Request Completed---");

    // if you have exception thrown then check it here and do something
    System.out.println(numOfUsers.getAndIncrement());

}

Upvotes: 0

rieckpil
rieckpil

Reputation: 12051

As you are using a Spring Boot related project, I would recommend using Springs actuator package. Integrating this functionality is as easy as adding the new dependency to your project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

with this enabled you have access to several new endpoints like /trace, /metrics, /trace, /health and much more.

For more information have a look at the official Spring documentation (https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html) or at the following blog post from Baeldung http://www.baeldung.com/spring-boot-actuators.

Upvotes: 5

Simon Berthiaume
Simon Berthiaume

Reputation: 643

What you might want to use is DropWizard's metrics; you can see examples of what can be done here and you can even use their servlet to have a REST endpoint to monitor those metrics, it will generate something like this:

{
  "version": "3.0.0",
  "gauges": {},
  "counters": {
    "m01-counter": {
      "count": 1
    }
  },
  "histograms": {
    "m02-histogram": {
      "count": 3,
      "max": 100,
      "mean": 41.66666666666666,
      "min": 5,
      "p50": 20,
      "p75": 100,
      "p95": 100,
      "p98": 100,
      "p99": 100,
      "p999": 100,
      "stddev": 41.69998667732268
    }
  },
  "meters": {},
  "timers": {}
}

The "meters" node would even contain the average call rate for the last 1-5-15 minutes.

Upvotes: 3

Related Questions