Skip to content

Commit d75da45

Browse files
committed
Add runtime hints for Cassandra observability.
See #1321 Original pull request: #1322
1 parent 786cf3c commit d75da45

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/aot/CassandraRuntimeHints.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.cassandra.aot;
1717

1818
import java.util.Arrays;
19+
import java.util.List;
1920

2021
import org.springframework.aot.hint.MemberCategory;
2122
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -28,6 +29,15 @@
2829
import org.springframework.data.cassandra.repository.support.SimpleReactiveCassandraRepository;
2930
import org.springframework.data.repository.util.ReactiveWrappers;
3031
import org.springframework.lang.Nullable;
32+
import org.springframework.util.ClassUtils;
33+
34+
import com.datastax.oss.driver.api.core.CqlSession;
35+
import com.datastax.oss.driver.api.core.cql.BatchStatement;
36+
import com.datastax.oss.driver.api.core.cql.BoundStatement;
37+
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
38+
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
39+
import com.datastax.oss.driver.api.core.cql.Statement;
40+
import com.datastax.oss.driver.api.core.session.Request;
3141

3242
/**
3343
* {@link RuntimeHintsRegistrar} for repository types and entity callbacks.
@@ -40,6 +50,9 @@ class CassandraRuntimeHints implements RuntimeHintsRegistrar {
4050
private static final boolean PROJECT_REACTOR_PRESENT = ReactiveWrappers
4151
.isAvailable(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR);
4252

53+
private static final boolean OBSERVABILITY_PRESENT = ClassUtils
54+
.isPresent("io.micrometer.observation.ObservationRegistry", CassandraRuntimeHints.class.getClassLoader());
55+
4356
@Override
4457
public void registerHints(org.springframework.aot.hint.RuntimeHints hints, @Nullable ClassLoader classLoader) {
4558

@@ -57,5 +70,27 @@ public void registerHints(org.springframework.aot.hint.RuntimeHints hints, @Null
5770
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
5871
MemberCategory.INVOKE_PUBLIC_METHODS));
5972
}
73+
74+
if (OBSERVABILITY_PRESENT) {
75+
76+
List<Class<?>> statementInterfaces = Arrays.asList(BatchStatement.class, PreparedStatement.class,
77+
BoundStatement.class, SimpleStatement.class, Statement.class);
78+
79+
hints.reflection().registerTypes(statementInterfaces.stream().map(TypeReference::of).toList(), builder -> builder
80+
.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS));
81+
82+
TypeReference obsSupplier = TypeReference
83+
.of("org.springframework.data.cassandra.observability.CassandraObservationSupplier");
84+
hints.reflection().registerTypes(List.of(obsSupplier), builder -> builder
85+
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS));
86+
87+
for (Class<?> statementInterface : statementInterfaces) {
88+
hints.proxies().registerJdkProxy(TypeReference.of(statementInterface), TypeReference.of(Request.class),
89+
obsSupplier);
90+
}
91+
92+
hints.proxies().registerJdkProxy(CqlSession.class);
93+
}
94+
6095
}
6196
}

0 commit comments

Comments
 (0)