0

I have an Spring Boot application integrated with Dropwizard Metrics following this link.

When I added the @Timed annotation into some APIs (controller methods) it showed on the metrics link.

For example, for below RestController:

@RestController
public class TestController {
    @GET
    @Path("/ping")
    @Timed
    @ApiOperation("Ping server")    
    public Response Ping() {
        return Response.ok().build();
    }
}

then result is as follows:

"timers": {
    "com.test.testcontroller.Ping": {
        "count": 0,
        "max": 0.0,
        "mean": 0.0,
        "min": 0.0,
        "p50": 0.0,
        "p75": 0.0,
        "p95": 0.0,
        "p98": 0.0,
        "p99": 0.0,
        "p999": 0.0,
        "stddev": 0.0,
        "m15_rate": 0.0,
        "m1_rate": 0.0,
        "m5_rate": 0.0,
        "mean_rate": 0.0,
        "duration_units": "seconds",
        "rate_units": "calls/second"
    }
}

I have around 20 controllers with a total of 130 APIs (methods) so I would like to configure a wide annotation or inject the @Timed annotation automatically. Something like:

@RestController
@Timed
public class TestController {
    @GET
    @Path("/ping")
    @ApiOperation("Ping server")    
    // timed will auto applied in here
    public Response Ping() {
        return Response.ok().build();
    }
}

How can I achieve that?

tmarwen
  • 15,750
  • 5
  • 43
  • 62
Ca Pham Van
  • 316
  • 1
  • 3
  • 12

1 Answers1

0

@Timed metric instrumentation is not supported at Class (Controller) level. Here donw an excerpt of the adviced pointcut (from the metrics-spring integration library):

class TimedMethodInterceptor extends AbstractMetricMethodInterceptor<Timed, Timer> implements Ordered {

    public static final Class<Timed> ANNOTATION = Timed.class;
    public static final Pointcut POINTCUT = new AnnotationMatchingPointcut(null, ANNOTATION);
    public static final MethodFilter METHOD_FILTER = new AnnotationFilter(ANNOTATION, PROXYABLE_METHODS);

    public TimedMethodInterceptor(final MetricRegistry metricRegistry, final Class<?> targetClass) {
        super(metricRegistry, targetClass, ANNOTATION, METHOD_FILTER);
    }
//...
}

As you can see from above excerpt, @Timed annotations are only matched at method level.

You will then have to either adapt the library sources and use a custom build for it (take good note of the library licence) or add the timer metric annotation explicitly over all you API methods.

tmarwen
  • 15,750
  • 5
  • 43
  • 62