actuator–基础–6.2–端点解析–metrics端点
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/actuator-learn/actuator01
1、介绍
用于查看当前应用的各类重要度量指标
1.1、查看当前应用的各类重要度量指标
http://127.0.0.1:8080/actuator/metrics
1.2、各个指标说明
2、查看某个度量的详细信息
2.1、命令
http://127.0.0.1:8080/actuator/metrics/{MetricName}
2.2、案例
查看JVM最大内存的详细信息
http://127.0.0.1:8080/actuator/metrics/jvm.memory.max
3、查看 某个度量 某个区域 的详细信息
用query param的方式查看 某个区域 的详细信息
3.1、案例
# 查看tag=id的Metaspace区域 的详细信息
http://127.0.0.1:8080/actuator/metrics/jvm.memory.max?tag=id:Metaspace
3.1.1、查看tag=id的Metaspace区域 的详细信息
3.1.2、结果
4、添加自定义统计指标
- 除了使用 metrics 端点默认的这些统计指标外,我们还可以实现自定义统计指标。
- Metrics 提供 4 种基本的度量类型:Gauge、Counter、Timer、Summary。
4.1、Gauge(计量器)
- Gauge(计量器)是最简单的度量类型,只有一个简单的返回值
- 用来记录一些对象或者事物的瞬时值。
4.1.1、定义一个计量器
我们在一个 Contoller 使用一个类型为 Gauge 的计数器来记录一个数值
@RestController
public class GaugeController {
@GetMapping("/gauge/hello")
public String hello() {
Metrics.gauge("user.test.gauge", 3);
return " gauge hello";
}
}
4.1.2、执行请求
http://127.0.0.1:8080/gauge/hello
4.1.3、查看自定义指标
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.gauge
4.2、Counter(计数器)
- Counter(计数器)简单理解就是一种只增不减的计数器。
- 通常用于记录服务的请求数量、完成的任务数量、错误的发生数量等等。
4.2.1、代码
/**
* 通过http请求,触发这个服务
*/
@RestController
public class MyCounterController {
@Autowired
MyCounterService myCounterService;
@GetMapping("/counter/hello")
public String hello() {
// 触发这个服务
myCounterService.processCollectResult();
return " counter hello";
}
}
/**
* 自定义一个计数器服务:
*/
@Service
public class MyCounterService {
static final Counter userCounter = Metrics.counter("user.counter.total", "services", "demo");
// 自定义一个计数器服务
public void processCollectResult() {
userCounter.increment(1D);
}
}
4.2.2、查看自定义的指标
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
4.2.3、查看自定义的指标明细
# 执行这个请求7次
http://127.0.0.1:8080/counter/hello
# 查看自定义的指标明细,可以看到计数次数为7
http://127.0.0.1:8080/actuator/metrics/user.counter.total
4.3、Timer(计时器)
- 可以同时测量一个特定的代码逻辑块的调用(执行)速度和它的时间分布。
- 简单来说,就是在调用结束的时间点记录整个调用块执行的总时间
- 适用于测量短时间执行的事件的耗时分布,例如消息队列消息的消费速率。
4.3.1、定义一个计时器
@RestController
public class TimeController {
private Timer timer = Metrics.timer("user.test.timer", "timer", "timersample");
@GetMapping("/time/hello")
public String hello() {
// 执行createOrder方法并记录执行时间
timer.record(() -> createOrder());
return " time hello";
}
// 模拟方法耗时
private void createOrder() {
try {
// 耗时3秒
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}
}
}
4.3.2、执行请求
# 请求3次
http://127.0.0.1:8080/time/hello
4.3.3、查看自定义度量的信息
# 查看自定义指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.timer
4.4、Summary(摘要)
- Summary(摘要)用于跟踪事件的分布
- 它类似于一个计时器,但更一般的情况是,它的大小并不一定是一段时间的测量值。
- 在 micrometer 中,对应的类是 DistributionSummary,它的用法有点像 Timer,但是记录的值是需要直接指定,而不是通过测量一个任务的执行时间。
4.4.1、定义一个Summary
@RestController
public class SummaryController {
private DistributionSummary summary = Metrics.summary("user.test.summary", "summary", "summarysample");
@GetMapping("/summary/hello")
public String hello() {
// 使用 Summary 来连续记录三次值
summary.record(2D);
summary.record(3D);
summary.record(4D);
return " summary hello";
}
}
4.4.2、查看自定义指标
# 执行请求,这里执行了16次
http://127.0.0.1:8080/summary/hello
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.summary