Correctly handle getTargetClass method for observation proxies.

Closes #1426
This commit is contained in:
Mark Paluch
2023-08-23 16:23:16 +02:00
parent 364efe5bcb
commit 66de8a8c91
3 changed files with 61 additions and 1 deletions

View File

@@ -66,6 +66,10 @@ final class CqlSessionObservationInterceptor implements MethodInterceptor {
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);
}

View File

@@ -17,6 +17,8 @@ package org.springframework.data.cassandra.observability;
import io.micrometer.observation.Observation;
import java.lang.reflect.Method;
import javax.annotation.Nonnull;
import org.aopalliance.intercept.MethodInterceptor;
@@ -77,7 +79,13 @@ final class ObservationStatement implements MethodInterceptor {
@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;
}

View File

@@ -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);
}
}