Skip to content

Commit 61ec4d4

Browse files
Adds missing contextd reference
without this change the BatchJobTagsProvider or BatchStepTagsProvider is not applied because it has a condition on instanceof BatchJobContext or BatchStepContext with this change we're adding the missing context when the observation is created, without this the default context was used
1 parent 1f9325f commit 61ec4d4

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public final void execute(JobExecution execution) {
308308
LongTaskTimer longTaskTimer = BatchMetrics.createLongTaskTimer("job.active", "Active jobs",
309309
Tag.of("name", execution.getJobInstance().getJobName()));
310310
LongTaskTimer.Sample longTaskTimerSample = longTaskTimer.start();
311-
Observation observation = BatchMetrics.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName())
311+
Observation observation = BatchMetrics.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName(), new BatchJobContext(execution))
312312
.contextualName(execution.getJobInstance().getJobName())
313313
.tagsProvider(this.tagsProvider)
314314
.start();

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/metrics/BatchMetrics.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public static Timer createTimer(String name, String description, Tag... tags) {
7777
* in the user code. Otherwise you won't observe any metrics.
7878
* @return a new observation instance
7979
*/
80-
public static Observation createObservation(String name) {
81-
return Observation.createNotStarted(name, Metrics.globalRegistry);
80+
public static Observation createObservation(String name, Observation.Context context) {
81+
return Observation.createNotStarted(name, context, Metrics.globalRegistry);
8282
}
8383

8484
/**

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public final void execute(StepExecution stepExecution) throws JobInterruptedExce
195195
}
196196
stepExecution.setStartTime(new Date());
197197
stepExecution.setStatus(BatchStatus.STARTED);
198-
Observation observation = BatchMetrics.createObservation(BatchStepObservation.BATCH_STEP_OBSERVATION.getName())
198+
Observation observation = BatchMetrics.createObservation(BatchStepObservation.BATCH_STEP_OBSERVATION.getName(), new BatchStepContext(stepExecution))
199199
.contextualName(stepExecution.getStepName())
200200
.tagsProvider(this.tagsProvider)
201201
.start();

Diff for: spring-batch-samples/src/test/java/org/springframework/batch/sample/metrics/BatchMetricsTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import io.micrometer.core.instrument.Meter;
2929
import io.micrometer.core.instrument.Metrics;
30+
import org.junit.BeforeClass;
3031
import org.junit.Test;
3132

3233
import org.springframework.batch.core.ExitStatus;
@@ -60,6 +61,11 @@ public class BatchMetricsTests {
6061

6162
private static final int EXPECTED_SPRING_BATCH_METRICS = 10;
6263

64+
@BeforeClass
65+
public static void setup() {
66+
Metrics.globalRegistry.withTimerObservationHandler();
67+
}
68+
6369
@Test
6470
public void testCalculateDuration() {
6571
LocalDateTime startTime = LocalDateTime.now();

0 commit comments

Comments
 (0)