服务监控-友好地集成Metrics到项目中

一、概述

Metrics的基本介绍能够参考以前的文章:Metrics-服务指标度量java

本文简单介绍下如何将Metrics监控集成到咱们的项目中。spring

本文所使用的metrics-core为3.1.0版本。bash

<dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.1.0</version>
        </dependency>
复制代码

二、场景

咱们的主要的监控需求有如下方面:jvm

  • 机器指标

内存、线程、硬盘、服务GC状况等基本信息是咱们关心的核心指标。咱们能够考虑经过Gauge指标项把这些机器指标作统一收集。ide

  • 服务接口请求频率及耗时

请求频率及耗时是咱们服务接口性能的核心指标,咱们能够考虑经过Timer指标项来采集相关信息。post

  • 服务内部基本数据

在某些场景下咱们将内部Service统计到的瞬时指标上报,如Web Filter里面统计当前正在处理的请求数等。咱们也可使用Gauge指标项来收集。性能

三、方案

针对于以上场景,咱们虽然能够经过写代码的方式建立和注册相应的服务指标,但是在使用上却不太友好。如何更方便灵活地将Metrics指标统计集成到咱们的项目中呢?this

3.一、MetricSet自动注册,收集机器指标

  • (1) 预先定义好MetricSet;

指标集合MetricSet可参看metrics-jvm库的MemoryUsageGaugeSet来定义,MemoryUsageGaugeSet定义了内存使用状况的基本指标,以下所示。spa

/** * A set of gauges for JVM memory usage, including stats on heap vs. non-heap memory, plus * GC-specific memory pools. */
public class MemoryUsageGaugeSet implements MetricSet {
    private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");

    private final MemoryMXBean mxBean;
    private final List<MemoryPoolMXBean> memoryPools;

    public MemoryUsageGaugeSet() {
        this(ManagementFactory.getMemoryMXBean(),
             ManagementFactory.getMemoryPoolMXBeans());
    }

    public MemoryUsageGaugeSet(MemoryMXBean mxBean, Collection<MemoryPoolMXBean> memoryPools) {
        this.mxBean = mxBean;
        this.memoryPools = new ArrayList<MemoryPoolMXBean>(memoryPools);
    }

    @Override
    public Map<String, Metric> getMetrics() {
        final Map<String, Metric> gauges = new HashMap<String, Metric>();

        gauges.put("total.init", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getInit() +
                        mxBean.getNonHeapMemoryUsage().getInit();
            }
        });

        gauges.put("total.used", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getUsed() +
                        mxBean.getNonHeapMemoryUsage().getUsed();
            }
        });

        gauges.put("total.max", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getMax() +
                        mxBean.getNonHeapMemoryUsage().getMax();
            }
        });

        gauges.put("total.committed", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getCommitted() +
                        mxBean.getNonHeapMemoryUsage().getCommitted();
            }
        });


        gauges.put("heap.init", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getInit();
            }
        });

        gauges.put("heap.used", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getUsed();
            }
        });

        gauges.put("heap.max", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getMax();
            }
        });

        gauges.put("heap.committed", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getHeapMemoryUsage().getCommitted();
            }
        });

        gauges.put("heap.usage", new RatioGauge() {
            @Override
            protected Ratio getRatio() {
                final MemoryUsage usage = mxBean.getHeapMemoryUsage();
                return Ratio.of(usage.getUsed(), usage.getMax());
            }
        });

        gauges.put("non-heap.init", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getNonHeapMemoryUsage().getInit();
            }
        });

        gauges.put("non-heap.used", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getNonHeapMemoryUsage().getUsed();
            }
        });

        gauges.put("non-heap.max", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getNonHeapMemoryUsage().getMax();
            }
        });

        gauges.put("non-heap.committed", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return mxBean.getNonHeapMemoryUsage().getCommitted();
            }
        });

        gauges.put("non-heap.usage", new RatioGauge() {
            @Override
            protected Ratio getRatio() {
                final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
                return Ratio.of(usage.getUsed(), usage.getMax());
            }
        });

        for (final MemoryPoolMXBean pool : memoryPools) {
            gauges.put(name("pools",
                            WHITESPACE.matcher(pool.getName()).replaceAll("-"),
                            "usage"),
                       new RatioGauge() {
                           @Override
                           protected Ratio getRatio() {
                               final long max = pool.getUsage().getMax() == -1 ?
                                       pool.getUsage().getCommitted() :
                                       pool.getUsage().getMax();
                               return Ratio.of(pool.getUsage().getUsed(), max);
                           }
                       });
        }

        return Collections.unmodifiableMap(gauges);
    }
}
复制代码
  • (2) 经过BeanPostProcessor处理器自动注册MetricSet对象Bean;
public class UserDefinedMetricBeanPostProcessor implements BeanPostProcessor {

    private final Logger LOG = LoggerFactory.getLogger(getClass());

    private final MetricRegistry metrics = MetricBeans.getRegistry();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof MetricSet) {
            MetricSet metricSet = (MetricSet) bean;
            if (!canRegister(beanName)) {
                return bean;
            }
            String metricName;
            if (isJvmCollector(beanName)) {
                metricName = Config.getProjectPrefix() + "." + beanName;
            } else {
                //根据规则生成Metric的名字
                metricName = Util.forMetricBean(bean.getClass(), beanName);
            }
            try {
                metrics.register(metricName, metricSet);
                LOG.debug("Registered metric named {} in registry. class: {}.", metricName, metricSet);
            } catch (IllegalArgumentException ex) {
                LOG.warn("Error injecting metric for field. bean named {}.", metricName, ex);
            }

        }
        return bean;
    }

    private boolean isJvmCollector(String beanName) {
        return beanName.indexOf("jvm") != -1;
    }

    private boolean canRegister(String beanName) {
        return !isJvmCollector(beanName) || Config.canJvmCollectorStart();
    }
}
复制代码
  • (3) 在spring xml文件或经过spring注解定义bean对象;
<!--定义Jvm监控对象-->
    <bean id="jvm.memory" class="com.codahale.metrics.jvm.MemoryUsageGaugeSet"/>
    <!--自动添加用户定义的监控对象Metric-->
    <bean class="com.test.metrics.collector.UserDefinedMetricBeanPostProcessor"/>
复制代码

能够根据须要定制MetricSet集合,实现服务指标的自动注册及上报。线程

3.二、结合注解实现成员变量自动注册

咱们能够结合注解实现成员变量的自动注册。在BeanPostProcessor能够获取到成员变量的注解,如果咱们的目标注解,能够经过反射的方式获取到变量信息进行自动注册。

下面以Gauged注解为例说明,Gauged注解可让成员变量自动注册并上报。

  • (1) Gauged注解定义;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
public @interface Gauged {
    String name() default "";
}
复制代码
  • (2) 使用BeanPostProcessor解析Gauge注解并注册;

核心代码以下所示:

protected void withField(final Object bean, String beanName, Class<?> targetClass, final Field field) {
        ReflectionUtils.makeAccessible(field);

        final Gauged annotation = field.getAnnotation(Gauged.class);
        final String metricName = Util.forGauge(targetClass, field, annotation);

        metrics.register(metricName, new Gauge<Object>() {
            @Override
            public Object getValue() {
                return ReflectionUtils.getField(field, bean);
            }
        });

        LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName());
    }
复制代码
  • (3) 在spring.xml文件定义对应BeanPostProcessor便可使用。

基本使用以下:

@Component
public class GaugeUsage {

    @Gauged(name = "gaugeField")
    private int gaugedField = 999;
    
}
复制代码

3.三、结合注解实现方法切面的拦截统计

基于Spring AOP能够实现接口调用的耗时统计。

下面以Timed注解为例,Timed注解能够统计接口方法耗时状况。

  • (1) Timed注解定义;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
public @interface Timed {
    String name() default "";
}
复制代码
  • (2) Timed注解切面定义;
@Component
@Aspect
public class MetricAspect {
    @Around("@annotation(timed)")
    public Object processTimerAnnotation(ProceedingJoinPoint joinPoint, Timed timed) throws Throwable {
        Class clazz = joinPoint.getTarget().getClass();
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        String metricName = Util.forTimedMethod(clazz, method, timed);
        Timer timer = MetricBeans.timer(metricName);
        final Timer.Context context = timer.time();
        try {
            return joinPoint.proceed();
        } finally {
            context.stop();
        }
    }
}
复制代码
  • (3) 在spring.xml文件定义MetricAspect便可实现带Timed注解接口的请求频率和耗时统计。

基本示例以下:

@Component
public class TimedUsage {

    //@Timed注解会让监控组件建立Timer对象,统计该方法的执行次数和执行时间等指标
    @Timed(name = "simple-timed-method")
    public void timedMethod() {
        for (int i = 0; i < 1000; i++) {
        }
    }
}
复制代码

四、总结

当前咱们主要经过BenPostProcessorSpring AOP对类实例进行拦截,从而实现服务指标的自动注册和收集。

相关文章
相关标签/搜索