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 AspectJ load-time weaving and class-level annotations #5060

Closed
wants to merge 1 commit into from
Closed

Fix AspectJ load-time weaving and class-level annotations #5060

wants to merge 1 commit into from

Conversation

mihalyr
Copy link
Contributor

@mihalyr mihalyr commented May 9, 2024

The pointcut syntax used in the aspects is not valid for the AspectJ
weaver/compiler. Replaced and not with && ! to fix it and also had
to add a condition to match only methods and not constructors which the
implementation assumed it gets.

One problem was that the aspects were only tested with Spring AOP
proxies, but AspectJ weaver/compiler bugs were going unnoticed. The
added LTW test module will run with the aspectjweaver java agent to
weave the classes at load time, then tests if the aspects are applied
and metrics collected as expected.

@mihalyr
Copy link
Contributor Author

mihalyr commented May 9, 2024

I sent the pointcut syntax separately to be able to merge it ASAP to fix the crashes #5061

@mihalyr mihalyr changed the title Fix AspectJ load-time weaving and class-level annotations Add AspectJ load-time weaving tests May 9, 2024
@mihalyr mihalyr marked this pull request as ready for review May 9, 2024 20:24
@mihalyr
Copy link
Contributor Author

mihalyr commented May 9, 2024

@marcingrzejszczak I think you have this also covered in your PR draft, feel free to cancel this one if you are adding the tests there.

@jonatan-ivanov
Copy link
Member

The build is failing because of checkstyle, running ./gradlew format + commit-push the changes should fix it.

The pointcut syntax used in the aspects is not valid for the AspectJ
weaver/compiler. Replaced `and not` with `&& !` to fix it and also had
to add a condition to match only methods and not constructors which the
implementation assumed it gets.

One problem was that the aspects were only tested with Spring AOP
proxies, but AspectJ weaver/compiler bugs were going unnoticed.  The
added LTW test module will run with the `aspectjweaver` java agent to
weave the classes at load time, then tests if the aspects are applied
and metrics collected as expected.
@mihalyr mihalyr changed the title Add AspectJ load-time weaving tests Fix AspectJ load-time weaving and class-level annotations May 10, 2024
@mihalyr
Copy link
Contributor Author

mihalyr commented May 10, 2024

@jonatan-ivanov @marcingrzejszczak I've updated this PR now with the reverted changes from main and the change from my other LTW PR that I merged into one here.

This is now passing every check locally with ./gradlew -x japicmp check

The only remaining issue is with japicmp because I changed a method in CountedAspect.

Comparing binary compatibility of  against 
***! MODIFIED CLASS: PUBLIC io.micrometer.core.aop.CountedAspect  (not serializable)
	===  CLASS FILE FORMAT VERSION: 52.0 <- 52.0
	===  UNCHANGED SUPERCLASS: java.lang.Object (<- java.lang.Object)
	---! REMOVED METHOD: PUBLIC(-) java.lang.Object interceptAndRecord(org.aspectj.lang.ProceedingJoinPoint, io.micrometer.core.annotation.Counted)
		---  REMOVED EXCEPTION: java.lang.Throwable
***  MODIFIED CLASS: PUBLIC io.micrometer.core.instrument.config.InvalidConfigurationException  (default serialVersionUID changed)
	===  CLASS FILE FORMAT VERSION: 52.0 <- 52.0
	===  UNCHANGED SUPERCLASS: java.lang.IllegalStateException (<- java.lang.IllegalStateException)

@@ -167,7 +167,7 @@ public CountedAspect(MeterRegistry registry, Function<ProceedingJoinPoint, Itera
this.shouldSkip = shouldSkip;
}

@Around("@within(io.micrometer.core.annotation.Counted) and not @annotation(io.micrometer.core.annotation.Counted)")
@Around("@within(io.micrometer.core.annotation.Counted) && !@annotation(io.micrometer.core.annotation.Counted) && execution(* *(..))")
Copy link
Contributor

Choose a reason for hiding this comment

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

why is there && execution(* *(..)) at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the implementation has an unchecked class-cast to MethodSignature that causes runtime crashes if the pointcut matches a constructor, which it does without this addition that restricts it only to method signatures (with a return type).

  • @within(io.micrometer.core.annotation.Counted) - will match the methods of class annotated with @Counted
  • && !@annotation(io.micrometer.core.annotation.Counted) - excludes methods that are already annotated separately with @Counted to avoid double instrumentation
  • && execution(* *(..)) - matches method signatures only excluding constructors

What happens if you don't include the last part is that if you annotate a class with @Counted it will crash at runtime when you try to create a new instance of the class because the pointcut would match the constructor which gives a ConstructorSignature and fails to be cast to MethodSignature in the implementation. The tests cover this, so if you remove that last part you'll see the error when running the tests.

@mihalyr
Copy link
Contributor Author

mihalyr commented May 10, 2024

Agree with @marcingrzejszczak that we will do this together in his PR over at #5059 (comment)

@mihalyr mihalyr closed this May 10, 2024
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