@@ -17,6 +17,9 @@ package org.springframework.batch.core.launch.support;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -73,6 +76,10 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
|
||||
|
||||
private TaskExecutor taskExecutor;
|
||||
|
||||
private MeterRegistry meterRegistry = Metrics.globalRegistry;
|
||||
|
||||
private Counter jobLaunchCount; // NoopCounter is still incubating
|
||||
|
||||
/**
|
||||
* Run the provided job with the given {@link JobParameters}. The
|
||||
* {@link JobParameters} will be used to determine if this is an execution of an
|
||||
@@ -96,6 +103,9 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
|
||||
|
||||
Assert.notNull(job, "The Job must not be null.");
|
||||
Assert.notNull(jobParameters, "The JobParameters must not be null.");
|
||||
if (this.jobLaunchCount != null) {
|
||||
this.jobLaunchCount.increment();
|
||||
}
|
||||
|
||||
final JobExecution jobExecution;
|
||||
JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
|
||||
@@ -202,6 +212,16 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
|
||||
this.taskExecutor = taskExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the meter registry to use for metrics. Defaults to
|
||||
* {@link Metrics#globalRegistry}.
|
||||
* @param meterRegistry the meter registry
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setMeterRegistry(MeterRegistry meterRegistry) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the required dependencies of a {@link JobRepository} have been set.
|
||||
*/
|
||||
@@ -212,6 +232,7 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
|
||||
logger.info("No TaskExecutor has been set, defaulting to synchronous executor.");
|
||||
taskExecutor = new SyncTaskExecutor();
|
||||
}
|
||||
this.jobLaunchCount = BatchMetrics.createCounter(this.meterRegistry, "job.launch.count", "Job launch count");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.LongTaskTimer;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
@@ -71,6 +72,20 @@ public final class BatchMetrics {
|
||||
.register(meterRegistry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Counter}.
|
||||
* @param meterRegistry the meter registry to use
|
||||
* @param name of the counter. Will be prefixed with
|
||||
* {@link BatchMetrics#METRICS_PREFIX}.
|
||||
* @param description of the counter
|
||||
* @param tags of the counter
|
||||
* @return a new timer instance
|
||||
*/
|
||||
public static Counter createCounter(MeterRegistry meterRegistry, String name, String description, Tag... tags) {
|
||||
return Counter.builder(METRICS_PREFIX + name).description(description).tags(Arrays.asList(tags))
|
||||
.register(meterRegistry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Observation}. It's not started, you must explicitly call
|
||||
* {@link Observation#start()} to start it.
|
||||
|
||||
@@ -45,7 +45,6 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
@@ -60,7 +59,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
*/
|
||||
class BatchMetricsTests {
|
||||
|
||||
private static final int EXPECTED_SPRING_BATCH_METRICS = 10;
|
||||
private static final int EXPECTED_SPRING_BATCH_METRICS = 11;
|
||||
|
||||
@Test
|
||||
void testCalculateDuration() {
|
||||
@@ -149,6 +148,9 @@ class BatchMetricsTests {
|
||||
|
||||
// Job metrics
|
||||
|
||||
assertDoesNotThrow(() -> Metrics.globalRegistry.get("spring.batch.job.launch.count").counter(),
|
||||
"There should be a meter of type COUNTER named spring.batch.job.launch.count registered in the global registry");
|
||||
|
||||
assertDoesNotThrow(
|
||||
() -> Metrics.globalRegistry.get("spring.batch.job").tag("spring.batch.job.name", "job")
|
||||
.tag("spring.batch.job.status", "COMPLETED").timer(),
|
||||
|
||||
Reference in New Issue
Block a user