Skip to content

Commit 46b7bd2

Browse files
committed
Add configuration property to enable micrometer annotations
Prior to this commit, the Micrometer annotations support (`@Timed`, `@Counted`...) was guarded by the presence of both Micrometer and AspectJ on the classpath. This signal is too weak, considering the startup performance impact and the fact that the AspectJ dependency can be brought transitively in many cases. This commit adds a new `micrometer.observations.annotations.enabled` property that is set to `false` by default to only process the annotations support when this property is enabled. Fixes gh-39128
1 parent 8bdaae3 commit 46b7bd2

File tree

9 files changed

+56
-9
lines changed

9 files changed

+56
-9
lines changed

buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ private void rsocketPrefixes(Config prefix) {
215215

216216
private void actuatorPrefixes(Config prefix) {
217217
prefix.accept("management");
218+
prefix.accept("micrometer");
218219
}
219220

220221
private void dockerComposePrefixes(Config prefix) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAspectsAutoConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3132
import org.springframework.context.annotation.Bean;
3233

3334
/**
@@ -39,6 +40,7 @@
3940
*/
4041
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
4142
@ConditionalOnClass({ MeterRegistry.class, Advice.class })
43+
@ConditionalOnProperty(prefix = "micrometer.observations.annotations", name = "enabled", havingValue = "true")
4244
@ConditionalOnBean(MeterRegistry.class)
4345
public class MetricsAspectsAutoConfiguration {
4446

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/MicrometerTracingAutoConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3636
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3737
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
38+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3839
import org.springframework.context.annotation.Bean;
3940
import org.springframework.context.annotation.Configuration;
4041
import org.springframework.core.Ordered;
@@ -96,6 +97,7 @@ public PropagatingReceiverTracingObservationHandler<?> propagatingReceiverTracin
9697

9798
@Configuration(proxyBeanMethods = false)
9899
@ConditionalOnClass(Advice.class)
100+
@ConditionalOnProperty(prefix = "micrometer.observations.annotations", name = "enabled", havingValue = "true")
99101
static class SpanAspectConfiguration {
100102

101103
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+6
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,12 @@
22292229
"defaultValue": [
22302230
"W3C"
22312231
]
2232+
},
2233+
{
2234+
"name": "micrometer.observations.annotations.enabled",
2235+
"type": "java.lang.Boolean",
2236+
"description": "Whether auto-configuration of Micrometer annotations is enabled.",
2237+
"defaultValue": false
22322238
}
22332239
],
22342240
"hints": [

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAspectsAutoConfigurationTests.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,19 @@
4141
class MetricsAspectsAutoConfigurationTests {
4242

4343
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().with(MetricsRun.simple())
44+
.withPropertyValues("micrometer.observations.annotations.enabled=true")
4445
.withConfiguration(AutoConfigurations.of(MetricsAspectsAutoConfiguration.class));
4546

47+
@Test
48+
void shouldNotConfigureAspectsByDefault() {
49+
new ApplicationContextRunner().with(MetricsRun.simple())
50+
.withConfiguration(AutoConfigurations.of(MetricsAspectsAutoConfiguration.class))
51+
.run((context) -> {
52+
assertThat(context).doesNotHaveBean(CountedAspect.class);
53+
assertThat(context).doesNotHaveBean(TimedAspect.class);
54+
});
55+
}
56+
4657
@Test
4758
void shouldConfigureAspects() {
4859
this.contextRunner.run((context) -> {
@@ -78,11 +89,12 @@ void shouldNotConfigureAspectsIfAspectjIsMissing() {
7889

7990
@Test
8091
void shouldNotConfigureAspectsIfMeterRegistryBeanIsMissing() {
81-
new ApplicationContextRunner().run((context) -> {
82-
assertThat(context).doesNotHaveBean(MeterRegistry.class);
83-
assertThat(context).doesNotHaveBean(CountedAspect.class);
84-
assertThat(context).doesNotHaveBean(TimedAspect.class);
85-
});
92+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(MetricsAspectsAutoConfiguration.class))
93+
.run((context) -> {
94+
assertThat(context).doesNotHaveBean(MeterRegistry.class);
95+
assertThat(context).doesNotHaveBean(CountedAspect.class);
96+
assertThat(context).doesNotHaveBean(TimedAspect.class);
97+
});
8698
}
8799

88100
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/MicrometerTracingAutoConfigurationTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@
4747
*
4848
* @author Moritz Halbritter
4949
* @author Jonatan Ivanov
50+
* @author Brian Clozel
5051
*/
5152
class MicrometerTracingAutoConfigurationTests {
5253

5354
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
55+
.withPropertyValues("micrometer.observations.annotations.enabled=true")
5456
.withConfiguration(AutoConfigurations.of(MicrometerTracingAutoConfiguration.class));
5557

5658
@Test
5759
void shouldSupplyBeans() {
5860
this.contextRunner.withUserConfiguration(TracerConfiguration.class, PropagatorConfiguration.class)
61+
.withPropertyValues("micrometer.observations.annotations.enabled=true")
5962
.run((context) -> {
6063
assertThat(context).hasSingleBean(DefaultTracingObservationHandler.class);
6164
assertThat(context).hasSingleBean(PropagatingReceiverTracingObservationHandler.class);
@@ -127,6 +130,17 @@ void shouldNotSupplyBeansIfTracerIsMissing() {
127130
});
128131
}
129132

133+
@Test
134+
void shouldNotSupplyAspectBeansIfPropertyIsDisabled() {
135+
this.contextRunner.withUserConfiguration(TracerConfiguration.class, PropagatorConfiguration.class)
136+
.withPropertyValues("micrometer.observations.annotations.enabled=false")
137+
.run((context) -> {
138+
assertThat(context).doesNotHaveBean(DefaultNewSpanParser.class);
139+
assertThat(context).doesNotHaveBean(ImperativeMethodInvocationProcessor.class);
140+
assertThat(context).doesNotHaveBean(SpanAspect.class);
141+
});
142+
}
143+
130144
@Test
131145
void shouldNotSupplyBeansIfAspectjIsMissing() {
132146
this.contextRunner.withUserConfiguration(TracerConfiguration.class)

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ Metrics for Jetty's `Connector` instances are bound by using Micrometer's `Jetty
10671067

10681068
[[actuator.metrics.supported.timed-annotation]]
10691069
==== @Timed Annotation Support
1070-
To use `@Timed` where it is not directly supported by Spring Boot, refer to the {micrometer-concepts-docs}#_the_timed_annotation[Micrometer documentation].
1070+
To enable scanning of `@Timed` annotations, you will need to set the configprop:micrometer.observations.annotations.enabled[] property to `true`.
1071+
Please refer to the {micrometer-concepts-docs}#_the_timed_annotation[Micrometer documentation].
10711072

10721073

10731074

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/observability.adoc

+8
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ NOTE: Spring Boot does not provide auto-configuration for OpenTelemetry metrics
8585
OpenTelemetry tracing is only auto-configured when used together with <<actuator#actuator.micrometer-tracing, Micrometer Tracing>>.
8686

8787
The next sections will provide more details about logging, metrics and traces.
88+
89+
90+
91+
[[actuator.observability.annotations]]
92+
=== Micrometer Observation Annotations support
93+
94+
To enable scanning of metrics and tracing annotations like `@Timed`, `@Counted`, `@MeterTag` and `@NewSpan` annotations, you will need to set the configprop:micrometer.observations.annotations.enabled[] property to `true`.
95+
This feature is supported Micrometer directly, please refer to the {micrometer-concepts-docs}#_the_timed_annotation[Micrometer] and {micrometer-tracing-docs}/api.html#_aspect_oriented_programming[Micrometer Tracing] reference docs.

spring-boot-project/spring-boot-docs/src/docs/asciidoc/attributes.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
:micrometer-docs: https://micrometer.io/docs
110110
:micrometer-concepts-docs: {micrometer-docs}/concepts
111111
:micrometer-registry-docs: {micrometer-docs}/registry
112+
:micrometer-tracing-docs: https://docs.micrometer.io/tracing/reference
112113
:tomcat-docs: https://tomcat.apache.org/tomcat-{tomcat-version}-doc
113114
:graal-version: 22.3
114115
:graal-native-image-docs: https://www.graalvm.org/{graal-version}/reference-manual/native-image

0 commit comments

Comments
 (0)