1- /*
2- * Copyright The OpenTelemetry Authors
3- * SPDX-License-Identifier: Apache-2.0
4- */
5-
61package io .opentelemetry .instrumentation .spring .autoconfigure .internal .instrumentation .jdbc ;
72
83import com .google .errorprone .annotations .CanIgnoreReturnValue ;
94import io .opentelemetry .api .OpenTelemetry ;
105import io .opentelemetry .instrumentation .jdbc .datasource .JdbcTelemetry ;
116import io .opentelemetry .instrumentation .spring .autoconfigure .internal .properties .InstrumentationConfigUtil ;
127import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
8+
9+ import javax .annotation .Nonnull ;
10+ import javax .annotation .Nullable ;
1311import javax .sql .DataSource ;
12+
13+ import org .aopalliance .intercept .MethodInterceptor ;
14+ import org .aopalliance .intercept .MethodInvocation ;
15+ import org .springframework .aop .framework .ProxyFactory ;
1416import org .springframework .aop .scope .ScopedProxyUtils ;
17+ import org .springframework .aop .support .AopUtils ;
1518import org .springframework .beans .factory .ObjectProvider ;
1619import org .springframework .beans .factory .config .BeanPostProcessor ;
1720import 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,20 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
6669 .getBoolean ("otel.instrumentation.jdbc.experimental.transaction.enabled" , false ))
6770 .build ()
6871 .wrap (dataSource );
72+
73+ ProxyFactory proxyFactory = new ProxyFactory (DataSource .class );
74+ proxyFactory .setTarget (bean );
75+ proxyFactory .addAdvice (
76+ new MethodInterceptor () {
77+ @ Nullable
78+ @ Override
79+ public Object invoke (@ Nonnull MethodInvocation invocation ) throws Throwable {
80+ return AopUtils .invokeJoinpointUsingReflection (
81+ wrapped , invocation .getMethod (), invocation .getArguments ());
82+ }
83+ });
84+
85+ return proxyFactory .getProxy ();
6986 }
7087 return bean ;
7188 }
0 commit comments