This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Batch Documentation 5.1.2!spring-doc.cn

This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Batch Documentation 5.1.2!spring-doc.cn

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.spring-doc.cn

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:spring-doc.cn

Metric Namespring-doc.cn

Typespring-doc.cn

Descriptionspring-doc.cn

Tagsspring-doc.cn

spring.batch.jobspring-doc.cn

TIMERspring-doc.cn

Duration of job executionspring-doc.cn

name, statusspring-doc.cn

spring.batch.job.activespring-doc.cn

LONG_TASK_TIMERspring-doc.cn

Currently active jobsspring-doc.cn

namespring-doc.cn

spring.batch.stepspring-doc.cn

TIMERspring-doc.cn

Duration of step executionspring-doc.cn

name, job.name, statusspring-doc.cn

spring.batch.step.activespring-doc.cn

LONG_TASK_TIMERspring-doc.cn

Currently active stepspring-doc.cn

namespring-doc.cn

spring.batch.item.readspring-doc.cn

TIMERspring-doc.cn

Duration of item readingspring-doc.cn

job.name, step.name, statusspring-doc.cn

spring.batch.item.processspring-doc.cn

TIMERspring-doc.cn

Duration of item processingspring-doc.cn

job.name, step.name, statusspring-doc.cn

spring.batch.chunk.writespring-doc.cn

TIMERspring-doc.cn

Duration of chunk writingspring-doc.cn

job.name, step.name, statusspring-doc.cn

The status tag can be either SUCCESS or FAILURE.

Metric Namespring-doc.cn

Typespring-doc.cn

Descriptionspring-doc.cn

Tagsspring-doc.cn

spring.batch.jobspring-doc.cn

TIMERspring-doc.cn

Duration of job executionspring-doc.cn

name, statusspring-doc.cn

spring.batch.job.activespring-doc.cn

LONG_TASK_TIMERspring-doc.cn

Currently active jobsspring-doc.cn

namespring-doc.cn

spring.batch.stepspring-doc.cn

TIMERspring-doc.cn

Duration of step executionspring-doc.cn

name, job.name, statusspring-doc.cn

spring.batch.step.activespring-doc.cn

LONG_TASK_TIMERspring-doc.cn

Currently active stepspring-doc.cn

namespring-doc.cn

spring.batch.item.readspring-doc.cn

TIMERspring-doc.cn

Duration of item readingspring-doc.cn

job.name, step.name, statusspring-doc.cn

spring.batch.item.processspring-doc.cn

TIMERspring-doc.cn

Duration of item processingspring-doc.cn

job.name, step.name, statusspring-doc.cn

spring.batch.chunk.writespring-doc.cn

TIMERspring-doc.cn

Duration of chunk writingspring-doc.cn

job.name, step.name, statusspring-doc.cn

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:spring-doc.cn

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:spring-doc.cn

Metrics.globalRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("spring.batch"))

See Micrometer’s reference documentation for more details.spring-doc.cn