Skip to content

Commit f995adf

Browse files
committed
Resolve ParameterNameDiscoverer Bean Conflict in spring-boot-autoconfigure
This update addresses a conflict arising in the `spring-boot-autoconfigure` project when using `opentelemetry-spring-boot-starter` and other projects which use the `ParameterNameDiscoverer` bean simultaneously, e.g. `springdoc-openapi-starter-webmvc-ui`. The issue occurs due to the presence of multiple candidates for the `ParameterNameDiscoverer` object, leading to an injection conflict. When both starters are used together, the application fails to start, displaying an error related to the ParameterNameDiscoverer object. This is due to the ambiguity in selecting the correct bean to inject, as multiple candidates are present. Example of the error output: ``` *************************** APPLICATION FAILED TO START *************************** Description: Parameter 1 of method otelInstrumentationWithSpanAspect in io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.annotations.InstrumentationAnnotationsAutoConfiguration required a single bean, but 2 were found: - parameterNameDiscoverer: defined by method 'parameterNameDiscoverer' in class path resource [io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/annotations/InstrumentationAnnotationsAutoConfiguration.class] - localSpringDocParameterNameDiscoverer: defined by method 'localSpringDocParameterNameDiscoverer' in class path resource [org/springdoc/core/configuration/SpringDocConfiguration.class] Action: Consider marking one of the beans as @primary, updating the consumer to accept multiple beans, or using @qualifier to identify the bean that should be consumed ``` To resolve this issue, this commit defines the private static `ParameterNameDiscoverer` instance and use that directly instead of injection in `InstrumentationAnnotationsAutoConfiguration`. Signed-off-by: moznion <[email protected]>
1 parent da51db9 commit f995adf

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/annotations/InstrumentationAnnotationsAutoConfiguration.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.aspectj.lang.annotation.Aspect;
1111
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
1212
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
13-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1413
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1514
import org.springframework.context.annotation.Bean;
1615
import org.springframework.context.annotation.Configuration;
@@ -23,25 +22,19 @@
2322
@ConditionalOnProperty(name = "otel.instrumentation.annotations.enabled", matchIfMissing = true)
2423
@Configuration
2524
public class InstrumentationAnnotationsAutoConfiguration {
26-
27-
@Bean
28-
@ConditionalOnMissingBean
29-
ParameterNameDiscoverer parameterNameDiscoverer() {
30-
return new DefaultParameterNameDiscoverer();
31-
}
25+
private final ParameterNameDiscoverer parameterNameDiscoverer =
26+
new DefaultParameterNameDiscoverer();
3227

3328
@Bean
3429
@ConditionalOnClass(WithSpan.class)
35-
InstrumentationWithSpanAspect otelInstrumentationWithSpanAspect(
36-
OpenTelemetry openTelemetry, ParameterNameDiscoverer parameterNameDiscoverer) {
30+
InstrumentationWithSpanAspect otelInstrumentationWithSpanAspect(OpenTelemetry openTelemetry) {
3731
return new InstrumentationWithSpanAspect(openTelemetry, parameterNameDiscoverer);
3832
}
3933

4034
@Bean
4135
@SuppressWarnings("deprecation") // instrumenting deprecated class for backwards compatibility
4236
@ConditionalOnClass(io.opentelemetry.extension.annotations.WithSpan.class)
43-
SdkExtensionWithSpanAspect otelSdkExtensionWithSpanAspect(
44-
OpenTelemetry openTelemetry, ParameterNameDiscoverer parameterNameDiscoverer) {
37+
SdkExtensionWithSpanAspect otelSdkExtensionWithSpanAspect(OpenTelemetry openTelemetry) {
4538
return new SdkExtensionWithSpanAspect(openTelemetry, parameterNameDiscoverer);
4639
}
4740
}

0 commit comments

Comments
 (0)