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

Define helper classes in loadClass #5528

Merged
merged 4 commits into from
Mar 9, 2022

Conversation

laurit
Copy link
Contributor

@laurit laurit commented Mar 8, 2022

Resolves #5499
Resolves #5493
Resolves #5466
Instead of injecting helper classes during transformation instrument loadClass methods of class loaders to load them. This should avoid defining helper class triggering loading additional classes that won't get transformed.

@laurit laurit requested a review from a team March 8, 2022 19:39

// Now attempt to load our injected instrumentation class from the same class loader as
// DispatcherServlet
Class<?> clazz =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have a test with wildfly where loading our injected class from getBeanClassLoader() fails. Previously it worked because it hit findLoadedClass during class loading.
If we do it this way we can probably put OpenTelemetryHandlerMappingFilter back into our own package. We needed to put in spring package because we set up the bean definition in the wrong way. We set both name and class that are actually kept in the same field in spring bean definition so it always tried to use loadClass which previously worked only if the class was in spring package.

if (resolution.isResolved()) {
return new ClasspathType(resolution.resolve());
if (!helperClassPredicate.isHelperClass(className)) {
Resolution resolution = classpathPool.describe(className);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking up an injected helper records it in AgentCachingPoolStrategy as not found. If that helper is defined before it gets evicted there will be a stack trace about resource not found.

Copy link
Member

Choose a reason for hiding this comment

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

this is helpful explanation, can you turn it into code comment? (especially since breaking it won't fail tests, only log annoying debug messages)

@laurit laurit changed the title Define helper classes in loadClass similarly to regular classes Define helper classes in loadClass Mar 8, 2022
Copy link
Member

@trask trask left a comment

Choose a reason for hiding this comment

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

Nice! I think this is an exciting improvement 🎉

Comment on lines +59 to +62
Class<?> helperClass = InjectedClassHelper.loadHelperClass(classLoader, name);
if (helperClass != null) {
return helperClass;
}
Copy link
Member

Choose a reason for hiding this comment

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

oh, this is very nice!

Comment on lines -212 to +215
.setSuperClassName(TestHelperClasses.HelperSuperClass.name)
.setSuperClassName(TestAbstractSuperClass.name)
Copy link
Member

Choose a reason for hiding this comment

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

I didn't follow the reason for these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was needed because of the change in HelperReferenceWrapper that added !helperClassPredicate.isHelperClass(className) check. Now classes that are in io.opentelemetry.instrumentation are matched by that check which made a couple of tests fail. I believe the intent of the test was to test injected helper extending a class from application. To ensure that the class that the test extends is treated as an application class and not as a helper I had to copy it into a different package. I also tried moving TestHelperClasses but that made a bunch of other tests fail.

if (resolution.isResolved()) {
return new ClasspathType(resolution.resolve());
if (!helperClassPredicate.isHelperClass(className)) {
Resolution resolution = classpathPool.describe(className);
Copy link
Member

Choose a reason for hiding this comment

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

this is helpful explanation, can you turn it into code comment? (especially since breaking it won't fail tests, only log annoying debug messages)

@trask trask merged commit c461d22 into open-telemetry:main Mar 9, 2022
@trask
Copy link
Member

trask commented Mar 9, 2022

🥳

RashmiRam pushed a commit to RashmiRam/opentelemetry-auto-instr-java that referenced this pull request May 23, 2022
* Define helper classes in loadClass similarly to regular classes

* fix test

* spotless

* address review comments
@laurit laurit deleted the load-helper-class branch July 6, 2023 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants