SpringBoot 使用Prometheus采集自定义指标数据的方案

作者:张志翔 时间:2023-04-25 02:50:39 

我们在k8s集群成功搭建了Prometheus服务。今天,我们将在springboot2.x中使用prometheus记录指标。

一、我们需要什么指标

对于DDD、TDD等,大家比较熟悉了,但是对于MDD可能就比较陌生了。MDD是Metrics-Driven Development的缩写,主张开发过程由指标驱动,通过实用指标来驱动快速、精确和细粒度的软件迭代。MDD可使所有可以测量的东西都得到量化和优化,进而为整个开发过程带来可见性,帮助相关人员快速、准确地作出决策,并在发生错误时立即发现问题并修复。依照MDD的理念,在需求阶段就应该考虑关键指标,在应用上线后通过指标了解现状并持续优化。有一些基于指标的方 * ,建议大家了解一下:

  • Google的四大黄金指标:延迟Latency、流量Traffic、错误Errors、饱和度Saturation

  • Netflix的USE方法:使用率Utilization、饱和度Saturation、错误Error

  • WeaveCloud的RED方法:速率Rate、错误Errors、耗时Duration

二、在SrpingBoot中引入prometheus

SpringBoot2.x集成Prometheus非常简单,首先引入maven依赖:

io.micrometer
micrometer-registry-prometheus
1.7.3
io.github.mweirauch
micrometer-jvm-extras
0.2.2

然后,在application.properties中将prometheus的endpoint放出来。

management:
 endpoints:
   web:
     exposure:
       include: info,health,prometheus

接下来就可以进行指标埋点了,Prometheus的四种指标类型此处不再赘述,请自行学习。一般指标埋点代码实现上有两种形式:AOP、侵入式,建议尽量使用AOP记录指标,对于无法使用aop的场景就只能侵入代码了。常用的AOP方式有:

  • @Aspect(通用)

  • HandlerInterceptor (SpringMVC的 * )

  • ClientHttpRequestInterceptor (RestTemplate的 * )

  • DubboFilter (dubbo接口)

我们选择通用的@Aspect,结合自定义指标注解来实现。首先自定义指标注解:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodMetrics {
   String name() default "";
   String desc() default "";
   String[] tags() default {};
   //是否记录时间间隔
   boolean withoutDuration() default false;
}

然后是切面实现:

@Aspect
public class PrometheusAnnotationAspect {

@Autowired
   private MeterRegistry meterRegistry;

@Pointcut("@annotation(com.smac.prometheus.annotation.MethodMetrics)")
   public void pointcut() {}

@Around(value = "pointcut()")
   public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
       Method targetMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();
       Method currentMethod = ClassUtils.getUserClass(joinPoint.getTarget().getClass()).getDeclaredMethod(targetMethod.getName(), targetMethod.getParameterTypes());
       if (currentMethod.isAnnotationPresent(MethodMetrics.class)) {
           MethodMetrics methodMetrics = currentMethod.getAnnotation(MethodMetrics.class);
           return processMetric(joinPoint, currentMethod, methodMetrics);
       } else {
           return joinPoint.proceed();
       }
   }

private Object processMetric(ProceedingJoinPoint joinPoint, Method currentMethod, MethodMetrics methodMetrics) {
       String name = methodMetrics.name();
       if (!StringUtils.hasText(name)) {
           name = currentMethod.getName();
       }
       String desc = methodMetrics.desc();
       if (!StringUtils.hasText(desc)) {
           desc = currentMethod.getName();
       }
       //不需要记录时间
       if (methodMetrics.withoutDuration()) {
           Counter counter = Counter.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);
           try {
               return joinPoint.proceed();
           } catch (Throwable e) {
               throw new IllegalStateException(e);
           } finally {
               counter.increment();
           }
       }
       //需要记录时间(默认)
       Timer timer = Timer.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);
       return timer.record(() -> {
           try {
               return joinPoint.proceed();
           } catch (Throwable e) {
               throw new IllegalStateException(e);
           }
       });
   }
}

代码很容易,没什么可说明的,接下来就是在需要记监控的地方加上这个注解就行,比如:

@MethodMetrics(name="sms_send",tags = {"vendor","aliyun"})
public void send(String mobile, SendMessage message) throws Exception {
   ...
}

至此,aop形式的指标实现方式就完成了。如果是侵入式的话,直接使用meterRegistry就行:

meterRegistry.counter("sms.send","vendor","aliyun").increment();

启动服务,打开http://localhost:8080/actuator/prometheus查看指标。

三、高级指标之分位数

分位数(P50/P90/P95/P99)是我们常用的一个性能指标,Prometheus提供了两种解决方案:

client侧计算方案

summery类型,设置percentiles,在本地计算出Pxx,作为指标的一个tag被直接收集。

Timer timer = Timer.builder("sms.send").publishPercentiles(0.5, 0.9, 0.95,0.99).register(meterRegistry);
timer.record(costTime, TimeUnit.MILLISECONDS);

会出现四个带quantile的指标,如图:

SpringBoot 使用Prometheus采集自定义指标数据的方案

server侧计算方案

开启histogram,将所有样本放入buckets中,在server侧通过histogram_quantile函数对buckets进行实时计算得出。注意:histogram采用了线性插值法,buckets的划分对误差的影响比较大,需合理设置。

Timer timer = Timer.builder("sms.send")
               .publishPercentileHistogram(true)
               .serviceLevelObjectives(Duration.ofMillis(10),Duration.ofMillis(20),Duration.ofMillis(50))
               .minimumExpectedValue(Duration.ofMillis(1))
               .maximumExpectedValue(Duration.ofMillis(100))
               .register(meterRegistry);
timer.record(costTime, TimeUnit.MILLISECONDS);

会出现一堆xxxx_bucket的指标,如图:

SpringBoot 使用Prometheus采集自定义指标数据的方案

然后,使用

histogram_quantile(0.95, rate(sms_send_seconds_bucket[5m]))

就可以看到P95的指标了,如图:

SpringBoot 使用Prometheus采集自定义指标数据的方案

结论:

方案1适用于单机或只关心本地运行情况的指标,比如gc时间、定时任务执行时间、本地缓存更新时间等;

方案2则适用于分布式环境下的整体运行情况的指标,比如搜索接口的响应时间、第三方接口的响应时间等。

来源:https://blog.csdn.net/qq_19734597/article/details/127211312

标签:SpringBoot,Prometheus,采集
0
投稿

猜你喜欢

  • springboot如何获取相对路径文件夹下静态资源的方法

    2023-07-12 06:23:09
  • 详细解读Java编程中面向字符的输入流

    2023-09-27 10:39:21
  • Android实现在子线程中更新Activity中UI的方法

    2022-12-16 23:57:46
  • Android编程实现获得内存剩余大小与总大小的方法

    2023-06-07 05:49:17
  • Java非静态成员变量之死循环(详解)

    2022-04-15 09:18:20
  • Android填坑系列:在小米系列等机型上放开定位权限后的定位请求弹框示例

    2022-03-29 15:15:49
  • synchronized背后的monitor锁实现详解

    2023-07-31 08:14:10
  • java sql ResultSet 之getRow()用法说明

    2022-07-01 02:43:15
  • Hibernate三种状态和Session常用的方法

    2021-11-28 15:04:43
  • 浅析Spring工厂的反射和配置文件

    2023-06-22 20:52:23
  • 一个简单的Python名片管理系统

    2022-09-20 09:26:15
  • Andriod arcgis保存Mapview为图片的实例代码

    2022-08-15 01:23:29
  • Springboot如何获取yml、properties参数

    2021-09-20 17:34:43
  • c# 通过wbemtest和WMI Code Cretor更加高效的访问WMI

    2022-11-17 16:30:16
  • Android Studio安装配置方法图文详细教程

    2023-01-08 23:10:16
  • JavaWeb实现注册用户名检测

    2022-07-20 05:25:46
  • C#发送内置图片html格式邮件的方法

    2021-11-14 08:07:42
  • Java文件操作实例详解

    2023-11-25 10:29:40
  • Lombok使用@Tolerate实现冲突兼容问题

    2021-06-27 06:50:55
  • Java 中Comparable和Comparator区别比较

    2023-10-28 23:16:34
  • asp之家 软件编程 m.aspxhome.com