Monitoring and metrics
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
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 |
|
|
|
Duration of item reading |
|
|
|
Duration of item processing |
|
|
|
Duration of chunk writing |
|
The status tag can be either SUCCESS or FAILURE .
|
Custom metrics
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
:
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.