# 监测和量度
# 监控和度量
自版本 4.2 以来, Spring Batch 提供了对基于Micrometer (opens new window)的批监视和度量的支持。本节描述了哪些度量是开箱即用的,以及如何贡献自定义度量。
# 内置度量
度量集合不需要任何特定的配置。框架提供的所有指标都注册在千分尺的全球注册中心 (opens new window)的spring.batch
前缀下。下表详细解释了所有指标:
Metric Name | Type | Description | 标签 |
---|---|---|---|
spring.batch.job | TIMER | Duration of job execution | name , status |
spring.batch.job.active | LONG_TASK_TIMER | Currently active jobs | name |
spring.batch.step | TIMER | Duration of step execution | name , job.name , status |
spring.batch.item.read | TIMER | Duration of item reading | job.name , step.name , status |
spring.batch.item.process | TIMER | Duration of item processing | job.name , step.name , status |
spring.batch.chunk.write | TIMER | Duration of chunk writing | job.name , step.name , status |
status 标记可以是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;
}
}
# 禁用度量
度量收集是一个类似于日志记录的问题。禁用日志通常是通过配置日志记录库来完成的,对于度量标准来说也是如此。在 Spring 批处理中没有禁用千分尺的度量的功能,这应该在千分尺的一侧完成。由于 Spring 批处理将度量存储在带有spring.batch
前缀的 Micrometer 的全局注册中心中,因此可以通过以下代码片段将 Micrometer 配置为忽略/拒绝批处理度量:
Metrics.globalRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("spring.batch"))
有关更多详情,请参阅千分尺的参考文献 (opens new window)。