Skip to content

Commit

Permalink
Correctly handle getTargetClass method for observation proxies.
Browse files Browse the repository at this point in the history
Closes #1426
  • Loading branch information
mp911de committed Aug 23, 2023
1 parent 4442226 commit c476bbd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import io.micrometer.observation.Observation;

import java.lang.reflect.Method;

import javax.annotation.Nonnull;

import org.aopalliance.intercept.MethodInterceptor;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit c476bbd

Please sign in to comment.