-
Notifications
You must be signed in to change notification settings - Fork 990
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group = 'io.micrometer' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
configurations { | ||
agents | ||
} | ||
|
||
dependencies { | ||
agents libs.aspectjweaver | ||
implementation project(':micrometer-core') | ||
testImplementation libs.junitJupiter | ||
testImplementation libs.assertj | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
jvmArgs '-javaagent:' + configurations.agents.files.find { it.name.startsWith('aspectjweaver') } | ||
} |
39 changes: 39 additions & 0 deletions
39
micrometer-test-aspectj-ltw/src/main/java/io/micrometer/MeasuredClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer; | ||
|
||
import io.micrometer.core.annotation.Counted; | ||
import io.micrometer.core.annotation.Timed; | ||
|
||
@Counted | ||
@Timed | ||
public class MeasuredClass { | ||
|
||
@Timed | ||
public void timedMethod() { | ||
} | ||
|
||
@Counted | ||
public void countedMethod() { | ||
} | ||
|
||
public void classLevelTimedMethod() { | ||
} | ||
|
||
public void classLevelCountedMethod() { | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
micrometer-test-aspectj-ltw/src/main/java/io/micrometer/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Support for testing. | ||
*/ | ||
package io.micrometer; |
11 changes: 11 additions & 0 deletions
11
micrometer-test-aspectj-ltw/src/main/resources/META-INF/aop.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://eclipse.dev/aspectj/dtd/aspectj.dtd"> | ||
<aspectj> | ||
<weaver options="-verbose -showWeaveInfo"> | ||
<include within="io.micrometer.core.aop.*"/> | ||
<include within="io.micrometer.MeasuredClass"/> | ||
</weaver> | ||
<aspects> | ||
<aspect name="io.micrometer.core.aop.CountedAspect"/> | ||
<aspect name="io.micrometer.core.aop.TimedAspect"/> | ||
</aspects> | ||
</aspectj> |
103 changes: 103 additions & 0 deletions
103
micrometer-test-aspectj-ltw/src/test/java/io/micrometer/MeasuredClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import org.junit.jupiter.api.*; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class MeasuredClassTest { | ||
|
||
static MeterRegistry meterRegistry; | ||
|
||
@BeforeAll | ||
static void setUpAll() { | ||
meterRegistry = new SimpleMeterRegistry(); | ||
Metrics.addRegistry(meterRegistry); | ||
} | ||
|
||
MeasuredClass measuredClass; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
measuredClass = new MeasuredClass(); | ||
} | ||
|
||
@Test | ||
void it_measures_timed_method() { | ||
measuredClass.timedMethod(); | ||
|
||
var timer = meterRegistry.get("method.timed") | ||
.tag("class", MeasuredClass.class.getName()) | ||
.tag("method", "timedMethod") | ||
.timer(); | ||
|
||
assertThat(timer.count()).isOne(); | ||
|
||
measuredClass.timedMethod(); | ||
assertThat(timer.count()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void it_measures_counted_method() { | ||
measuredClass.countedMethod(); | ||
|
||
var counter = meterRegistry.get("method.counted") | ||
.tag("class", MeasuredClass.class.getName()) | ||
.tag("method", "countedMethod") | ||
.counter(); | ||
|
||
assertThat(counter.count()).isOne(); | ||
|
||
measuredClass.countedMethod(); | ||
assertThat(counter.count()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void it_measures_class_level_timed_method() { | ||
measuredClass.classLevelTimedMethod(); | ||
|
||
var timer = meterRegistry.get("method.timed") | ||
.tag("class", MeasuredClass.class.getName()) | ||
.tag("method", "classLevelTimedMethod") | ||
.timer(); | ||
|
||
assertThat(timer.count()).isOne(); | ||
|
||
measuredClass.classLevelTimedMethod(); | ||
assertThat(timer.count()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void it_measures_class_level_counted_method() { | ||
measuredClass.classLevelCountedMethod(); | ||
|
||
var counter = meterRegistry.get("method.counted") | ||
.tag("class", MeasuredClass.class.getName()) | ||
.tag("method", "classLevelCountedMethod") | ||
.counter(); | ||
|
||
assertThat(counter.count()).isOne(); | ||
|
||
measuredClass.classLevelCountedMethod(); | ||
assertThat(counter.count()).isEqualTo(2); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why is there
&& execution(* *(..))
at the end?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.
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 constructorsWhat 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.