【监控利器Prometheus】——Prometheus+Grafana监控SpringBoot项目业务指标监控

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Prometheus+Grafana监控SpringBoot项目业务指标监控1、SpringBoot项目配置2、prometheus添加配置3、Grafana配置

1、SpringBoot项目配置


(1)maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>


(2)application.properties


spring.application.name=springboot_business
server.port=6002
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}


(3)这里以【订单成功数量】、【订单失败数量】、【订单成功金额】、【订单失败金额】为例进行统计

import com.alibaba.fastjson.JSON;
import com.danny.monitor.prometheus.business.bean.Order;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MeterRegistry registry;
    private Counter orderSuccessCounter; // 累计成功订单数量
    private Counter orderFailedCounter; // 累计失败订单数量
    private DistributionSummary orderSuccessSummary; // 成功订单金额
    private DistributionSummary orderFailedSummary; // 失败订单金额
    @PostConstruct
    private void init() {
        orderSuccessCounter = registry.counter("create_order_total", "create_order_total", "success");
        orderFailedCounter = registry.counter("create_order_total", "create_order_total", "failed");
        orderSuccessSummary = registry.summary("order_amount", "order_amount", "success");
        orderFailedSummary = registry.summary("order_amount", "order_amount", "failed");
    }
    // 创建订单
    @Override
    public Order createOrder(BigDecimal orderAmount) {
        log.info("createOrder orderAmount:{}", orderAmount);
        Order order = null;
        try {
            if (orderAmount.compareTo(BigDecimal.valueOf(10)) < 0) {
                throw new Exception("金额小于10时,模拟订单失败的情况");
            }
            order = new Order();
            order.setOrderAmount(orderAmount);
            order.setOrderNo(RandomStringUtils.randomAlphabetic(32));
            order.setOrderTime(LocalDateTime.now());
            orderSuccessCounter.increment(); // 订单成功数量埋点数据
            orderSuccessSummary.record(orderAmount.doubleValue()); // 订单成功金额埋点数据
        } catch (Exception e) {
            log.info("createOrder error", e);
            orderFailedCounter.increment();  // 订单失败数量埋点数据
            orderFailedSummary.record(orderAmount.doubleValue());  // 订单失败金额埋点数据
            order = null;
        } finally {
            log.info("queryOrder result:{}", JSON.toJSONString(order));
            return order;
        }
    }
}


2、prometheus添加配置


修改prometheus.yml,添加SpringBoot项目的地址

scrape_configs:
  - job_name: 'springboot_business'
    scrape_interval: 10s
    scrape_timeout: 10s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['10.246.140.63:6002']


重启Prometheus后,启动SpringBoot项目并访问几次下单接口(目的是为了造点埋点数据),在 http://localhost:6002/actuator/prometheus 页面可以看到在上面 OrderServiceImpl 中添加的统计指标。


在Prometheus UI界面通过PromeSQL查询统计指标的数据:

(1)查询【创建订单数量】统计数据


33.png


(2)查询【创建订单金额】统计数据


34.png

以上数据在重启SpringBoot项目后,就会丢失。可以把counter、summary中的值在缓存中存一份,在启动的时候初始化counter设置初始值。


3、Grafana配置


(1)订单数量统计

在Grafana中新建一个Dashboard(Create -> Dashboard),在新的Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单数量】,数据源选择【Prometheus】,Metircs选择【create_order_total】(在上面代码中定义的统计订单数量的Counter的名称),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key)。


35.png

35.png


保存

36.png


(2)订单金额统计

按照同样的方式添加统计订单金额的pannel,需要注意的是,DistributionSummary定义的指标,在prometheus中会加个后缀,比如上面定义的 DistributionSummary 的 name 为 “order_amount”,在prometheus收集时会有“order_amount_sum”、“order_amount_count”、“order_amount_max”多个指标,具体可以在 http://localhost:6002/actuator/prometheus 中查看。


37.png


(3)订单增长率统计

在Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单量增长率】,数据源选择【Prometheus】,Metircs写【rate(create_order_total [1m])】(表示1分钟内的订单量增长率),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key),在Standard options -> Unit 中选择纵坐标的显示方式为 Percent(0.0-1.0),也就是百分比显示。


38.png


(4)同理可以按照(3)的方式配置订单金额增长率的Pannel

39.png


相关文章
|
6天前
|
存储 JSON Prometheus
如何精简 Prometheus 的指标和存储占用
如何精简 Prometheus 的指标和存储占用
|
6天前
|
存储 Prometheus Kubernetes
「译文」通过 Relabel 减少 Prometheus 指标的使用量
「译文」通过 Relabel 减少 Prometheus 指标的使用量
|
6天前
|
Prometheus 监控 Cloud Native
应用监控(Prometheus + Grafana)
应用监控(Prometheus + Grafana)
24 2
|
6天前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
69 2
|
6天前
|
数据采集 监控 数据库
请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
【2月更文挑战第25天】请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
26 2
|
6天前
|
存储 Prometheus Cloud Native
Grafana 系列 - 统一展示 -2-Prometheus 数据源
Grafana 系列 - 统一展示 -2-Prometheus 数据源
|
6天前
|
JSON Prometheus Cloud Native
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
|
6天前
|
监控 Cloud Native 关系型数据库
使用 Grafana 统一监控展示 - 对接 Zabbix
使用 Grafana 统一监控展示 - 对接 Zabbix
|
6天前
|
Prometheus Cloud Native
「译文」如何使用 PromQL join 来更有效地查询大规模的 Prometheus 指标
「译文」如何使用 PromQL join 来更有效地查询大规模的 Prometheus 指标
|
6天前
|
Prometheus 监控 Kubernetes
Prometheus + Grafana安装
Prometheus + Grafana安装