监控和指标
从版本 4.2 开始,Spring Batch 提供了对批处理监控和指标的支持 基于 Micrometer。本节介绍 哪些指标是开箱即用的,以及如何提供自定义指标。
内置指标
指标采集不需要任何特定配置。提供所有指标
在 Micrometer 的全局注册表中注册的spring.batch
前缀。下表详细介绍了所有指标:
指标名称 |
类型 |
描述 |
标签 |
|
|
任务执行持续时间 |
|
|
|
当前活动的作业 |
|
|
|
步骤执行的持续时间 |
|
|
|
当前活动步骤 |
|
|
|
项目读取的持续时间 |
|
|
|
商品处理的持续时间 |
|
|
|
块写入的持续时间 |
|
这status tag 可以是SUCCESS 或FAILURE . |
自定义指标
如果您想在自定义组件中使用自己的指标,我们建议使用
Micrometer API 直接使用。以下是如何为Tasklet
:
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
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.