diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionObservationInterceptor.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionObservationInterceptor.java index cc2d892a2..978937ea6 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionObservationInterceptor.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionObservationInterceptor.java @@ -66,6 +66,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); Object[] args = invocation.getArguments(); + if (method.getName().equals("getTargetClass")) { + return delegate.getClass(); + } + if (method.getName().equals("execute") && args.length > 0) { return observe(createStatement(args), method.getName(), this.delegate::execute); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationStatement.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationStatement.java index 3c0ae89a5..a68d5aad0 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationStatement.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationStatement.java @@ -17,6 +17,8 @@ import io.micrometer.observation.Observation; +import java.lang.reflect.Method; + import javax.annotation.Nonnull; import org.aopalliance.intercept.MethodInterceptor; @@ -77,7 +79,13 @@ public static boolean isObservationStatement(Statement statement) { @Override public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable { - if (invocation.getMethod().getName().equals("getObservation")) { + Method method = invocation.getMethod(); + + if (method.getName().equals("getTargetClass")) { + return this.delegate.getClass(); + } + + if (method.getName().equals("getObservation")) { return this.observation; } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/ObservableCqlSessionFactoryUnitTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/ObservableCqlSessionFactoryUnitTests.java new file mode 100644 index 000000000..a63ddbe41 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/observability/ObservableCqlSessionFactoryUnitTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.observability; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +import io.micrometer.observation.ObservationRegistry; + +import org.junit.jupiter.api.Test; +import org.springframework.aop.framework.AopProxyUtils; +import org.springframework.aop.support.AopUtils; + +import com.datastax.oss.driver.api.core.CqlSession; + +/** + * Unit tests for {@link ObservableCqlSessionFactory}. + * + * @author Mark Paluch + */ +class ObservableCqlSessionFactoryUnitTests { + + @Test // GH-1426 + void sessionFactoryBeanUnwrapsObservationProxy() throws Exception { + + CqlSession session = mock(CqlSession.class); + ObservationRegistry registry = ObservationRegistry.NOOP; + + CqlSession object = ObservableCqlSessionFactory.wrap(session, registry); + + assertThat(AopUtils.getTargetClass(object)).isEqualTo(session.getClass()); + assertThat(AopProxyUtils.getSingletonTarget(object)).isEqualTo(session); + } + +}