Monitoring and metrics
自 4.2 版本以来,Spring Batch 提供对基于 Micrometer 的批处理监控和指标的支持。本部分介绍了哪些指标开箱即用,以及如何提供自定义指标。
Since version 4.2, Spring Batch provides support for batch monitoring and metrics based on Micrometer. This section describes which metrics are provided out-of-the-box and how to contribute custom metrics.
Built-in metrics
指标收集不需要任何特定配置。框架提供的所有指标均注册在 Micrometer’s global registry 中,其前缀为 spring.batch
。下表详细解释了所有指标:
Metrics collection does not require any specific configuration. All metrics provided
by the framework are registered in
Micrometer’s global registry
under the spring.batch
prefix. The following table explains all the metrics in details:
Metric Name |
Type |
Description |
Tags |
|
|
Duration of job execution |
|
|
|
Currently active jobs |
|
|
|
Duration of step execution |
|
|
|
Currently active step |
|
|
|
Duration of item reading |
|
|
|
Duration of item processing |
|
|
|
Duration of chunk writing |
|
|
The |
Custom metrics
如果您想在自定义组件中使用自己的指标,我们建议直接使用 Micrometer API。以下是一个对 Tasklet
进行时间测量的示例:
If you want to use your own metrics in your custom components, we recommend using
Micrometer APIs directly. The following is an example of how to time a 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
指标收集与日志记录类似。禁用日志通常是通过配置日志记录库来完成的,对于指标也是如此。Spring Batch 中没有用于禁用 Micrometer 指标的功能。应该在 Micrometer 端执行此操作。由于 Spring Batch 使用 spring.batch
前缀将指标存储在 Micrometer 的全局注册表中,您可以使用以下代码片段配置 Micrometer 以忽略或拒绝批处理指标:
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"))
有关详细信息,请参阅 Micrometer 的 reference documentation。
See Micrometer’s reference documentation for more details.