• Spring Boot 2.x系列【24】应用监控篇之指标Metrics


    有道无术,术尚可求,有术无道,止于术。

    本系列Spring Boot版本2.7.0

    前言

    IT监控系统简介一文中,介绍过一个成熟的监控系统包含了系统层、中间件及基础设施类、应用层、业务层四个层面的监控,并且包含指标数据采集、指标数据存储、指标数据趋势分析及可视化、告警等功能。

    Metrics翻译过来是量度;指标的意思,是指随时间推移产生的一些与监控相关的可聚合数据点。

    接下里我们学习下Spring Boot是如何进行应用层面指标采集的。

    指标端点

    Spring Boot提供了应用层指标监控,可以诊断性检查应用程序收集的指标。

    有了指标数据,就可以实现数据存储、可视化查询、告警,当然这些需要依赖于第三方监控系统。
    在这里插入图片描述
    访问metrics端点,可以看到一些指标项:
    在这里插入图片描述
    访问具体的指标项可获取到对应的指标数据:
    在这里插入图片描述

    Micrometer

    可以看到spring-boot-starter-actuator默认是引入了一个Micrometer框架:
    在这里插入图片描述
    Micrometer 是一个用于基于 JVM 的应用程序的指标检测库。旨在为指标收集活动增加很少甚至没有开销,同时最大限度地提高指标工作的可移植性。

    有了指标检测和收集,就可以对接应用监控系统,将数据对接入监控系统进行展示。

    Spring Boot ActuatorMicrometer提供依赖管理和自动配置,支持众多监控系统包括:

    • AppOptics

    • Atlas

    • Datadog

    • Dynatrace

    • Elastic

    • Ganglia

    • Graphite

    • Humio

    • Influx

    • JMX

    • KairosDB

    • New Relic

    • Prometheus

    • SignalFx

    • Simple (in-memory)

    • Stackdriver

    • StatsD

    • Wavefront

    Prometheus+Grafana监控Spring Boot

    支持的指标

    Spring Boot 默认提供了很多指标。

    应用程序启动指标

    自动配置公开应用程序启动时间指标:

    • application.started.time: 启动应用程序所用的时间。

    • application.ready.time:应用程序准备好为请求提供服务所需的时间。

    访问/actuator/metrics/application.started.time

    {
        "name": "application.started.time", // 指标名称
        "description": "Time taken (ms) to start the application", // 描述信息
        "baseUnit": "seconds", // 时间单位
        "measurements": [
            {
                "statistic": "VALUE",
                "value": 2.826  // 启动耗时2.826秒
            }
        ],
        "availableTags": [
            {
                "tag": "main.application.class", 
                "values": [
                    "org.pearl.app.App1"// 启动类
                ]
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    系统指标

    自动配置通过使用核心 Micrometer 类启用系统指标。系统指标以system.、process.disk.计量名称发布。

    提供了以下系统指标

    • CPU 指标

    • 文件描述符指标

    • 正常运行时间指标

    • 可用磁盘空间

    可用指标如下

    {
        "names": [
            "disk.free",
            "disk.total",
            "process.cpu.usage",
            "process.start.time",
            "process.uptime",
            "system.cpu.count",
            "system.cpu.usage",
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    访问/actuator/metrics/disk.total

    {
        "name": "disk.total",
        "description": "Total space for path",
        "baseUnit": "bytes",
        "measurements": [
            {
                "statistic": "VALUE",
                "value": 3.32862058496E11
            }
        ],
        "availableTags": [
            {
                "tag": "path",
                "values": [
                    "E:\\TD\\project\\my\\study-demo\\."
                ]
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    JVM 指标

    JVM 指标以jvm.计量名称发布。提供了以下 JVM 指标:

    • 各种内存和缓冲池细节

    • 垃圾回收相关统计

    • 线程利用率

    • 加载和卸载的类数

    可用指标如下

    {
        "names": [
            "jvm.buffer.count",
            "jvm.buffer.memory.used",
            "jvm.buffer.total.capacity",
            "jvm.classes.loaded",
            "jvm.classes.unloaded",
            "jvm.gc.live.data.size",
            "jvm.gc.max.data.size",
            "jvm.gc.memory.allocated",
            "jvm.gc.memory.promoted",
            "jvm.gc.overhead",
            "jvm.gc.pause",
            "jvm.memory.committed",
            "jvm.memory.max",
            "jvm.memory.usage.after.gc",
            "jvm.memory.used",
            "jvm.threads.daemon",
            "jvm.threads.live",
            "jvm.threads.peak",
            "jvm.threads.states",
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    访问/actuator/metrics/jvm.threads.live

    {
        "name": "jvm.threads.live",
        "description": "The current number of live threads including both daemon and non-daemon threads",
        "baseUnit": "threads",
        "measurements": [
            {
                "statistic": "VALUE",
                "value": 25.0 // 存活线程数 
            }
        ],
        "availableTags": []
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    日志记录器指标

    自动配置为 Logback 和 Log4J2 启用事件指标。

    访问/actuator/metrics/logback.events

    {
        "name": "logback.events",
        "description": "Number of trace level events that made it to the logs",
        "baseUnit": "events",
        "measurements": [
            {
                "statistic": "COUNT",
                "value": 11.0
            }
        ],
        "availableTags": [
            {
                "tag": "level",
                "values": [
                    "warn",
                    "trace",
                    "debug",
                    "error",
                    "info"
                ]
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    定时任务指标

    检测所有的ThreadPoolTaskExecutorThreadPoolTaskScheduler Bean对象,度量标签来自于线程池的 Bean 名称。

    Spring MVC 指标

    自动配置启用对Spring MVC控制器和功能处理程序处理的所有请求的进行检测。默认情况下,生成的指标名称为 http.server.requests. 可以通过设置management.metrics.web.server.request.metric-name属性来自定义名称。

    比如访问/actuator/metrics/http.server.requests

    {
        "name": "http.server.requests",
        "description": null,
        "baseUnit": "seconds",
        "measurements": [
            {
                "statistic": "COUNT",
                "value": 34.0
            },
            {
                "statistic": "TOTAL_TIME",
                "value": 0.7422510000000001
            },
            {
                "statistic": "MAX",
                "value": 0.0
            }
        ],
        "availableTags": [
            {
                "tag": "exception",
                "values": [
                    "None" // 处理请求时引发的任何异常的简单类名。
                ]
            },
            {
                "tag": "method", // 请求的方法(例如,GET或POST) 
                "values": [
                    "GET"
                ]
            },
            {
                "tag": "uri",// 请求路径
                "values": [
                    "/actuator/metrics/{requiredMetricName}", 
                    "/actuator/metrics",
                    "/app1/test"
                ]
            },
            {
                "tag": "outcome",
                "values": [
                    "CLIENT_ERROR", // 请求的结果,基于响应的状态代码。
                    "SUCCESS"
                ]
            },
            {
                "tag": "status", // 响应的 HTTP 状态代码(例如,200或500)
                "values": [
                    "404",
                    "200"
                ]
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    当然也支持Spring WebFlux

    Tomcat 指标

    Tomcat 指标以tomcat.计量名称发布。

            "tomcat.sessions.active.current",
            "tomcat.sessions.active.max",
            "tomcat.sessions.alive.max",
            "tomcat.sessions.created",
            "tomcat.sessions.expired",
            "tomcat.sessions.rejected"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    数据源指标

    DataSource数据源指标使用前缀为jdbc.connections. ,检测会生成表示池中当前活动、空闲、允许的最大连接数和允许的最小连接数。

            "hikaricp.connections",
            "hikaricp.connections.acquire",
            "hikaricp.connections.active",
            "hikaricp.connections.creation",
            "hikaricp.connections.idle",
            "hikaricp.connections.max",
            "hikaricp.connections.min",
            "hikaricp.connections.pending",
            "hikaricp.connections.timeout",
            "hikaricp.connections.usage",
            "jdbc.connections.max",
            "jdbc.connections.min",
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其他指标

    其他还有很多指标,这里就以一一赘述了,如下:

    • Jersey Server
    • HTTP Client
    • Cache
    • Spring GraphQL
    • Hibernate
    • Spring Data Repository
    • RabbitMQ
    • Spring Integration
    • Kafka
    • MongoDB
    • Jetty
    • Redis

    总结

    Spring Boot使用Micrometer框架进行指标检测,可以采集CPU、JVM、磁盘、数据库、缓存等指标,然后可以对接第三方监控系统,进行数据存储和可视化UI。

  • 相关阅读:
    【红队】ATT&CK - 文件隐藏
    FileInputStream文件字节输入流
    【在线教育】课程封面上传图片到阿里云OSS
    文本对比学习综述
    centos7磁盘挂载及目录扩容
    《程序员数学:斐波那契》—— 为什么不能用斐波那契散列,做数据库路由算法?
    数据结构和算法
    架构师核心-云计算&云上实战(云计算基础、云服务器ECS、云设施实战、云上高并发Web架构)
    alpha融合详解(alpha compositing)
    cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头
  • 原文地址:https://blog.csdn.net/qq_43437874/article/details/126113221