Skip to content

Commit

Permalink
fix: add runtime hints for trace (#1990)
Browse files Browse the repository at this point in the history
* fix:add hints for trace; include module in the CI; update to graalvm 22.3.2

---------

Co-authored-by: Mridula <[email protected]>
Co-authored-by: mpeddada1 <[email protected]>
  • Loading branch information
3 people authored Aug 3, 2023
1 parent 95bcafb commit 2ba4a75
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/NativeTests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -43,6 +45,7 @@
@ConditionalOnClass({PublisherFactory.class, MessagingTracing.class})
@AutoConfigureAfter({BraveAutoConfiguration.class})
@AutoConfigureBefore(GcpPubSubAutoConfiguration.class)
@ImportRuntimeHints(TraceRuntimeHints.class)
class TracePubSubAutoConfiguration {

@Bean
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.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 @@ -91,6 +91,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 @@ -134,6 +135,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));
}
}
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

0 comments on commit 2ba4a75

Please sign in to comment.