iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot使用Prometheus采集自定义指标数据的方案
  • 526
分享到

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

2024-04-02 19:04:59 526人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、我们需要什么指标二、在SrpingBoot中引入prometheus三、高级指标之分位数我们在k8s集群成功搭建了Prometheus服务。今天,我们将在SpringBoot

我们在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的指标,如图:

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的指标,如图:

然后,使用

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

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

结论:

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

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

到此这篇关于SpringBoot 使用Prometheus采集自定义指标数据的文章就介绍到这了,更多相关SpringBoot采集自定义指标数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

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

本文链接: https://www.lsjlt.com/news/169011.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作