
io.astefanutti.metrics.aspectj.AbstractMetricAspect.aj Maven / Gradle / Ivy
/**
* Copyright (C) 2013 Antonin Stefanutti ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.astefanutti.metrics.aspectj;
import com.codahale.metrics.Metric;
import com.codahale.metrics.annotation.ExceptionMetered;
import com.codahale.metrics.annotation.Gauge;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
abstract aspect AbstractMetricAspect {
protected interface MetricFactory {
T metric(String name, boolean absolute);
}
protected AnnotatedMetric metricAnnotation(Method method, Class extends Annotation> clazz, MetricFactory factory) {
if (method.isAnnotationPresent(clazz)) {
Annotation annotation = method.getAnnotation(clazz);
T metric = factory.metric(metricAnnotationName(annotation), metricAnnotationAbsolute(annotation));
return new AnnotatedMetric.IsPresent(metric, annotation);
} else {
return new AnnotatedMetric.IsNotPresent();
}
}
protected static String metricAnnotationName(Annotation annotation) {
if (Gauge.class.isInstance(annotation))
return ((Gauge) annotation).name();
else if (ExceptionMetered.class.isInstance(annotation))
return ((ExceptionMetered) annotation).name();
else if (Metered.class.isInstance(annotation))
return ((Metered) annotation).name();
else if (Timed.class.isInstance(annotation))
return ((Timed) annotation).name();
else
throw new IllegalArgumentException("Unsupported Metrics annotation [" + annotation.getClass().getName() + "]");
}
protected static boolean metricAnnotationAbsolute(Annotation annotation) {
if (Gauge.class.isInstance(annotation))
return ((Gauge) annotation).absolute();
else if (ExceptionMetered.class.isInstance(annotation))
return ((ExceptionMetered) annotation).absolute();
else if (Metered.class.isInstance(annotation))
return ((Metered) annotation).absolute();
else if (Timed.class.isInstance(annotation))
return ((Timed) annotation).absolute();
else
throw new IllegalArgumentException("Unsupported Metrics annotation [" + annotation.getClass().getName() + "]");
}
protected static class ForwardingGauge implements com.codahale.metrics.Gauge
© 2015 - 2025 Weber Informatics LLC | Privacy Policy