Skip to content

Commit 8839778

Browse files
committed
fix: Use AOP proxy for DataSource telemetry to preserve bean type
1 parent 8604da3 commit 8839778

File tree

1 file changed

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

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil;
1212
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1313
import javax.sql.DataSource;
14+
import org.aopalliance.intercept.MethodInterceptor;
15+
import org.springframework.aop.framework.ProxyFactory;
1416
import org.springframework.aop.scope.ScopedProxyUtils;
17+
import org.springframework.aop.support.AopUtils;
1518
import org.springframework.beans.factory.ObjectProvider;
1619
import org.springframework.beans.factory.config.BeanPostProcessor;
1720
import org.springframework.core.Ordered;
@@ -50,7 +53,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
5053
&& !isRoutingDatasource(bean)
5154
&& !ScopedProxyUtils.isScopedTarget(beanName)) {
5255
DataSource dataSource = (DataSource) bean;
53-
return JdbcTelemetry.builder(openTelemetryProvider.getObject())
56+
DataSource wrapped = JdbcTelemetry.builder(openTelemetryProvider.getObject())
5457
.setStatementSanitizationEnabled(
5558
InstrumentationConfigUtil.isStatementSanitizationEnabled(
5659
configPropertiesProvider.getObject(),
@@ -66,6 +69,12 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
6669
.getBoolean("otel.instrumentation.jdbc.experimental.transaction.enabled", false))
6770
.build()
6871
.wrap(dataSource);
72+
ProxyFactory proxyFactory = new ProxyFactory(new Class<?>[] {DataSource.class});
73+
proxyFactory.setTarget(bean);
74+
proxyFactory.addAdvice(
75+
(MethodInterceptor) invocation -> AopUtils.invokeJoinpointUsingReflection(
76+
wrapped, invocation.getMethod(), invocation.getArguments()));
77+
return proxyFactory.getProxy();
6978
}
7079
return bean;
7180
}

0 commit comments

Comments
 (0)