普罗米修斯Prometheus(5) - springboot API自定义监控

注册application配置

import io.micrometer.core.instrument.MeterRegistry;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MicrometerConfig {    @Value("${spring.application.name}")    private String applicationName;    @Bean    MeterRegistryCustomizer metricsCommonTags() {        return registry -> registry.config().commonTags("application", applicationName);    }}

配置application.yml

spring:  application:    name: 应用名称management:  metrics:    tags:      application: ${spring.application.name}    export:      prometheus:        enabled: true  endpoints:    web:      exposure:        include: "*"

统计单个API的请求次数

// 方式一
static final Counter counterBook = Metrics.counter("app_requests_method_count", "method", "BookController.book");
。。。
counterBook.increment();
// 方式二
@AutowiredMeterRegistry registry;
private Counter counterBookPage;
@PostConstruct private void init(){     
counterBookPage = registry.counter("app_requests_method_count", "method", "BookController.bookPage"); 
}
。。。
counterBookPage.increment();

统计API总次数,采用AOP

import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.MeterRegistry;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;/** * Prometheus监控 */@Aspect@Componentpublic class PrometheusMonitor {    private static Logger logger = LoggerFactory.getLogger(PrometheusMonitor.class);    @Autowired    MeterRegistry registry;    //记录请求总次数    private Counter totalCounter;    //记录方法请求数    private Counter methodCounter;    //统计请求的处理时间    ThreadLocal startTime = new ThreadLocal<>();    @Pointcut("execution(public * 域名.controller.*.*(..))")    private void pointCut() {    }    @PostConstruct    public void init() {        totalCounter = registry.counter("api_requests_total", "status", "success");    }    @Before("pointCut()")    public void doBefore(JoinPoint joinPoint) throws Throwable {        startTime.set(System.currentTimeMillis());        totalCounter.increment();    }    @AfterReturning(returning = "returnVal", pointcut = "pointCut()")    public void doAfterReturning(Object returnVal) {        System.out.println("请求执行时间:" + (System.currentTimeMillis() - startTime.get()));    }}

查看springboot的actuator

http://服务器ip:端口/actuator/prometheus

查询prometheus

访问:http://服务器ip:9090/graph
输入查询语句:app_requests_method_count_total{application="应用名称",method="BookController.book",}

配置Grafana

1. 左侧菜单栏选择Create --> Dashboard;
2. 在New Panel界面,点击Add Query;
3. 选择对应的数据源,添加PromQL,点击右上角保存,填写Dashboard的名称。

你可能感兴趣的:(普罗米修斯Prometheus(5) - springboot API自定义监控)