监控和指标
监控和指标
从版本 4.2 开始,Spring Batch 提供了对批处理监控和指标的支持 基于 Micrometer。本节介绍 哪些指标是开箱即用的,以及如何提供自定义指标。
内置指标
指标采集不需要任何特定配置。提供所有指标
在 Micrometer 的全局注册表中注册的spring.batch
前缀。下表详细介绍了所有指标:
指标名称 |
类型 |
描述 |
标签 |
|
|
任务执行持续时间 |
|
|
|
当前活动的作业 |
|
|
|
步骤执行的持续时间 |
|
|
|
当前活动步骤 |
|
|
|
项目读取的持续时间 |
|
|
|
商品处理的持续时间 |
|
|
|
块写入的持续时间 |
|
这status tag 可以是SUCCESS 或FAILURE . |
自定义指标
如果您想在自定义组件中使用自己的指标,我们建议使用
Micrometer API 直接使用。以下是如何为Tasklet
:
public class MyTimedTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
Timer.Sample sample = Timer.start(Metrics.globalRegistry);
String status = "success";
try {
// do some work
} catch (Exception e) {
// handle exception
status = "failure";
} finally {
sample.stop(Timer.builder("my.tasklet.timer")
.description("Duration of MyTimedTasklet")
.tag("status", status)
.register(Metrics.globalRegistry));
}
return RepeatStatus.FINISHED;
}
}
Disabling Metrics
Metrics collection is a concern similar to logging. Disabling logs is typically
done by configuring the logging library, and this is no different for metrics.
There is no feature in Spring Batch to disable Micrometer’s metrics. This should
be done on Micrometer’s side. Since Spring Batch stores metrics in the global
registry of Micrometer with the spring.batch
prefix, you can configure
micrometer to ignore or deny batch metrics with the following snippet:
Metrics.globalRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("spring.batch"))
See Micrometer’s reference documentation
for more details.
Tracing
As of version 5, Spring Batch provides tracing through Micrometer’s Observation
API. By default, tracing is enabled
when using @EnableBatchProcessing
. Spring Batch will create a trace for each job execution and a span for each
step execution.
If you do not use EnableBatchProcessing
, you need to register a BatchObservabilityBeanPostProcessor
in your
application context, which will automatically setup Micrometer’s observability in your jobs and steps beans.