Add metrics sample
Issue BATCH-2774
This commit is contained in:
committed by
Michael Minella
parent
4de1a9056e
commit
96ec0d0d27
@@ -0,0 +1,31 @@
|
||||
package org.springframework.batch.sample.metrics;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
@EnableScheduling
|
||||
@EnableBatchProcessing
|
||||
@Import({Job1Configuration.class, Job2Configuration.class, JobScheduler.class, PrometheusConfiguration.class})
|
||||
@PropertySource("metrics-sample.properties")
|
||||
public class BatchMetricsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BatchMetricsApplication.class);
|
||||
applicationContext.start();
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
public ThreadPoolTaskScheduler taskScheduler(@Value("${thread.pool.size}") int threadPoolSize) {
|
||||
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
threadPoolTaskScheduler.setPoolSize(threadPoolSize);
|
||||
return threadPoolTaskScheduler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.springframework.batch.sample.metrics;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class Job1Configuration {
|
||||
|
||||
private Random random;
|
||||
private JobBuilderFactory jobs;
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
public Job1Configuration(JobBuilderFactory jobs, StepBuilderFactory steps) {
|
||||
this.jobs = jobs;
|
||||
this.steps = steps;
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job1() {
|
||||
return jobs.get("job1")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return steps.get("step1")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("hello");
|
||||
// simulate processing time
|
||||
Thread.sleep(random.nextInt(3000));
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return steps.get("step2")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("world");
|
||||
// simulate step failure
|
||||
int nextInt = random.nextInt(3000);
|
||||
Thread.sleep(nextInt);
|
||||
if (nextInt % 5 == 0) {
|
||||
throw new Exception("Boom!");
|
||||
}
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.springframework.batch.sample.metrics;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepScope;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.support.ListItemReader;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class Job2Configuration {
|
||||
|
||||
private Random random;
|
||||
private JobBuilderFactory jobs;
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
public Job2Configuration(JobBuilderFactory jobs, StepBuilderFactory steps) {
|
||||
this.jobs = jobs;
|
||||
this.steps = steps;
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job2() {
|
||||
return jobs.get("job2")
|
||||
.start(step())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step() {
|
||||
return steps.get("step1")
|
||||
.<Integer, Integer>chunk(3)
|
||||
.reader(itemReader())
|
||||
.writer(itemWriter())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public ListItemReader<Integer> itemReader() {
|
||||
List<Integer> items = new LinkedList<>();
|
||||
// read a random number of items in each run
|
||||
for (int i = 0; i < random.nextInt(100); i++) {
|
||||
items.add(i);
|
||||
}
|
||||
return new ListItemReader<>(items);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemWriter<Integer> itemWriter() {
|
||||
return items -> {
|
||||
for (Integer item : items) {
|
||||
int nextInt = random.nextInt(1000);
|
||||
Thread.sleep(nextInt);
|
||||
// simulate write failure
|
||||
if (nextInt % 57 == 0) {
|
||||
throw new Exception("Boom!");
|
||||
}
|
||||
System.out.println("item = " + item);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.springframework.batch.sample.metrics;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class JobScheduler {
|
||||
|
||||
private final Job job1;
|
||||
private final Job job2;
|
||||
private final JobLauncher jobLauncher;
|
||||
|
||||
@Autowired
|
||||
public JobScheduler(Job job1, Job job2, JobLauncher jobLauncher) {
|
||||
this.job1 = job1;
|
||||
this.job2 = job2;
|
||||
this.jobLauncher = jobLauncher;
|
||||
}
|
||||
|
||||
@Scheduled(cron="*/10 * * * * *")
|
||||
public void launchJob1() throws Exception {
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addLong("time", System.currentTimeMillis())
|
||||
.toJobParameters();
|
||||
|
||||
jobLauncher.run(job1, jobParameters);
|
||||
}
|
||||
|
||||
@Scheduled(cron="*/15 * * * * *")
|
||||
public void launchJob2() throws Exception {
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addLong("time", System.currentTimeMillis())
|
||||
.toJobParameters();
|
||||
|
||||
jobLauncher.run(job2, jobParameters);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.springframework.batch.sample.metrics;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import io.micrometer.prometheus.PrometheusConfig;
|
||||
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
||||
import io.prometheus.client.CollectorRegistry;
|
||||
import io.prometheus.client.exporter.PushGateway;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
@Configuration
|
||||
public class PrometheusConfiguration {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusConfiguration.class);
|
||||
|
||||
@Value("${prometheus.job.name}")
|
||||
private String prometheusJobName;
|
||||
|
||||
@Value("${prometheus.grouping.key}")
|
||||
private String prometheusGroupingKey;
|
||||
|
||||
@Value("${prometheus.pushgateway.url}")
|
||||
private String prometheusPushGatewayUrl;
|
||||
|
||||
private Map<String, String> groupingKey = new HashMap<>();
|
||||
private PushGateway pushGateway;
|
||||
private CollectorRegistry collectorRegistry;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
pushGateway = new PushGateway(prometheusPushGatewayUrl);
|
||||
groupingKey.put(prometheusGroupingKey, prometheusJobName);
|
||||
PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
|
||||
collectorRegistry = prometheusMeterRegistry.getPrometheusRegistry();
|
||||
Metrics.globalRegistry.add(prometheusMeterRegistry);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRateString = "${prometheus.push.rate}")
|
||||
public void pushMetrics() {
|
||||
try {
|
||||
pushGateway.pushAdd(collectorRegistry, prometheusJobName, groupingKey);
|
||||
} catch (Throwable ex) {
|
||||
LOGGER.error("Unable to push metrics to Prometheus Push Gateway", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
thread.pool.size=3
|
||||
prometheus.push.rate=5000
|
||||
prometheus.job.name=springbatch
|
||||
prometheus.grouping.key=appname
|
||||
prometheus.pushgateway.url=0.0.0.0:9091
|
||||
Reference in New Issue
Block a user