Skip to content

Commit 621ec92

Browse files
committed
Add Micrometer counter for job launches
Resolves #4226
1 parent d444a8a commit 621ec92

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.time.Duration;
1919

20+
import io.micrometer.core.instrument.Counter;
21+
import io.micrometer.core.instrument.MeterRegistry;
22+
import io.micrometer.core.instrument.Metrics;
2023
import org.apache.commons.logging.Log;
2124
import org.apache.commons.logging.LogFactory;
2225

@@ -73,6 +76,10 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
7376

7477
private TaskExecutor taskExecutor;
7578

79+
private MeterRegistry meterRegistry = Metrics.globalRegistry;
80+
81+
private Counter jobLaunchCount; // NoopCounter is still incubating
82+
7683
/**
7784
* Run the provided job with the given {@link JobParameters}. The
7885
* {@link JobParameters} will be used to determine if this is an execution of an
@@ -96,6 +103,9 @@ public JobExecution run(final Job job, final JobParameters jobParameters)
96103

97104
Assert.notNull(job, "The Job must not be null.");
98105
Assert.notNull(jobParameters, "The JobParameters must not be null.");
106+
if (this.jobLaunchCount != null) {
107+
this.jobLaunchCount.increment();
108+
}
99109

100110
final JobExecution jobExecution;
101111
JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
@@ -202,6 +212,16 @@ public void setTaskExecutor(TaskExecutor taskExecutor) {
202212
this.taskExecutor = taskExecutor;
203213
}
204214

215+
/**
216+
* Set the meter registry to use for metrics. Defaults to
217+
* {@link Metrics#globalRegistry}.
218+
* @param meterRegistry the meter registry
219+
* @since 5.0
220+
*/
221+
public void setMeterRegistry(MeterRegistry meterRegistry) {
222+
this.meterRegistry = meterRegistry;
223+
}
224+
205225
/**
206226
* Ensure the required dependencies of a {@link JobRepository} have been set.
207227
*/
@@ -212,6 +232,7 @@ public void afterPropertiesSet() throws Exception {
212232
logger.info("No TaskExecutor has been set, defaulting to synchronous executor.");
213233
taskExecutor = new SyncTaskExecutor();
214234
}
235+
this.jobLaunchCount = BatchMetrics.createCounter(this.meterRegistry, "job.launch.count", "Job launch count");
215236
}
216237

217238
}

spring-batch-core/src/main/java/org/springframework/batch/core/observability/BatchMetrics.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import io.micrometer.core.instrument.Counter;
2324
import io.micrometer.core.instrument.LongTaskTimer;
2425
import io.micrometer.core.instrument.MeterRegistry;
2526
import io.micrometer.core.instrument.Tag;
@@ -71,6 +72,20 @@ public static Timer createTimer(MeterRegistry meterRegistry, String name, String
7172
.register(meterRegistry);
7273
}
7374

75+
/**
76+
* Create a {@link Counter}.
77+
* @param meterRegistry the meter registry to use
78+
* @param name of the counter. Will be prefixed with
79+
* {@link BatchMetrics#METRICS_PREFIX}.
80+
* @param description of the counter
81+
* @param tags of the counter
82+
* @return a new timer instance
83+
*/
84+
public static Counter createCounter(MeterRegistry meterRegistry, String name, String description, Tag... tags) {
85+
return Counter.builder(METRICS_PREFIX + name).description(description).tags(Arrays.asList(tags))
86+
.register(meterRegistry);
87+
}
88+
7489
/**
7590
* Create a new {@link Observation}. It's not started, you must explicitly call
7691
* {@link Observation#start()} to start it.

spring-batch-core/src/test/java/org/springframework/batch/core/observability/BatchMetricsTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4646
import org.springframework.context.annotation.Bean;
4747
import org.springframework.context.annotation.Configuration;
48-
import org.springframework.context.annotation.Import;
4948
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5049
import org.springframework.jdbc.support.JdbcTransactionManager;
5150
import org.springframework.transaction.PlatformTransactionManager;
@@ -60,7 +59,7 @@
6059
*/
6160
class BatchMetricsTests {
6261

63-
private static final int EXPECTED_SPRING_BATCH_METRICS = 10;
62+
private static final int EXPECTED_SPRING_BATCH_METRICS = 11;
6463

6564
@Test
6665
void testCalculateDuration() {
@@ -149,6 +148,9 @@ void testBatchMetrics() throws Exception {
149148

150149
// Job metrics
151150

151+
assertDoesNotThrow(() -> Metrics.globalRegistry.get("spring.batch.job.launch.count").counter(),
152+
"There should be a meter of type COUNTER named spring.batch.job.launch.count registered in the global registry");
153+
152154
assertDoesNotThrow(
153155
() -> Metrics.globalRegistry.get("spring.batch.job").tag("spring.batch.job.name", "job")
154156
.tag("spring.batch.job.status", "COMPLETED").timer(),

0 commit comments

Comments
 (0)