-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Compile-time and load-time weaving support for aspects #5059
Conversation
What would be the best way to merge this with my changes from #5060 and https://github.com/mihalyr/micrometer/tree/fix-aspectj-ctw ? There are a few things I noticed missing from this PR:
I have all this addressed in #5060 and added also CTW in https://github.com/mihalyr/micrometer/tree/fix-aspectj-ctw. I named the module slightly differently But I didn't address the There is a thread-safety issue with the new |
I think we can do everything in this PR. So please create a PR to the issues_#1149_ctw branch so that I can merge your changes. |
Ok, no problem, just wanted to point out that my change in #5060 is a bugfix for the broken pointcut with AspectJ load-time weaving rather an enhancement as the compile-time weaving is. |
I understand. Regardless, let's go with everything in that PR. I'll merge it to my branch and then will refine the branch. |
@marcingrzejszczak here we go #5064 This works for me locally the only issue is that Spring aspect check that we know about - it should work with JDK17 and Spring 6.1.7-SNAPSHOT. |
65a1dca
to
b77a51d
Compare
@@ -3,7 +3,7 @@ activemq-artemis = "2.33.0" | |||
application-insights = "2.6.4" | |||
archunit = "1.3.0" | |||
asmForPlugins = "7.3.1" | |||
aspectjweaver = "1.9.22.1" | |||
aspectjweaver = "1.9.20.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.9.20.1 is the last release that accepts jdk 11 for building https://github.com/eclipse-aspectj/aspectj/blob/master/docs/release/JavaVersionCompatibility.adoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to avoid bumping this version in the future, we should leave a comment here so we know why. I'll add it as a polish commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Looks good, just minor comments.
<aspects> | ||
<!-- These are the two aspects we want to switch on for now. --> | ||
<aspect name="io.micrometer.observation.aop.ObservedAspect"/> | ||
</aspects> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this aop.xml
file is being used, it should be only needed for LTW with AspectJ which is tested in the micrometer-test-aspectj-ltw
module.
87851de
to
6dd40dc
Compare
micrometer-core/build.gradle
Outdated
@@ -81,6 +82,8 @@ dependencies { | |||
} | |||
|
|||
// Aspects | |||
implementation libs.aspectjrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to add a transitive dependency for micrometer-core users (same for micrometer-observation). When/how is this dependency used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a known fact, and of course it is a dependency. I already explained long ago in #1149 (comment). Using annotations like Why would you want to avoid the dependency? It is like e.g. using Log4J and trying to avoid declaring a dependency on the library. IMO, it does make a lot of sense to have this dependency. You always had it, both for Spring AOP and AspectJ. If you did not advertise it to users before, it was wrong to begin with and a bug. Edit: In Maven, there is the concept of an optional dependency. If not all Micrometer users need Spring AOP or AspectJ aspects - I have no idea how to use Micrometer, not being an active user - maybe you can declare it as optional, if Gradle supports that. But I guess anyone using the aspect-driven Micrometer annotations absolutely needs the dependency. How else are the aspects supposed to work? |
Yeah so that being optional would be the way to go. E.g. in Spring users need to explicitly say that they want aspect support by adding |
Maybe in the future, you can split the library and create something like a |
This commit makes the aspectj deps optional |
I mentioned this also previously in #1149 (comment)
But for some reason my build failed when I only used it as an optional as it seemed CTW required the dependency during the build, not sure how does it work with the latest change, but I'm happy with it 😄 |
java11Implementation libs.aspectjrt | ||
optionalApi 'org.aspectj:aspectjweaver' | ||
optionalApi libs.aspectjrt | ||
java11RuntimeOnly libs.aspectjrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this still adds this as a transient dependency for the Java 11 build, is that OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was the closest I could get to optional for java11. I'm not an expert with Gradle 😢 cc @jonatan-ivanov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also not an expert with Gradle and don't know much about the project setup, just caught my 👁️ on the "runtime", but I made a little test with Java 11 locally and I don't see aspecjrt included, so it might be OK
runtimeClasspath - Runtime classpath of source set 'main'.
+--- io.micrometer:micrometer-java11:1.14.0-SNAPSHOT
| \--- io.micrometer:micrometer-core:1.14.0-SNAPSHOT
| +--- io.micrometer:micrometer-commons:1.14.0-SNAPSHOT
| +--- io.micrometer:micrometer-observation:1.14.0-SNAPSHOT
| | \--- io.micrometer:micrometer-commons:1.14.0-SNAPSHOT
| +--- org.hdrhistogram:HdrHistogram:2.2.1
| \--- org.latencyutils:LatencyUtils:2.0.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the dependency at all for the java11 sources in micrometer-core. There is no aspect-related code there, and the code that is there is deprecated in favor of the separate module micrometer-java11.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do that then we get 😞
* What went wrong:
Execution failed for task ':micrometer-core:compileJava11Java'.
> Cannot infer AspectJ class path because no AspectJ Jar was found on class path: configuration ':micrometer-core:java11RuntimeClasspath'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two new test modules should be configured to not be published. Other than that, and my comment about the java11 dependency, the changes look good to me.
There is |
I mean we do have those classes but you don't need to use them. E.g. in Spring Boot not a single aspect will be registered if the aop dependencies are not on the classpath. |
I've added a commit about not deploying the aspectj test modules. Hopefully now it's ok? |
* Revert "Fix AspectJ pointcut syntax (#5058)" This reverts commit 5e16809. * Fix AspectJ load-time weaving and class-level annotations Fix AspectJ pointcut syntax to use `&& !` instead `and not` which is invalid for the AspectJ compiler/weaver and only works with the Spring AOP implementation. Also add `&& execution(* *.*(..))` to match only methods, because the implementation assumes it gets only MethodSignatures and crashes on ConstructorSignature at runtime. Fixed the thread-safety and mutability issues with the singleton Observations class, so changes are propagated to aspects that are initialized only once. Added AspectJ load-time weaving tests to make sure that the further issues with pointcuts and aspects are noticed at build time. * Add more AspectJ compile-time tests Added more class-level annotation tests and moved the module to 'micrometer-test-aspectj-ctw' to align with 'micrometer-test-aspectj-ltw'. * Revert CountedAspect method change with simpler syntax A fix kindly provided by @kriegaex that avoids changing the method signature and thus breaking binary compatibility and still fixes the problem with double counting in AspectJ. See the explanation in #1149 (comment) --------- Co-authored-by: Marcin Grzejszczak <[email protected]>
Co-authored-by: Robert Mihaly <[email protected]>
Co-authored-by: Robert Mihaly <[email protected]>
52f2f80
to
a1c9d3f
Compare
I rebased and resolved the conflicts. I think this is ready to be merged.
I think your points are reasonable. Historically, Micrometer has not taken such an approach except where we have no choice due to a conflict (see micrometer-jakarta9, micrometer-jetty12, etc). Almost all of the optional dependencies in micrometer-core are libraries that Micrometer maintains instrumentation for in micrometer-core. The aspects are a bit different in nature, and perhaps a different approach would have been better for them, but TimedAspect at least has been there since 1.0.0. We could consider making a separate aspect module and migrating users to there from the aspects in micrometer-core and micrometer-observation now, but that will be a long process and it should be considered separate of this work since this can still provide benefit without otherwise changing the current arrangement. |
…spectj-ltw This commit also polishes the changes made in micrometer-metricsgh-5059.
Adds support for CTW (compile time weaving) and LTW (load time weaving) support
fixes #1149