Skip to content

Commit 66de8a8

Browse files
committed
Correctly handle getTargetClass method for observation proxies.
Closes #1426
1 parent 364efe5 commit 66de8a8

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/CqlSessionObservationInterceptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
6666
Method method = invocation.getMethod();
6767
Object[] args = invocation.getArguments();
6868

69+
if (method.getName().equals("getTargetClass")) {
70+
return delegate.getClass();
71+
}
72+
6973
if (method.getName().equals("execute") && args.length > 0) {
7074
return observe(createStatement(args), method.getName(), this.delegate::execute);
7175
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/observability/ObservationStatement.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import io.micrometer.observation.Observation;
1919

20+
import java.lang.reflect.Method;
21+
2022
import javax.annotation.Nonnull;
2123

2224
import org.aopalliance.intercept.MethodInterceptor;
@@ -77,7 +79,13 @@ public static boolean isObservationStatement(Statement<?> statement) {
7779
@Override
7880
public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
7981

80-
if (invocation.getMethod().getName().equals("getObservation")) {
82+
Method method = invocation.getMethod();
83+
84+
if (method.getName().equals("getTargetClass")) {
85+
return this.delegate.getClass();
86+
}
87+
88+
if (method.getName().equals("getObservation")) {
8189
return this.observation;
8290
}
8391

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.observability;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import io.micrometer.observation.ObservationRegistry;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.aop.framework.AopProxyUtils;
25+
import org.springframework.aop.support.AopUtils;
26+
27+
import com.datastax.oss.driver.api.core.CqlSession;
28+
29+
/**
30+
* Unit tests for {@link ObservableCqlSessionFactory}.
31+
*
32+
* @author Mark Paluch
33+
*/
34+
class ObservableCqlSessionFactoryUnitTests {
35+
36+
@Test // GH-1426
37+
void sessionFactoryBeanUnwrapsObservationProxy() throws Exception {
38+
39+
CqlSession session = mock(CqlSession.class);
40+
ObservationRegistry registry = ObservationRegistry.NOOP;
41+
42+
CqlSession object = ObservableCqlSessionFactory.wrap(session, registry);
43+
44+
assertThat(AopUtils.getTargetClass(object)).isEqualTo(session.getClass());
45+
assertThat(AopProxyUtils.getSingletonTarget(object)).isEqualTo(session);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)