Springboot应用整合Prometheus+Grafana进行自定义指标监控
最近需要监控接口的请求数据P90,P99,平均耗时,接口参数包含userId的接口请求数据等等
第一步 创建一个springboot项目
pom文件
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <!--aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--prometheus --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency>
springboot的actuator可以很好的配合prometheus实现项目的监控,所以这里使用actuator来进行配合
配置信息
spring.application.name="prometheus-test" //建议写上
management.endpoint.metrics.enabled=true //支持metrics
management.endpoints.web.exposure.include=* //开放端口
management.endpoint.prometheus.enabled=true //支持prometheus
management.metrics.export.prometheus.enabled=true
切面实现prometheus自定义监控指标
监控请求数量 ,请求响应时间,请求特定参数
package com.example.prometheus.demo.aspect; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Timer; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.time.LocalDate; import java.util.concurrent.TimeUnit; @Aspect @Component public class PrometheusMetricsAspect { // 切入所有controll