SpringBoot 使用Prometheus采集自定义指标数据
我们在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.3io.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 {@Autowiredprivate 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采集自定义指标数据相关推荐
- Kubernetes:HPA 详解-基于 CPU、内存和自定义指标自动扩缩容
目录 HPA 基本原理 Metrics Server 聚合 API 安装Metrics Server HPA 基于 CPU自动扩缩容 查看 HPA 资源的对象了解工作过程: HPA 基于 内存自动扩缩 ...
- Java获取Prometheus监控指标数据
Java获取Prometheus监控指标数据 一. 准备工作 1. 有可以被Prometheus监控的服务 没有的话可以参考以下链接本地搭建:SpringBoot应用接入Prometheus+Graf ...
- Prometheus 使用 PushGateway 进行数据上报采集
文章目录 1.PushGateway 介绍 2.环境.软件准备 3.PushGateway 安装配置 3.1.二进制包安装 3.2.Docker 安装 4.API 方式 Push 数据到 PushGa ...
- java结合prometheus实现自定义数据监控
一.配置prometheus prometheus.yml ...- job_name: 'my-service'metrics_path: /metricsstatic_configs:- targ ...
- k8s自定义指标HPA实践(微服务基于自定义指标自动扩缩容的实践)附demo
先上demo代码仓库 https://github.com/wentjiang/prometheus-HPA-test 自动扩缩容的使用场景 在开发微服务时,我们会有一些请求量突增的场景,举个例子,快 ...
- Exporter介绍与指标数据汇总(完结)
1.exporter是什么 广义上讲所有可以向Prometheus提供监控样本数据的程序都可以被称为一个Exporter.而Exporter的一个实例称为target,如下所示,Prometheus通 ...
- 基于prometheus的网络指标监控
1 网络指标监控 网络设备(交换机.路由器.防火墙)本身并没有太多的监控方式,常规的是使用snmp,通过oid去采集网络设备的指标,例如,流量或者错包,不过近年来也出现了新的采集方式,例如,telem ...
- 通过生成指标功能从非指标数据中分析趋势
不同的企业用于分析产品质量.用户体验.业务价值的角度和方法各有不同,然而标准的采集器却满足不了各式各样的可观测需求.一个能够自定义生成指标的功能,能够让采集器收集上来数据更贴合您的需求,通过跟踪这些数 ...
- SpringBoot使用prometheus监控
本文介绍SpringBoot如何使用Prometheus配合Grafana监控. 1.关于Prometheus Prometheus是一个根据应用的metrics来进行监控的开源工具.相信很多工程都在 ...
- SpringBoot之获取配置文件中的数据
SpringBoot之获取配置文件中的数据 项目结构 配置application.properties book.author=Tom book.name=SpringBoot # spring.pr ...
最新文章
- 记录下Linux难记实用的命令
- Linux tcpdump命令用法详解
- 32位Windows系统未分页内存限制导致的VPS的容量问题
- 数据访问层代码自动生成
- 那些天天都在野外钓鱼的人不上班吗,有收入吗?
- 背景图片固定(背景图片附着)(HTML、CSS)
- ospf路由 华3_华三模拟器ospf的简单配置
- 身份证省市区SQL代码
- 统计数据收集方式与收集方法
- 【现代信号处理】 07 - 正则化
- 32位与64位CPU字长
- html5 今日头条视频播放,今日头条app设置自动播放视频的方法
- 计算机考试显示延时一分钟,电脑显示延迟解决方法教程
- 20201212大一集训牛客网题之d题中学数学题
- 网页/公众号音乐下载
- android 读写sd文件系统,SD卡FAT16文件系统的学习笔记
- python拼音转换汉字
- 吉他弹唱精通——更高难度的分解和弦
- Pytorch 中的数据类型 torch.utils.data.DataLoader 参数详解
- CAD修改默认捕捉点设置