-
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 weaving support for aspects #1149
Comments
Have you added aspectj weaving to the build section of your POM?
|
Yes. The aspectj-maven-plugin configuration I posted in my initial post is under |
My current hypothesis is that the |
Also, one way to work around this is to write your own aspect that weaves around an annotation and use the |
I don't have much expertise on AspectJ but after some quick research, it seems to require a similar configuration to |
Looks like spring-aspects.jar compiles with ajc, then. I think the solution to this ticket is to either compile Micrometer with ajc, or improve the documentation on how to get Micrometer to work properly with AspectJ. I can write a small addition to the documentation explaining this, if desired. |
I am running into the exact same issue as @jefftap. While the compile time weaving seems to succeed, at runtime the aspectOf methods are not there on the
Would be great to have some documentation on how to get this working. |
Methods annotated with @timed now use Micrometers annotation. The @Gauge annotation has been remove without replacement for now. While configuration for the use of Micrometer's TimedAspect has been prepared, it is not yet active because of compatibility issues with compile-time weaving. See micrometer-metrics/micrometer#1149.
I didn't manage to get this working with CTW. As far as I understood it, it seems that micrometer-core is not compiled with the ajc and thus the aspects are missing the required factory methods. |
The issue still persist. As noted higher it could be resolved by using LTW, but in my case it is not an option because I do not have access to the deployment setup, and the only option is CTW which doesn't work with this library |
Same issue here, CTW does not work. Is there any plan to support it properly, so CTW will work ? |
I've had the same issue with CTW, although I got around this by wrapping the aspect like this for now.
and adding the following to the aop.xml
It would be great if this works out of the box for CTW. |
Hi, for those on the interweb who came here for load time weaving (LTW), I confirm that it works. <aspectj>
<weaver>
<include within="io.micrometer.core..*"/>
<include within="my.app..*"/>
</weaver>
<aspects>
<aspect name="io.micrometer.core.aop.TimedAspect"/>
</aspects>
</aspectj> To be used in conjunction with a class like: package my.app;
public class App {
@Timed("xyz")
public void fun1() { }
} In a Spring Boot application with the I'm a newbie when it comes to aspects, but I'm wondering what the advantages of CTW over LTW are. |
Because I often answer questions on StackOverflow, I found this issue, not for the first time in the last few years. Today I took some time to look into it, producing a little example project, showing how to apply https://github.com/kriegaex/SO_AJ_MicrometerTimed_67803726 The project simply consists of one class and a POM + an explanatory read-me file. The gist of it is: You must make sure to apply binary weaving to the Micrometer aspect in order to transform it into a real AspectJ aspect, and then apply that aspect to your own project. |
Since there are ways of how to achieve this is there anything else we should do here, other than maybe document this? |
My workaround is very contrived. IMO, it is unreasonable to assume that any normal user who does not happen to be an AspectJ expert can find out how to do this. Even if it was documented, users would be doing the Micrometer team's work there.
Yes, add an AspectJ compilation step to the Gradle build (e.g. using Freefair plugin) and make sure that what is advertised as an AspectJ aspect, ... micrometer/micrometer-core/src/main/java/io/micrometer/core/aop/TimedAspect.java Lines 36 to 37 in 2252347
... actually really does deserve its name, and avoid that incomplete non-AspectJ compilation with Javac needs to be finished by the user. That this happens to work with LTW, because the AspectJ weaver is smart enough to notice and finish the job, does not mean that CTW is some kind of second-class citizen. Make your product easy to use. The correctly compiled aspect would, of course, still work with LTW. With CTW, the library could just be put on the aspectpath, as is customary. No extra build steps or extra modules, creating a refined version of this library as an input for later steps. Of course, micrometer/micrometer-core/build.gradle Lines 80 to 81 in 2252347
which is a superset of |
I am trying to migrate my project from Dropwizard Metrics to Micrometer and I'm blocked on this issue as I am trying to use compile-time weaving just like with Dropwizard annotations before and it doesn't seem to work with Micrometer despite the documentation saying it can be used also with compile time weaving. I have a Gradle project and was trying to convert the above mentioned workaround from Maven to Gradle, but the plugins are different and am running into various build failures. I tried to revert to Spring AOP proxies, but that doesn't work on my project, CGLIB breaking some of the classes. Are there any updates on the fix? Or did anyone make it work with Gradle (Freefair plugin)? (Update: Fixed it by configuring AspectJ load-time weaving, seems to be working alongside CTW for other classes) |
@marcingrzejszczak, see, this is what I mean. It is possible to finish the job unfinished by Micrometer manually as a user, but it is not trivial. Instead of burdening users, trying to figure out how to use your aspect, because you think it is unnecessary to compile it with the AspectJ compiler, you should finally get this issue off the table. Or are you expecting me to convert my Maven solution to Gradle on behalf of @mihalyr-prospect? |
@kriegaex I don't think that your answer is nice or brings any value to this discussion. We will take care of this issue whenever we have bandwith to do so. |
@marcingrzejszczak: I wanted to be clear rather than nice. I was not super nice, maybe, but also not non-nice. I did not insult anyone. I simply want to push this forward. Honestly, this issue has been open for more than 5 years, and I do not really buy the bandwidth argument, because
The product I maintain, AspectJ, is a cornerstone of Micrometer, so please understand that I would rather like to see it fixed than feeling compelled to devise custom workarounds to various users for a few more years. |
Another hint, just in case you happen to be using or be planning to use pointcuts with something like Probably, this is not a problem at the moment, otherwise I guess you would be running into test problems already. Just a caveat for the future... |
Thank you Alexander, super useful information and the simpler syntax would solve binary compatibility issue I have also.
I can't comment on this part as it was @jhoeller who made that change in Spring, but I also assume it was something like that. |
* 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]>
@kriegaex we're building micrometer with jdk11. Can we fix the AspectJ compiler to 1.9.20.1? |
@marcingrzejszczak, yes, if you do not mind missing compiler enhancements and bugfixes. Would it be a big deal to build on JDK 17+ instead? JDKs 17 and 21 are LTS. |
We would have to discuss this internally @shakuzen @jonatan-ivanov |
* 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]>
Ok, #5059 is ready for review. Let's try to close this 5 year old issue ;) |
* 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]>
* 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]>
This has been delivered in |
ObservedAspect uses `and not` which is invalid for the AspectJ compiler/weaver and only works with the Spring AOP implementation. See micrometer-metrics/micrometer#1149
The aspect is excluded as a workaround because a change in one of the recent Micrometer releases breaks compilation with ajc. Once this is fixed in Micrometer, the exclusion can be removed. See: micrometer-metrics/micrometer#1149
I just found some time to test the snapshot briefly. I added the post-compile weaving Gradle plugin, removed the runtime javaagent and aop.xml from my project and checked that it builds and I get the metrics from the First, I thought, something might be missing as
But, I think, this was for the class-level annotation ( Before that, I tested also load-time weaving with the javaagent and was not getting the invalid pointcut syntax errors anymore at runtime, so that should be good too. What I don't use is Spring AOP, so I didn't test that. |
This was fixed as part of micrometer-metrics/micrometer#1149, see comment micrometer-metrics/micrometer#1149 (comment).
Hi, I am attempting to make use of
TimedAspect
via AspectJ compile-time weaving using theaspectj-maven-plugin
. When the aspect code is executed, it throws one of the two errors, depending on what is being executed:This usually indicates that the AspectJ weaver has not properly processed the aspect code. I am not sure if this is a problem with my own code/configuration, or if it's an issue in Micrometer's jar. The aspect works perfectly fine using Spring AOP, but I would prefer to use AspectJ weaving with
@Timed
to allow it access to private/protected methods and the other various benefits one gets from AspectJ integration.Relevant information:
aspectj-maven-plugin configuration:
The text was updated successfully, but these errors were encountered: