Micrometer support

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

Built-in metrics

Metrics collection is disabled by default. To enable it, you need to define a Micrometer ObservationRegistry bean in your application context. Typically, you would need to define which ObservationHandler to use. The following example shows how to register a DefaultMeterObservationHandler that will store metrics in a MeterRegistry (for example, a Prometheus registry):spring-doc.cn

@Bean
public ObservationRegistry observationRegistry(MeterRegistry meterRegistry) {
    ObservationRegistry observationRegistry = ObservationRegistry.create();
    observationRegistry.observationConfig()
        .observationHandler(new DefaultMeterObservationHandler(meterRegistry));
    return observationRegistry;
}

Spring Batch specific metrics are registered 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 jobspring-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

spring.batch.job.launch.countspring-doc.cn

COUNTERspring-doc.cn

Job launch countspring-doc.cn

N/Aspring-doc.cn

The status tag for jobs and steps is equal to the exit status. For item reading, processing and writing, this 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.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

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 {

    private ObservationRegistry observationRegistry;

    public MyTimedTasklet(ObservationRegistry observationRegistry) {
        this.observationRegistry = observationRegistry;
    }

	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
		Observation observation = Observation.start("my.tasklet.step", this.observationRegistry);
		try (Observation.Scope scope = observation.openScope()) {
			// do some work
		    return RepeatStatus.FINISHED;
		} catch (Exception e) {
			// handle exception
            observation.error(exception);
		} finally {
			observation.stop();
		}
	}
}

Tracing

As of version 5, Spring Batch provides tracing through Micrometer’s Observation API. By default, tracing is disabled. To enable it, you need to define an ObservationRegistry bean configured with an ObservationHandler that supports tracing, such as TracingAwareMeterObservationHandler:spring-doc.cn

@Bean
public ObservationRegistry observationRegistry(MeterRegistry meterRegistry, Tracer tracer) {
    DefaultMeterObservationHandler observationHandler = new DefaultMeterObservationHandler(meterRegistry);
    ObservationRegistry observationRegistry = ObservationRegistry.create();
    observationRegistry.observationConfig()
            .observationHandler(new TracingAwareMeterObservationHandler<>(observationHandler, tracer));
    return observationRegistry;
}

With that in place, Spring Batch will create a trace for each job execution and a span for each step execution.spring-doc.cn

If you do not use EnableBatchProcessing or DefaultBatchConfiguration, you need to register a BatchObservabilityBeanPostProcessor in your application context, which will automatically set Micrometer’s observation registry in observable batch artefacts.spring-doc.cn