[toc]
介绍
可观测性是微服务架构的关键特征,应用程序指标是程序可观察性的一个维度,当应用程序在生产环境中运行时,我们可能想知道各种操作指标,如内存、CPU、线程池使用率等,以及业务指标,例如对特定操作发出了多少请求。
一、配置指标
要添加对指标的支持,我们需要添加maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
我们还需要启用指标端点,如下所示 -
management:
endpoints:
web:
exposure:
include: metrics
下面我们通过url地址访问:
curl localhost:8080/actuator/metrics | jq .
获取返回结果如下:
{
"names": [
"application.ready.time",
"application.started.time",
"disk.free",
"disk.total",
"executor.active",
"executor.completed",
"executor.pool.core",
"executor.pool.max",
"executor.pool.size",
"executor.queue.remaining",
"executor.queued",
"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",
"http.server.requests",
"jdbc.connections.active",
"jdbc.connections.idle",
"jdbc.connections.max",
"jdbc.connections.min",
"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",
"logback.events",
"process.cpu.usage",
"process.files.max",
"process.files.open",
"process.start.time",
"process.uptime",
"system.cpu.count",
"system.cpu.usage",
"system.load.average.1m",
"tomcat.sessions.active.current",
"tomcat.sessions.active.max",
"tomcat.sessions.alive.max",
"tomcat.sessions.created",
"tomcat.sessions.expired",
"tomcat.sessions.rejected"
]
}
这些是 spring boot 开箱即用提供的指标。我们可以看到它包括 jvm 内存、线程、cpu 使用率等。
以下请求将显示已使用的 jvm 内存:
请求地址:
curl 'http://localhost:8080/actuator/metrics/jvm.memory.max'| jq .
返回相应:
{
"name": "jvm.memory.max",
"description": "The maximum amount of memory in bytes that can be used for memory management",
"baseUnit": "bytes",
"measurements": [
{
"statistic": "VALUE",
"value": 5620367357
}
],
"availableTags": [
{
"tag": "area",
"values": [
"heap",
"nonheap"
]
},
{
"tag": "id",
"values": [
"CodeHeap 'profiled nmethods'",
"G1 Old Gen",
"CodeHeap 'non-profiled nmethods'",
"G1 Survivor Space",
"Compressed Class Space",
"Metaspace",
"G1 Eden Space",
"CodeHeap 'non-nmethods'"
]
}
]
}
指标可以有多个维度。例如,jvm.memory.max具有堆大小和非堆大小。我们可以使用其标签向下钻取到指标,例如 .
请求地址:
curl 'http://localhost:8080/actuator/metrics/jvm.memory.max?tag=area:heap' | jq .
返回相应:
{
"name": "jvm.memory.max",
"description": "The maximum amount of memory in bytes that can be used for memory management",
"baseUnit": "bytes",
"measurements": [
{
"statistic": "VALUE",
"value": 4294967294
}
],
"availableTags": [
{
"tag": "id",
"values": [
"G1 Old Gen",
"G1 Survivor Space",
"G1 Eden Space"
]
}
]
}
到目前为止,我们知道 spring boot 公开了指标,我们可以请求指标端点来获取这些指标,如果需要,我们可以使用可用的标签向下钻取到此指标。
二、自定义指标
如果我们需要更多指标怎么办?千分尺 (https://micrometer.io/),负责生成和公开指标。MeterRegistry 是容纳多个米的千分尺的核心概念。我们可以简单地在我们的自定义指标提供程序中注入 MeterRegistry 的实例,如下所示:
@Component
public class InventoryMetrics{
private Counter inventoryCounter;
public InventoryMetrics(MeterRegistry meterRegistry){
inventoryCounter = Counter.builder("products")
.description("Product Count")
.register(meterRegistry);
}
public void inventoryAdded(int count){
inventoryCounter.increment(count);
}
}
在这里,我们创建了一个名为 products 的新指标,每次添加新产品时,我们都会递增值。现在,如果我们请求我们的指标,则会得到产品计数
请求地址:
curl 'http://localhost:8080/actuator/metrics/products' | jq .
返回相应:
{
"name": "products",
"description": "Product Count",
"baseUnit": null,
"measurements": [
{
"statistic": "COUNT",
"value": 2
}
],
"availableTags": []
}
三、指标存储
在生产环境中,我们希望将指标流式传输到数据存储,例如 elasticsearch、influxdb 等。 Spring Boot 支持开箱即用的各种数据接收器。本篇文章中,我在本地运行了一个 influxdb docker 映像,我们可以看到我们的自定义指标被流入推送进来:
在应用程序端,配置如下所示 -
management:
metrics:
export:
influx:
uri: 'http://localhost:8086'
token: '<your-token>'
bucket: '<your bucket>'
org: '<your org>'
autoCreateDb: false
四、其他指标
计数器:计数器是单调递增的指标。它可以按固定的量递增,该量始终为正数。
仪表: 仪表是瞬时指标,可以上升或下降。
计时器:计时器是持续时间较短的延迟和事件频率。
小节
本节我们学习的时候微服务中的指标监测,指标是微服务中重要组成部分,Spring Boot 可以轻松收集指标并将其暴露给各种数据接收器。在生产中,我们需要了解指标的数量,如果数量和频率很高,我们的仪表板可能会变得非常慢,所以我们还应该考虑指标数值的保留策略,请不要存储不必要的旧数据。这将有助于节省一些存储空间。