Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add runtime hints for trace #1990

Merged
merged 14 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.lang.reflect.Method;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.util.ReflectionUtils;

public class TraceRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
Method method =
ReflectionUtils.findMethod(
ObservedAspect.class, "observeMethod", ProceedingJoinPoint.class);
hints
.reflection()
.registerMethod(method, ExecutableMode.INVOKE)
.registerTypes(
Arrays.asList(
TypeReference.of(com.google.protobuf.Value.class),
TypeReference.of(com.google.protobuf.Value.Builder.class)),
Copy link
Contributor

@mpeddada1 mpeddada1 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two configurations may be removed after the static jsons are generated through googleapis/sdk-platform-java#1841. For now, we get the following error without these configurations:

Failures (1):
  JUnit Jupiter:TraceSampleApplicationIntegrationTests:testTracesAreLoggedCorrectly()
    MethodSource [className = 'com.example.TraceSampleApplicationIntegrationTests', methodName = 'testTracesAreLoggedCorrectly', methodParameterTypes = '']
    => java.lang.IllegalStateException: Generated message class "com.google.protobuf.Value$Builder" missing method "hasNullValue".
       com.google.protobuf.GeneratedMessageV3.getMethodOrDie(GeneratedMessageV3.java:1998)
       com.google.protobuf.GeneratedMessageV3.access$1000(GeneratedMessageV3.java:79)
       com.google.protobuf.GeneratedMessageV3$FieldAccessorTable$SingularFieldAccessor$ReflectionInvoker.<init>(GeneratedMessageV3.java:2353)
       com.google.protobuf.GeneratedMessageV3$FieldAccessorTable$SingularFieldAccessor.<init>(GeneratedMessageV3.java:2418)
       com.google.protobuf.GeneratedMessageV3$FieldAccessorTable$SingularEnumFieldAccessor.<init>(GeneratedMessageV3.java:2893)
       [...]
     Caused by: java.lang.NoSuchMethodException: com.google.protobuf.Value$Builder.hasNullValue()
       [email protected]/java.lang.Class.checkMethod(DynamicHub.java:1038)
       [email protected]/java.lang.Class.getMethod(DynamicHub.java:1023)
       com.google.protobuf.GeneratedMessageV3.getMethodOrDie(GeneratedMessageV3.java:1995)
       [...]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see com.google.protobuf.Value as an entry in the Trace library's reflect-config.json. It does show up for some, though: https://github.com/search?q=repo%3Agoogleapis%2Fgoogle-cloud-java+com.google.protobuf.Value+path%3A**%2Freflect-config.json&type=code&p=1

  • Do we need to adjust the generation logic to include more than messages defined in the protos, or is a non-trace library triggering the reflection lookup?

Copy link
Contributor

@mpeddada1 mpeddada1 Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Analyzing the stacktrace of the test shows that the use of com.google.protobuf.Value is coming from

This line of code eventually leads to GeneratedMessageV3#getMethodOrDie being called:
Screenshot 2023-07-31 at 6 07 51 PM

I've moved the configuration to the test/ directory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Update] Tests are passing without these configurations after rebasing with origin/main.

hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
com.google.cloud.spring.autoconfigure.trace.aot.TraceRuntimeHints
Original file line number Diff line number Diff line change
@@ -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"));
}
}
2 changes: 2 additions & 0 deletions spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<id>native-sample-config</id>
<modules>
<module>spring-cloud-gcp-logging-sample</module>
<module>spring-cloud-gcp-trace-sample</module>
<module>spring-cloud-gcp-vision-api-sample</module>
</modules>
<build>
Expand Down Expand Up @@ -133,6 +134,7 @@
</includes>
<systemPropertyVariables>
<it.logging>true</it.logging>
<it.trace>true</it.trace>
<it.vision>true</it.vision>
</systemPropertyVariables>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Comment on lines +26 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this configuration, the sample test results in:

com.example.TraceSampleApplicationIntegrationTests > testTracesAreLoggedCorrectly() FAILED


Failures (1):
  JUnit Jupiter:TraceSampleApplicationIntegrationTests:testTracesAreLoggedCorrectly()
    MethodSource [className = 'com.example.TraceSampleApplicationIntegrationTests', methodName = 'testTracesAreLoggedCorrectly', methodParameterTypes = '']
    => com.oracle.svm.core.jdk.UnsupportedFeatureError: ThreadMXBean methods
       org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:89)
       org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.management.SubstrateThreadMXBean.findDeadlockedThreads(SubstrateThreadMXBean.java:258)
       org.awaitility.core.ConditionAwaiter.await(ConditionAwaiter.java:157)
       org.awaitility.core.AssertionCondition.await(AssertionCondition.java:119)
       org.awaitility.core.AssertionCondition.await(AssertionCondition.java:31)
       org.awaitility.core.ConditionFactory.until(ConditionFactory.java:985)
       org.awaitility.core.ConditionFactory.untilAsserted(ConditionFactory.java:769)
       com.example.TraceSampleApplicationIntegrationTests.testTracesAreLoggedCorrectly(TraceSampleApplicationIntegrationTests.java:191)
       [email protected]/java.lang.reflect.Method.invoke(Method.java:568)
       org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
       [...]

Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -75,6 +76,7 @@
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {Application.class})
@AutoConfigureObservability
@ImportRuntimeHints(TestRuntimeHints.class)
class TraceSampleApplicationIntegrationTests {

@DynamicPropertySource
Expand Down
Loading