Skip to content

Commit ef42bb1

Browse files
Uses TagsProvider
1 parent 7072e46 commit ef42bb1

File tree

11 files changed

+269
-20
lines changed

11 files changed

+269
-20
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
<spring-framework.version>6.0.0-M2</spring-framework.version>
5555
<spring-retry.version>1.3.1</spring-retry.version>
5656
<spring-integration.version>6.0.0-M1</spring-integration.version>
57-
<micrometer.version>2.0.0-M2</micrometer.version>
58-
<micrometer-tracing.version>1.0.0-M2</micrometer-tracing.version>
57+
<micrometer.version>2.0.0-SNAPSHOT</micrometer.version>
58+
<micrometer-tracing.version>1.0.0-SNAPSHOT</micrometer-tracing.version>
5959
<jackson.version>2.13.1</jackson.version>
6060

6161
<!-- optional production dependencies -->

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* @author Mahmoud Ben Hassine
6565
*/
6666
public abstract class AbstractJob implements Job, StepLocator, BeanNameAware,
67-
InitializingBean {
67+
InitializingBean, Observation.TagsProviderAware<BatchJobTagsProvider> {
6868

6969
protected static final Log logger = LogFactory.getLog(AbstractJob.class);
7070

@@ -82,6 +82,8 @@ public abstract class AbstractJob implements Job, StepLocator, BeanNameAware,
8282

8383
private StepHandler stepHandler;
8484

85+
private BatchJobTagsProvider tagsProvider = new DefaultBatchJobTagsProvider();
86+
8587
/**
8688
* Default constructor.
8789
*/
@@ -307,7 +309,9 @@ public final void execute(JobExecution execution) {
307309
Tag.of("name", execution.getJobInstance().getJobName()));
308310
LongTaskTimer.Sample longTaskTimerSample = longTaskTimer.start();
309311
Observation observation = BatchMetrics.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName())
310-
.contextualName(execution.getJobInstance().getJobName()).start();
312+
.contextualName(execution.getJobInstance().getJobName())
313+
.tagsProvider(this.tagsProvider)
314+
.start();
311315
try (Observation.Scope scope = observation.openScope()) {
312316

313317
jobParametersValidator.validate(execution.getJobParameters());
@@ -364,7 +368,7 @@ public final void execute(JobExecution execution) {
364368
ExitStatus.NOOP.addExitDescription("All steps already completed or no steps configured for this job.");
365369
execution.setExitStatus(exitStatus.and(newExitStatus));
366370
}
367-
stopTaggedObservation(execution, observation);
371+
stopObservation(execution, observation);
368372
longTaskTimerSample.stop();
369373
execution.setEndTime(new Date());
370374

@@ -383,11 +387,7 @@ public final void execute(JobExecution execution) {
383387

384388
}
385389

386-
private void stopTaggedObservation(JobExecution execution, Observation observation) {
387-
observation.lowCardinalityTag(BatchJobObservation.JobLowCardinalityTags.JOB_NAME.of(execution.getJobInstance().getJobName()))
388-
.lowCardinalityTag(BatchJobObservation.JobLowCardinalityTags.JOB_STATUS.of(execution.getExitStatus().getExitCode()))
389-
.highCardinalityTag(BatchJobObservation.JobHighCardinalityTags.JOB_INSTANCE_ID.of(String.valueOf(execution.getJobInstance().getInstanceId())))
390-
.highCardinalityTag(BatchJobObservation.JobHighCardinalityTags.JOB_EXECUTION_ID.of(String.valueOf(execution.getId())));
390+
private void stopObservation(JobExecution execution, Observation observation) {
391391
List<Throwable> throwables = execution.getFailureExceptions();
392392
if (!throwables.isEmpty()) {
393393
observation.error(mergedThrowables(throwables));
@@ -459,6 +459,11 @@ private void updateStatus(JobExecution jobExecution, BatchStatus status) {
459459
jobRepository.update(jobExecution);
460460
}
461461

462+
@Override
463+
public void setTagsProvider(BatchJobTagsProvider tagsProvider) {
464+
this.tagsProvider = tagsProvider;
465+
}
466+
462467
@Override
463468
public String toString() {
464469
return ClassUtils.getShortName(getClass()) + ": [name=" + name + "]";
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.job;
18+
19+
import io.micrometer.api.instrument.observation.Observation;
20+
21+
import org.springframework.batch.core.JobExecution;
22+
23+
public class BatchJobContext extends Observation.Context {
24+
25+
private final JobExecution jobExecution;
26+
27+
public BatchJobContext(JobExecution jobExecution) {
28+
this.jobExecution = jobExecution;
29+
}
30+
31+
public JobExecution getJobExecution() {
32+
return jobExecution;
33+
}
34+
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2006-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.job;
18+
19+
import io.micrometer.api.instrument.observation.Observation;
20+
21+
import org.springframework.batch.core.Job;
22+
import org.springframework.batch.core.JobExecution;
23+
import org.springframework.batch.core.JobInterruptedException;
24+
import org.springframework.batch.core.StartLimitExceededException;
25+
import org.springframework.batch.core.Step;
26+
import org.springframework.batch.core.StepExecution;
27+
import org.springframework.batch.core.repository.JobRestartException;
28+
29+
/**
30+
* {@link Observation.TagsProvider} for {@link BatchJobContext}.
31+
*
32+
* @author Marcin Grzejszczak
33+
*/
34+
public interface BatchJobTagsProvider extends Observation.TagsProvider<BatchJobContext> {
35+
36+
@Override
37+
default boolean supportsContext(Observation.Context context) {
38+
return context instanceof BatchJobContext;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2011-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.job;
17+
18+
import io.micrometer.api.instrument.Tags;
19+
20+
import org.springframework.batch.core.JobExecution;
21+
22+
/**
23+
* Default {@link BatchJobTagsProvider} implementation.
24+
*
25+
* @author Marcin Grzejszczak
26+
*/
27+
public class DefaultBatchJobTagsProvider implements BatchJobTagsProvider {
28+
@Override
29+
public Tags getLowCardinalityTags(BatchJobContext context) {
30+
JobExecution execution = context.getJobExecution();
31+
return Tags.of(BatchJobObservation.JobLowCardinalityTags.JOB_NAME.of(execution.getJobInstance().getJobName()),
32+
BatchJobObservation.JobLowCardinalityTags.JOB_STATUS.of(execution.getExitStatus().getExitCode()));
33+
}
34+
35+
@Override
36+
public Tags getHighCardinalityTags(BatchJobContext context) {
37+
JobExecution execution = context.getJobExecution();
38+
return Tags.of(BatchJobObservation.JobHighCardinalityTags.JOB_INSTANCE_ID.of(String.valueOf(execution.getJobInstance().getInstanceId())),
39+
BatchJobObservation.JobHighCardinalityTags.JOB_EXECUTION_ID.of(String.valueOf(execution.getId())));
40+
}
41+
42+
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* @author Chris Schaefer
5656
* @author Mahmoud Ben Hassine
5757
*/
58-
public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware {
58+
public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware, Observation.TagsProviderAware<BatchStepTagsProvider> {
5959

6060
private static final Log logger = LogFactory.getLog(AbstractStep.class);
6161

@@ -69,6 +69,8 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw
6969

7070
private JobRepository jobRepository;
7171

72+
private BatchStepTagsProvider tagsProvider = new DefaultBatchStepTagsProvider();
73+
7274
/**
7375
* Default constructor.
7476
*/
@@ -194,7 +196,9 @@ public final void execute(StepExecution stepExecution) throws JobInterruptedExce
194196
stepExecution.setStartTime(new Date());
195197
stepExecution.setStatus(BatchStatus.STARTED);
196198
Observation observation = BatchMetrics.createObservation(BatchStepObservation.BATCH_STEP_OBSERVATION.getName())
197-
.contextualName(stepExecution.getStepName()).start();
199+
.contextualName(stepExecution.getStepName())
200+
.tagsProvider(this.tagsProvider)
201+
.start();
198202
getJobRepository().update(stepExecution);
199203

200204
// Start with a default value that will be trumped by anything
@@ -262,7 +266,7 @@ public final void execute(StepExecution stepExecution) throws JobInterruptedExce
262266
logger.error(String.format("Encountered an error saving batch meta data for step %s in job %s. "
263267
+ "This job is now in an unknown state and should not be restarted.", name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
264268
}
265-
stopTaggedObservation(stepExecution, observation);
269+
stopObservation(stepExecution, observation);
266270
stepExecution.setEndTime(new Date());
267271
stepExecution.setExitStatus(exitStatus);
268272
Duration stepExecutionDuration = BatchMetrics.calculateDuration(stepExecution.getStartTime(), stepExecution.getEndTime());
@@ -296,11 +300,7 @@ public final void execute(StepExecution stepExecution) throws JobInterruptedExce
296300
}
297301
}
298302

299-
private void stopTaggedObservation(StepExecution stepExecution, Observation observation) {
300-
observation.lowCardinalityTag(BatchStepObservation.StepLowCardinalityTags.STEP_NAME.of(stepExecution.getStepName()))
301-
.lowCardinalityTag(BatchStepObservation.StepLowCardinalityTags.JOB_NAME.of(stepExecution.getJobExecution().getJobInstance().getJobName()))
302-
.lowCardinalityTag(BatchStepObservation.StepLowCardinalityTags.STEP_STATUS.of(stepExecution.getExitStatus().getExitCode()))
303-
.highCardinalityTag(BatchStepObservation.StepHighCardinalityTags.STEP_EXECUTION_ID.of(String.valueOf(stepExecution.getId())));
303+
private void stopObservation(StepExecution stepExecution, Observation observation) {
304304
List<Throwable> throwables = stepExecution.getFailureExceptions();
305305
if (!throwables.isEmpty()) {
306306
observation.error(mergedThrowables(throwables));
@@ -408,4 +408,8 @@ else if (ex instanceof NoSuchJobException || ex.getCause() instanceof NoSuchJobE
408408
return exitStatus;
409409
}
410410

411+
@Override
412+
public void setTagsProvider(BatchStepTagsProvider tagsProvider) {
413+
this.tagsProvider = tagsProvider;
414+
}
411415
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2013-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.step;
18+
19+
import io.micrometer.api.instrument.observation.Observation;
20+
21+
import org.springframework.batch.core.StepExecution;
22+
23+
public class BatchStepContext extends Observation.Context {
24+
25+
private final StepExecution stepExecution;
26+
27+
public BatchStepContext(StepExecution stepExecution) {
28+
this.stepExecution = stepExecution;
29+
}
30+
31+
public StepExecution getStepExecution() {
32+
return stepExecution;
33+
}
34+
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2006-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.step;
18+
19+
import io.micrometer.api.instrument.observation.Observation;
20+
21+
/**
22+
* {@link Observation.TagsProvider} for {@link BatchStepContext}.
23+
*
24+
* @author Marcin Grzejszczak
25+
*/
26+
public interface BatchStepTagsProvider extends Observation.TagsProvider<BatchStepContext> {
27+
28+
@Override
29+
default boolean supportsContext(Observation.Context context) {
30+
return context instanceof BatchStepContext;
31+
}
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2011-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.step;
17+
18+
import io.micrometer.api.instrument.Tags;
19+
20+
import org.springframework.batch.core.StepExecution;
21+
22+
/**
23+
* Default {@link BatchStepTagsProvider} implementation.
24+
*
25+
* @author Marcin Grzejszczak
26+
*/
27+
public class DefaultBatchStepTagsProvider implements BatchStepTagsProvider {
28+
@Override
29+
public Tags getLowCardinalityTags(BatchStepContext context) {
30+
StepExecution execution = context.getStepExecution();
31+
return Tags.of(BatchStepObservation.StepLowCardinalityTags.STEP_NAME.of(execution.getStepName()),
32+
BatchStepObservation.StepLowCardinalityTags.JOB_NAME.of(execution.getJobExecution().getJobInstance().getJobName()),
33+
BatchStepObservation.StepLowCardinalityTags.STEP_STATUS.of(execution.getExitStatus().getExitCode()));
34+
}
35+
36+
@Override
37+
public Tags getHighCardinalityTags(BatchStepContext context) {
38+
StepExecution execution = context.getStepExecution();
39+
return Tags.of(BatchStepObservation.StepHighCardinalityTags.STEP_EXECUTION_ID.of(String.valueOf(execution.getId())));
40+
}
41+
42+
}

spring-batch-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,11 @@
9292
<version>${micrometer-tracing.version}</version>
9393
<scope>test</scope>
9494
</dependency>
95+
<dependency>
96+
<groupId>io.micrometer</groupId>
97+
<artifactId>micrometer-test</artifactId>
98+
<version>${micrometer.version}</version>
99+
<scope>test</scope>
100+
</dependency>
95101
</dependencies>
96102
</project>

0 commit comments

Comments
 (0)