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 logging #1933

Merged
merged 17 commits into from
Jul 6, 2023
Merged

fix: add runtime hints for logging #1933

merged 17 commits into from
Jul 6, 2023

Conversation

zhumin8
Copy link
Contributor

@zhumin8 zhumin8 commented Jun 7, 2023

Need rebase after setups in #1931 are merged in.

This pr adds:

  • runtime hints for springframework aot to allow Logging module work with Native build.
  • a relevant unit test
  • runtime hints needed for sample integration test. (this is test setup specific and is not needed to run sample application).

CI for native is setup to run on sample integration tests that:

Modification to spring-cloud-gcp-samples/pom.xml so that:

  • by default, all modules are included.
  • To run native test only for selected modules, use --define notAllModules=true in mvn cmd.

This is needed because native test would fail on no test configuration found.

To verify locally:

  • Run the sample manually:
    • Follow guide in README, instead of mvn spring-boot:run, package with mvn clean package -Pnative-sample-config -Pnative
    • Run executable ./target/spring-cloud-gcp-logging-sample
    • Follow guide to issue a request and observe log lines from cloud console.
    • Log entry written by sample:
{
insertId: "1c08xcxf1v07m9"
jsonPayload: {
message: "This line was also written to the log with the same Trace ID."
}
labels: {3}
logName: "projects/[project-id]/logs/spring.log"
receiveTimestamp: "2023-06-06T14:15:08.236653291Z"
resource: {2}
severity: "INFO"
timestamp: "2023-06-06T14:15:08.122Z"
trace: "projects/[project-id]/traces/trace-id-to-show-1234"
}
  • Run sample test:
    • mvn -PnativeTest clean test -Pnative-sample-config
    • native test are run at the end of the build.
com.example.LoggingSampleApplicationIntegrationTests > testLogRecordedInStackDriver() SUCCESSFUL


Test run finished after 12975 ms
[         3 containers found      ]
[         0 containers skipped    ]
[         3 containers started    ]
[         0 containers aborted    ]
[         3 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         0 tests failed          ]

@zhumin8
Copy link
Contributor Author

zhumin8 commented Jun 28, 2023

force-pushed to rebase on main.

Comment on lines 41 to 45
- name: Install pubsub-emulator
if: ${{ matrix.it == 'pubsub-emulator' }}
run: |
gcloud components install pubsub-emulator beta && \
gcloud components update
Copy link
Member

Choose a reason for hiding this comment

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

I think matrix.it is always null. Can this step be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do.

Comment on lines +28 to +43
public class LoggingRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(LoggingAppender.class),
TypeReference.of(TraceIdLoggingEnhancer.class),
TypeReference.of(StackdriverJsonLayout.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, do we notice any advantages with using the RuntimeHintsRegistrar class over using static reflection configurations?
Mostly in terms of choosing the static jsons vs a dynamic way of registering classes for reflection, I'm wondering if we will run into any breaking changes when we upgrade our Spring stack that introduces some changes to it's class definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question! I went this way because it's documented and to be consistent to spring conventions, I notice spring-boot and spring-data projects are using this RuntimeHintsRegistar (e.g. logging autoconfig)
Some other benefit include: this allows hints be tested; It's easy use spring concepts when registering hints (for example in vision, this is for the test resources, but you can easily refer to spring resources when registering.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context! Considering this resolved to unblock this PR. Discussion has been moved offline.

@sonarcloud
Copy link

sonarcloud bot commented Jul 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Comment on lines +28 to +43
public class LoggingRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(LoggingAppender.class),
TypeReference.of(TraceIdLoggingEnhancer.class),
TypeReference.of(StackdriverJsonLayout.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context! Considering this resolved to unblock this PR. Discussion has been moved offline.

@zhumin8 zhumin8 merged commit 21903af into main Jul 6, 2023
@zhumin8 zhumin8 deleted the logging-hints branch July 6, 2023 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants