springboot如何集成Prometheus如何暴露Histogram来获取P99等监控指标
背景
springboot如何集成Prometheus我这里不做详细描述,要想了解集成过程,可以参考一下博客:
Spring Boot 使用 Micrometer 集成 Prometheus 监控 Java 应用性能,
这里我讲一下如果我们j需要获取到某些监控指标的p99、p90等度量数据,比如说http请求的响应时长p99等,
Micrometer
根据指标监控的对象个数、监视目的和含义的不同,Micrometer指标类型大体可以分为Gauge、Counter、DistributionSummary、Timer四种。
这里我重点说一下DistributionSummary。
DistributionSummary
DistributionSummary是用于跟踪事件的分布情况,有多个指标组成:
- count,事件的个数,聚合指标,如响应的个数
- sum,综合,聚合指标,如响应大小的综合
- histogram,分布,聚合指标,包含le标签用于区分bucket,例如web.response.size.historgram{le=512} = 99,表示响应大小不超过512(Byte)的响应个数是99个。一般有多个bucket,如le=128,le=256,le=512,le=1024,le=+Inf等。
每个bucket展示为一条时间序列,会得到类似下面的图。

percentile(quantile),百分位数,聚合指标,包含percentile标签用于区分不同的百分位,例如web.response.size.percentile{p=90) = 512,表示90%的响应大小都小于512。一般有多个percentile,如p50,p75,p90,p99。
每个百分位展示为一条时间序列,会得到类似下面的图。

Timer
Timer是DistributionSummary的特化,专门用于计时类的指标,可以对记录的时间值(duration)进行单位换算。
暴露histogram
<dependency>
<groupid>io.micrometer</groupid>
<artifactid>micrometer-registry-prometheus</artifactid>
</dependency>
其中prometheus的类库中为我们提供了很多的监控指标,可以访问下
http://localhost:8080/actuator/prometheus就可以看到已有监控看信息

但是默认这些监控指标并不是DistributionSummary,所以需要我们在初始化时替换配置并打开计数开关,如下:
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
@Configuration
@Slf4j
public class MicrometerConfig {
@Bean
MeterRegistryCustomizer<meterregistry> metricsCommonTags() {
return registry -> {
registry.config().meterFilter(
new MeterFilter() {
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
if (id.getType() == Meter.Type.TIMER&&id.getName().matches("^(http|hystrix){1}.*")) {
return DistributionStatisticConfig.builder()
.percentilesHistogram(true)
.percentiles(0.5, 0.90,0.95, 0.99)
.sla(Duration.ofMillis(50).toNanos(),
Duration.ofMillis(100).toNanos(),
Duration.ofMillis(200).toNanos(),
Duration.ofSeconds(1).toNanos(),
Duration.ofSeconds(5).toNanos(),
.minimumExpectedValue(Duration.ofMillis(1).toNanos())
.maximumExpectedValue(Duration.ofSeconds(5).toNanos())
.build()
.merge(config);
} else {
return config;
}
}
});
};
}
}
以上代码根据需要来暴露histogram信息,这里是选择http响应时长以及hystrix响应监控暴露histogram,如果有需要可以不增加过滤条件。
打开后可以看到多了很多监控信息:

配置到grafana
avg(http_server_requests_seconds{service=~"$service" ,quantile =~ "0.9|0.5|0.99", uri !~ "/actuator.*|/health|/prometheus|root"}*1000 > 0) by (uri,quantile)
</meterregistry>
springboot如何集成Prometheus如何暴露Histogram来获取P99等监控指标的更多相关文章
- asp.net core 集成 Prometheus
asp.net core 集成 prometheus Intro Prometheus 是一个开源的现代化,云原生的系统监控框架,并且可以轻松的集成 PushGateway, AlertManager ...
- SpringBoot应用集成微服务组件Nacos
目录 springboot与微服务组件nacos Nacos服务快速启动 STS4 开发工具 Maven 环境配置 STS4开发工具引入Maven配置 Maven Repo配置阿里云镜像源 Sprin ...
- Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警
Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...
- springboot elasticsearch 集成注意事项
文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...
- Springboot Application 集成 OSGI 框架开发
内容来源:https://www.ibm.com/developerworks/cn/java/j-springboot-application-integrated-osgi-framework-d ...
- SpringBoot项目集成Hystrix
Hystrix Hystrix是由Netflix开源的一个服务隔离组件,通过服务隔离来避免由于依赖延迟.异常,引起资源耗尽导致系统不可用的解决方案. 1.什么是服务熔断 服务熔断就是对该服务的调用 ...
- 钉钉通知机器人与SpringBoot的集成
Spring Boot Admin 集成自定义监控告警(2.0.1版本)------钉钉机器人 - yuancao24的博客 - CSDN博客https://blog.csdn.net/yuancao ...
- SpringBoot项目集成PageHelper使用
SpringBoot项目集成PageHelper使用 一.开始 地址:https://github.com/pagehelper/Mybatis-PageHelper 在spring boot ...
- Springboot简单集成ActiveMQ
Springboot简单集成ActiveMQ 消息发送者的实现 pom.xml添加依赖 <dependency> <groupId>org.springframework.bo ...
- 在springboot中集成mybatis开发
在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...
随机推荐
- Linux驱动:使用workqueue、tasklet处理中断
Linux驱动:使用workqueue.tasklet处理中断 背景 中断服务程序一般都是在中断请求关闭的条件下执行的,以避免嵌套而使中断控制复杂化.但是,中断是一个随机事件,它随时会到来,如果关中断 ...
- jsp+servlet+mysql-----------批量删除
jsp: <div class="result-wrap"> <form action="${pageContext.request.contextPa ...
- 实现Quartz.NET的HTTP作业调度
Quartz.NET作为一个开源的作业调度库,广泛应用于.NET应用程序中,以实现复杂的定时任务,本次记录利用Quartz.NET实现HTTP作业调度,通过自定义HTTP作业,实现对外部API的定时调 ...
- MFC基于对画框工程笔记->更改窗口图标以及生成的.exe图标
一.前言 继前一篇生成MFC基于对话框工程->新建MFC对话框后,开始改动对话框图标以及生成的.exe图标. 原对话框图标以及.exe图标: 在菜单栏中选择生成目录为Release 打开Rele ...
- Spring Boot集成Mybatis分页插件pagehelper
引入依赖 <!--分页插件开始--> <dependency> <groupId>com.github.pagehelper</groupId> < ...
- Oracle自定义数据类型
1 CREATE OR REPLACE FUNCTION split(p_str IN clob, 2 p_delimiter IN VARCHAR2 default (',') --分隔符,默认逗号 ...
- IDEA社区版新建一个最简单的Spring工程(Spring框架搭建 01)
创建Spring项目 IDEA打开New Project-Maven Archetype,选择Archetype:org.apache.maven.archetypes:maven-archetype ...
- h5使用js拉起微信支付
近期,业务需求对接了微信支付,做个总结.web网页想要拉起微信支付,有两种方法: H5下单支付 , JSAPI支付 .首先纯前端做不了微信支付,必须配合后端才能通过微信的下单请求.接下来说说这两种方法 ...
- 在缩小浏览器宽度的时候,图片会超出li的宽度
要确保在缩小浏览器宽度时,图片不会超出 <li> 元素的宽度,您可以为描述文本添加一些样式,以便让图片适应于 <li> 元素.一种常见的方法是使用 CSS 中的 max-wid ...
- Linux 提权-NFS 共享
本文通过 Google 翻译 NFS Share no_root_squash – Linux Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校 ...