com.alibaba.metrics.annotation.GaugeRegistry Maven / Gradle / Ivy
package com.alibaba.metrics.annotation;
import com.alibaba.metrics.Gauge;
import com.alibaba.metrics.MetricLevel;
import com.alibaba.metrics.MetricManager;
import com.alibaba.metrics.MetricName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* EnableGauge注解对应的Gauge类型度量指标的注册入口类
*
*/
public class GaugeRegistry {
private static final String KEY_SEPARATOR = "*";
private static final Logger logger = LoggerFactory.getLogger(GaugeRegistry.class);
private static Set registeredKeys = new HashSet();
public static void registerGauge(EnableGauge aliGaugeAnnotation, final Object bean, final Method method) {
String group = aliGaugeAnnotation.group();
String key = aliGaugeAnnotation.key();
String tags = aliGaugeAnnotation.tags();
MetricLevel level = aliGaugeAnnotation.level();
logger.debug("EnableGauge group:{}, key:{}, tags:{}, level:{}", group, key, tags, level);
String registeredKey = group + KEY_SEPARATOR + key + KEY_SEPARATOR + tags;
if (registeredKeys.contains(registeredKey)) {
throw new RuntimeException("不应该存在两个相同的EnableGauge注解,请仔细检查注解的属性值.group:" + group +
",key:" + key + ",tags:" + tags);
}
registeredKeys.add(registeredKey);
Class> returnType = method.getReturnType();
if (isInvalidType(returnType)) {
throw new RuntimeException("标记有EnableGauge注解的方法返回值仅支持int、long、float、double及对应的包装类类型,但该方法的返回类型为:"
+ returnType.getSimpleName() + " class:" + bean.getClass().getSimpleName() + " method:" + method
.getName());
}
Map tagMap = MetricsAnnotationUtil.parseTagMap(tags);
MetricManager.register(group, MetricName.build(key).tagged(tagMap).level(level), new Gauge