com.palominolabs.metrics.guice.MeteredInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-guice Show documentation
Show all versions of metrics-guice Show documentation
com.palominolabs.metrics:metrics-guice
package com.palominolabs.metrics.guice;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Metered;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Method;
/**
* A method interceptor which creates a meter for the declaring class with the given name (or the
* method's name, if none was provided), and which measures the rate at which the annotated method
* is invoked.
*/
class MeteredInterceptor implements MethodInterceptor {
static MethodInterceptor forMethod(MetricRegistry metricRegistry, Class> klass, Method method) {
final Metered annotation = method.getAnnotation(Metered.class);
if (annotation != null) {
final Meter meter = metricRegistry.meter(determineName(annotation, klass, method));
return new MeteredInterceptor(meter);
}
return null;
}
private static String determineName(Metered annotation, Class> klass, Method method) {
if (annotation.absolute()) {
return annotation.name();
}
if (annotation.name().isEmpty()) {
return MetricRegistry.name(klass, method.getName());
}
return MetricRegistry.name(klass, annotation.name());
}
private final Meter meter;
private MeteredInterceptor(Meter meter) {
this.meter = meter;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
meter.mark();
return invocation.proceed();
}
}