From 2ba4a75bc58fbc48a3f92f6bd6d79a1fd46a905b Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Thu, 3 Aug 2023 11:56:10 -0400 Subject: [PATCH] fix: add runtime hints for trace (#1990) * fix:add hints for trace; include module in the CI; update to graalvm 22.3.2 --------- Co-authored-by: Mridula <66699525+mpeddada1@users.noreply.github.com> Co-authored-by: mpeddada1 --- .github/workflows/NativeTests.yaml | 2 +- .../trace/aot/TraceRuntimeHints.java | 39 ++++++++++++++++ .../pubsub/TracePubSubAutoConfiguration.java | 3 ++ .../trace/aot/TraceRuntimeHintsTest.java | 34 ++++++++++++++ spring-cloud-gcp-samples/pom.xml | 2 + .../main/java/com/example/Application.java | 2 + .../java/com/example/SampleRuntimeHints.java | 34 ++++++++++++++ .../java/com/example/TestRuntimeHints.java | 44 +++++++++++++++++++ ...raceSampleApplicationIntegrationTests.java | 2 + 9 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHints.java create mode 100644 spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHintsTest.java create mode 100644 spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/SampleRuntimeHints.java create mode 100644 spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TestRuntimeHints.java diff --git a/.github/workflows/NativeTests.yaml b/.github/workflows/NativeTests.yaml index 683e4856f8..cf669e10fb 100644 --- a/.github/workflows/NativeTests.yaml +++ b/.github/workflows/NativeTests.yaml @@ -71,7 +71,7 @@ jobs: same-branch-only: false - uses: graalvm/setup-graalvm@v1 with: - version: '22.3.0' + version: '22.3.2' java-version: '17' components: 'native-image' github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHints.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHints.java new file mode 100644 index 0000000000..ed7575fe53 --- /dev/null +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHints.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023 Google LLC + * + * 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 com.google.cloud.spring.autoconfigure.trace.aot; + +import io.micrometer.observation.aop.ObservedAspect; +import java.util.Arrays; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.TypeReference; + +public class TraceRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints + .reflection() + .registerTypes( + Arrays.asList(TypeReference.of(ObservedAspect.class)), + hint -> + hint.withMembers( + MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, + MemberCategory.INVOKE_PUBLIC_METHODS)); + } +} diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/pubsub/TracePubSubAutoConfiguration.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/pubsub/TracePubSubAutoConfiguration.java index a5b16aa97f..667b59dd69 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/pubsub/TracePubSubAutoConfiguration.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/trace/pubsub/TracePubSubAutoConfiguration.java @@ -20,6 +20,7 @@ import brave.messaging.MessagingTracing; import com.google.cloud.pubsub.v1.Publisher; import com.google.cloud.spring.autoconfigure.pubsub.GcpPubSubAutoConfiguration; +import com.google.cloud.spring.autoconfigure.trace.aot.TraceRuntimeHints; import com.google.cloud.spring.pubsub.core.publisher.PublisherCustomizer; import com.google.cloud.spring.pubsub.support.PublisherFactory; import io.micrometer.observation.ObservationRegistry; @@ -34,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -43,6 +45,7 @@ @ConditionalOnClass({PublisherFactory.class, MessagingTracing.class}) @AutoConfigureAfter({BraveAutoConfiguration.class}) @AutoConfigureBefore(GcpPubSubAutoConfiguration.class) +@ImportRuntimeHints(TraceRuntimeHints.class) class TracePubSubAutoConfiguration { @Bean diff --git a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHintsTest.java b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHintsTest.java new file mode 100644 index 0000000000..77c5baed05 --- /dev/null +++ b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/trace/aot/TraceRuntimeHintsTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2023 Google LLC + * + * 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 com.google.cloud.spring.autoconfigure.trace.aot; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection; + +import io.micrometer.observation.aop.ObservedAspect; +import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; + +class TraceRuntimeHintsTest { + @Test + void registerObserveMethod() { + RuntimeHints hints = new RuntimeHints(); + TraceRuntimeHints registrar = new TraceRuntimeHints(); + registrar.registerHints(hints, null); + assertThat(hints).matches(reflection().onMethod(ObservedAspect.class, "observeMethod")); + } +} diff --git a/spring-cloud-gcp-samples/pom.xml b/spring-cloud-gcp-samples/pom.xml index 05a1c57098..328d870420 100644 --- a/spring-cloud-gcp-samples/pom.xml +++ b/spring-cloud-gcp-samples/pom.xml @@ -91,6 +91,7 @@ native-sample-config spring-cloud-gcp-logging-sample + spring-cloud-gcp-trace-sample spring-cloud-gcp-vision-api-sample @@ -134,6 +135,7 @@ true + true true diff --git a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/Application.java b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/Application.java index ad2f5be68b..bc2f75fb9e 100644 --- a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/Application.java +++ b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/Application.java @@ -34,6 +34,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.lang.NonNull; @@ -48,6 +49,7 @@ /** Sample spring boot application. */ @AutoConfiguration @SpringBootApplication +@ImportRuntimeHints(SampleRuntimeHints.class) public class Application implements WebMvcConfigurer { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); diff --git a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/SampleRuntimeHints.java b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/SampleRuntimeHints.java new file mode 100644 index 0000000000..db46c7a87b --- /dev/null +++ b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/java/com/example/SampleRuntimeHints.java @@ -0,0 +1,34 @@ +/* + * Copyright 2023 Google LLC + * + * 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 com.example; + +import java.util.Arrays; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.TypeReference; + +public class SampleRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.reflection().registerTypes( + Arrays.asList(TypeReference.of(Application.class)), + hint -> hint.withMembers( + MemberCategory.INVOKE_PUBLIC_METHODS)); + } +} diff --git a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TestRuntimeHints.java b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TestRuntimeHints.java new file mode 100644 index 0000000000..dac8c7f9fe --- /dev/null +++ b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TestRuntimeHints.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 Google LLC + * + * 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 com.example; + +import ch.qos.logback.classic.encoder.PatternLayoutEncoder; +import ch.qos.logback.core.ConsoleAppender; +import java.util.Arrays; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.TypeReference; + +public class TestRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints + .reflection() + .registerTypes( + Arrays.asList( + TypeReference.of(ConsoleAppender.class), + TypeReference.of(PatternLayoutEncoder.class)), + hint -> + hint.withMembers( + MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, + MemberCategory.INVOKE_PUBLIC_METHODS)); + hints.resources().registerPattern("logback-test.xml"); + hints.resources().registerPattern("com/google/cloud/spring/logging/logback-appender.xml"); + } +} diff --git a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TraceSampleApplicationIntegrationTests.java b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TraceSampleApplicationIntegrationTests.java index e4a855e06e..7bfcb8b477 100644 --- a/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TraceSampleApplicationIntegrationTests.java +++ b/spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/test/java/com/example/TraceSampleApplicationIntegrationTests.java @@ -60,6 +60,7 @@ import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -75,6 +76,7 @@ webEnvironment = WebEnvironment.RANDOM_PORT, classes = {Application.class}) @AutoConfigureObservability +@ImportRuntimeHints(TestRuntimeHints.class) class TraceSampleApplicationIntegrationTests { @DynamicPropertySource