Micrometer 支援

監控與度量

自 4.2 版本以來,Spring Batch 基於 Micrometer 提供了批處理監控和度量支援。本節介紹開箱即用的度量以及如何貢獻自定義度量。

內建度量

度量收集預設停用。要啟用它,您需要在應用程式上下文中定義一個 Micrometer ObservationRegistry bean。通常,您需要定義要使用的 ObservationHandler。以下示例演示如何註冊一個 DefaultMeterObservationHandler,它將把度量儲存在 MeterRegistry 中(例如,Prometheus 登錄檔):

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

Spring Batch 特定度量以 spring.batch 為字首註冊。下表詳細解釋了所有度量:

度量名稱

型別

描述

標籤

spring.batch.job

TIMER

作業執行時長

name, status

spring.batch.job.active

LONG_TASK_TIMER

當前活躍的作業

name

spring.batch.step

TIMER

步驟執行時長

name, job.name, status

spring.batch.step.active

LONG_TASK_TIMER

當前活躍的步驟

name

spring.batch.item.read

TIMER

專案讀取時長

job.name, step.name, status

spring.batch.item.process

TIMER

專案處理時長

job.name, step.name, status

spring.batch.chunk.write

TIMER

塊寫入時長

job.name, step.name, status

spring.batch.job.launch.count

COUNTER

作業啟動計數

不適用

作業和步驟的 status 標籤等於退出狀態。對於專案讀取、處理和寫入,此 status 標籤可以是 SUCCESSFAILURE

自定義度量

如果您想在自定義元件中使用自己的度量,我們建議直接使用 Micrometer API。以下是一個如何計時 Tasklet 的示例:

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();
		}
	}
}

追蹤

自 5 版本以來,Spring Batch 透過 Micrometer 的 Observation API 提供追蹤。預設情況下,追蹤是停用的。要啟用它,您需要定義一個配置了支援追蹤的 ObservationHandler(例如 TracingAwareMeterObservationHandler)的 ObservationRegistry bean。

@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;
}

有了這些,Spring Batch 將為每個作業執行建立一個追蹤,併為每個步驟執行建立一個 span。

如果您不使用 EnableBatchProcessingDefaultBatchConfiguration,則需要在應用程式上下文中註冊一個 BatchObservabilityBeanPostProcessor,它將自動在可觀測的批處理工件中設定 Micrometer 的觀察登錄檔。

© . This site is unofficial and not affiliated with VMware.