Skip to content

Commit 5ad29db

Browse files
Mark Bonnekesselsnicoll
authored andcommitted
Auto-configure observation for Spring-Batch
See gh-34305
1 parent 2f54a0c commit 5ad29db

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
plugins {
2-
id "java-library"
2+
id "java-library"
33
id "org.asciidoctor.jvm.convert"
44
id "org.springframework.boot.auto-configuration"
55
id "org.springframework.boot.conventions"
66
id "org.springframework.boot.deployed"
7-
id "org.springframework.boot.optional-dependencies"
7+
id "org.springframework.boot.optional-dependencies"
88
}
99

1010
description = "Spring Boot Actuator AutoConfigure"
@@ -116,6 +116,7 @@ dependencies {
116116
optional("org.springframework:spring-webflux")
117117
optional("org.springframework:spring-webmvc")
118118
optional("org.springframework.amqp:spring-rabbit")
119+
optional("org.springframework.batch:spring-batch-core")
119120
optional("org.springframework.data:spring-data-cassandra") {
120121
exclude group: "org.slf4j", module: "jcl-over-slf4j"
121122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2022 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.boot.actuate.autoconfigure.observation.batch;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
21+
import org.springframework.batch.core.configuration.annotation.BatchObservabilityBeanPostProcessor;
22+
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.AutoConfiguration;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.context.annotation.Bean;
29+
30+
/**
31+
* {@link EnableAutoConfiguration Auto-configuration} for instrumentation of Spring Batch.
32+
* Jobs
33+
*
34+
* @author Mark Bonnekessel
35+
* @since 3.0.3
36+
*/
37+
@AutoConfiguration(after = ObservationAutoConfiguration.class)
38+
@ConditionalOnBean(ObservationRegistry.class)
39+
@ConditionalOnClass({ ObservationRegistry.class, BatchObservabilityBeanPostProcessor.class })
40+
@SuppressWarnings("removal")
41+
public class BatchObservationAutoConfiguration {
42+
43+
@ConditionalOnMissingBean
44+
@Bean
45+
public BatchObservabilityBeanPostProcessor batchObservabilityBeanPostProcessor() {
46+
return new BatchObservabilityBeanPostProcessor();
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2022 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+
/**
18+
* Auto-configuration for Spring Batcn observations.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.observation.batch;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2023 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.boot.actuate.autoconfigure.observation.batch;
18+
19+
import io.micrometer.observation.tck.TestObservationRegistry;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.batch.core.configuration.annotation.BatchObservabilityBeanPostProcessor;
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
class BatchObservationAutoConfigurationTests {
29+
30+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
31+
.withBean(TestObservationRegistry.class, TestObservationRegistry::create)
32+
.withConfiguration(AutoConfigurations.of(BatchObservationAutoConfiguration.class));
33+
34+
@Test
35+
void backsOffWhenObservationRegistryIsMissing() {
36+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(BatchObservationAutoConfiguration.class))
37+
.run((context) -> assertThat(context).doesNotHaveBean(BatchObservabilityBeanPostProcessor.class));
38+
}
39+
40+
@Test
41+
void beanIsPresentWhenSpringBatchIsPresent() {
42+
this.contextRunner
43+
.run((context) -> assertThat(context).hasSingleBean(BatchObservabilityBeanPostProcessor.class));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)