Spring Batch and Boot千分尺 [英] Spring Batch and boot Micrometer

查看:67
本文介绍了Spring Batch and Boot千分尺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring批处理作业,该作业从DB读取记录,对其进行处理,然后写入另一个数据库.我想测量Spring批处理器和Writer中的总时间/平均时间.根据文档- https://docs.spring.io/spring-batch/docs/current/reference/html/monitoring-and-metrics.html 使用spring.batch前缀即可轻松获得指标.我如何登录到处理器和编写器中所花费的时间到控制台.当我在作业侦听器的末尾从指标注册表中打印指标结果时,我看到作业和步骤状态为已完成",但计时器"统计指标显示为0.要启用计时器,是否需要做任何事情?我无意将这些指标推向普罗米修斯,地图集或任何其他注册表.因此,在pom.xml中仅添加了千分尺核心依赖项.请告知如何记录不同组件的耗时指标.任何示例都将非常有帮助.

I have a Spring batch job that reads records from DB, processes it, and writes to another database. I want to measure the total time / average time taken in the Spring batch processor and Writer. As per the documentation - https://docs.spring.io/spring-batch/docs/current/reference/html/monitoring-and-metrics.html the metrics are readily available using spring.batch prefix. How do I log to the console the time taken in the Processor and writer. When i print the Metrics results from metrics registry at end of job listener, I see the job and step status as Completed but the Timer stats metrics are showing as 0. is there anything that needs to be done to enable Timer? I don't intend to push the metrics to prometheus or atlas or any other registries. So only the micrometer core dependency is added in pom.xml. Please advise how to log the time taken metrics for different components. Any examples would be really helpful.

推荐答案

我看到作业和步骤状态为已完成",但计时器"统计指标显示为0.

I see the job and step status as Completed but the Timer stats metrics are showing as 0.

其原因是,默认情况下,千分尺中的全局注册表为空复合.您需要添加至少一个注册表来保留指标.请参见为什么MicroMeter Timer返回零?

The reason for that is that by default, the global registry in micrometer is an empty composite. You need to add at least one registry to retain metrics. See Why does MicroMeter Timer returns zero?

我不打算将指标推送到 prometheus 或 atlas 或任何其他注册表.

I don't intend to push the metrics to prometheus or atlas or any other registries.

如果要使用指标而不将其推送到指标后端,则可以使用侦听器并直接从全局注册表中获取访问权限.这是您要求的物品处理时间的快速示例:

If you want to consume metrics without pushing them to a metrics backend, you can use a listener and get access from the global registry directly. Here is a quick example for item processing timing as you requested:

import java.util.Arrays;
import java.util.List;

import io.micrometer.core.instrument.Measurement;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import org.springframework.batch.core.ItemProcessListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class MyJob {

    @Bean
    public ItemReader<Integer> itemReader() {
        return new ListItemReader<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    }

    @Bean
    public ItemProcessor<Integer, Integer> itemProcessor() {
        return item -> {
            System.out.println("processing item " + item);
            Thread.sleep(2000);
            return item + 1;
        };
    }

    @Bean
    public ItemWriter<Integer> itemWriter() {
        return items -> {
            for (Integer item : items) {
                System.out.println("writing item = " + item);
            }
        };
    }

    @Bean
    public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
        return jobs.get("job")
                .start(steps.get("step")
                        .<Integer, Integer>chunk(5)
                        .reader(itemReader())
                        .processor(itemProcessor())
                        .writer(itemWriter())
                        .listener(new MonitoringItemProcessListener())
                        .build())
                .build();
    }
    
    static class MonitoringItemProcessListener implements ItemProcessListener<Integer, Integer> {

        @Override
        public void beforeProcess(Integer item) {
            
        }

        @Override
        public void afterProcess(Integer item, Integer result) {
            List<Meter> meters = Metrics.globalRegistry.getMeters();
            for (Meter meter : meters) {
                if (meter.getId().getName().equals("spring.batch.item.process")) {
                    System.out.println("meter description = " + meter.getId().getDescription());
                    Iterable<Measurement> measurements = meter.measure();
                    for (Measurement measurement : measurements) {
                        System.out.println("measurement: statistic = " + measurement.getStatistic() + " | value = " + measurement.getValue());
                    }
                }
            }
        }

        @Override
        public void onProcessError(Integer item, Exception e) {

        }
    }

    public static void main(String[] args) throws Exception {
        Metrics.addRegistry(new SimpleMeterRegistry());
        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job job = context.getBean(Job.class);
        jobLauncher.run(job, new JobParameters());
    }

}

此示例打印类似:

processing item 1
meter description = Item processing duration
measurement: statistic = COUNT | value = 1.0
measurement: statistic = TOTAL_TIME | value = 2.00080715
measurement: statistic = MAX | value = 2.00080715
processing item 2
meter description = Item processing duration
measurement: statistic = COUNT | value = 2.0
measurement: statistic = TOTAL_TIME | value = 4.003516877
measurement: statistic = MAX | value = 2.002709727
processing item 3
meter description = Item processing duration
measurement: statistic = COUNT | value = 3.0
measurement: statistic = TOTAL_TIME | value = 6.005287923
measurement: statistic = MAX | value = 2.002709727

这篇关于Spring Batch and Boot千分尺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆