Springboot应用整合Prometheus+Grafana进行自定义指标监控

本文介绍了如何在Springboot应用中整合Prometheus和Grafana进行自定义指标监控,包括创建Springboot项目、安装Prometheus和Grafana、设置自定义监控指标、使用Grafana展示数据以及开启P90、P99统计。通过监控接口的请求数据、响应时间和特定参数,实现了全面的性能监控。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值