Skip to content

Commit aaed8ac

Browse files
authored
Spring starter: exclude spring routing data source from instrumentation (#13054)
1 parent 93bca06 commit aaed8ac

File tree

1 file changed

+17
-1
lines changed
  • instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/jdbc

1 file changed

+17
-1
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/jdbc/DataSourcePostProcessor.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
final class DataSourcePostProcessor implements BeanPostProcessor, Ordered {
2020

21+
private static final Class<?> ROUTING_DATA_SOURCE_CLASS = getRoutingDataSourceClass();
22+
2123
private final ObjectProvider<OpenTelemetry> openTelemetryProvider;
2224
private final ObjectProvider<ConfigProperties> configPropertiesProvider;
2325

@@ -28,11 +30,25 @@ final class DataSourcePostProcessor implements BeanPostProcessor, Ordered {
2830
this.configPropertiesProvider = configPropertiesProvider;
2931
}
3032

33+
private static Class<?> getRoutingDataSourceClass() {
34+
try {
35+
return Class.forName("org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource");
36+
} catch (ClassNotFoundException exception) {
37+
return null;
38+
}
39+
}
40+
41+
private static boolean isRoutingDatasource(Object bean) {
42+
return ROUTING_DATA_SOURCE_CLASS != null && ROUTING_DATA_SOURCE_CLASS.isInstance(bean);
43+
}
44+
3145
@CanIgnoreReturnValue
3246
@Override
3347
public Object postProcessAfterInitialization(Object bean, String beanName) {
3448
// Exclude scoped proxy beans to avoid double wrapping
35-
if (bean instanceof DataSource && !ScopedProxyUtils.isScopedTarget(beanName)) {
49+
if (bean instanceof DataSource
50+
&& !isRoutingDatasource(bean)
51+
&& !ScopedProxyUtils.isScopedTarget(beanName)) {
3652
DataSource dataSource = (DataSource) bean;
3753
return JdbcTelemetry.builder(openTelemetryProvider.getObject())
3854
.setStatementSanitizationEnabled(

0 commit comments

Comments
 (0)