dropwizard/metrics — pull request #704 by rajuky (2014-11-21 16:11:25+00:00)
Title: Custom Annotation with Custom Interceptors

Ryan, I would like to request for changes to provide extensibility where we could inject custom interceptors with custom annotations into the metrics framework.

If I want to inject a custom exception for example
public @interface SystemExceptionMetered with the SystemExceptionMethodInterceptor which would catch a bunch of exceptions and increment the meter.

@Override
protected Object invoke(MethodInvocation invocation, Meter meter, SystemExceptionMetered annotation) throws Throwable {
try {
return invocation.proceed();
}
catch (SocketTimeOutException t) {
if (annotation.cause().isAssignableFrom(t.getClass())) {
meter.mark();
}
throw t;
}
catch (OutofMemoryException t) {
if (annotation.cause().isAssignableFrom(t.getClass())) {
meter.mark();
}
throw t;
}
catch (IOException t) {
if (annotation.cause().isAssignableFrom(t.getClass())) {
meter.mark();
}
throw t;
}
1.This would require AbstractMetricMethodInterceptor to be public
2.add new factory method to MetricsBeanPostProcessorFactory
public static AdvisingBeanPostProcessor customInterceptorFactoryMethod(
String interceptorName,MetricRegistry metricRegistry, ProxyConfig proxyConfig) {

try {
Class interceptorClass = Class.forName(interceptorName);
Field field = interceptorClass.getDeclaredField("POINTCUT");
Pointcut obj1 = (Pointcut) field.get(null);
Method m1 = interceptorClass.getDeclaredMethod("adviceFactory",
MetricRegistry.class);
FTAdviceFactory obj2 = (FTAdviceFactory) m1.invoke(null,
metricRegistry);
return new AdvisingBeanPostProcessor(obj1, obj2,
proxyConfig);}

4. Modify metrics.xsd to include interceptors as optional

so that we can list our interceptors

SystemExceptionMethodInterceptor
ClientExceptionMethodInterceptor

Please let me know your thoughts.