-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile-time and load-time weaving support for aspects (#5059)
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. 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) Fixes gh-1149 --------- Co-authored-by: Marcin Grzejszczak <[email protected]> Co-authored-by: Robert Mihaly <[email protected]>
- Loading branch information
1 parent
51b9b4f
commit 763e1f7
Showing
21 changed files
with
750 additions
and
12 deletions.
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
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
106 changes: 106 additions & 0 deletions
106
micrometer-observation/src/main/java/io/micrometer/observation/Observations.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,106 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* 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.observation; | ||
|
||
import io.micrometer.common.lang.Nullable; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Generator of observations bound to a static global registry. For use especially in | ||
* places where dependency injection of {@link ObservationRegistry} is not possible for an | ||
* instrumented type. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 1.14.0 | ||
*/ | ||
public final class Observations { | ||
|
||
private static final ObservationRegistry initialRegistry = ObservationRegistry.create(); | ||
|
||
private static final DelegatingObservationRegistry globalRegistry = new DelegatingObservationRegistry( | ||
initialRegistry); | ||
|
||
private Observations() { | ||
throw new UnsupportedOperationException("You can't instantiate a utility class"); | ||
} | ||
|
||
/** | ||
* Sets a registry as the global registry. | ||
* @param registry Registry to set. | ||
*/ | ||
public static void setRegistry(ObservationRegistry registry) { | ||
globalRegistry.setDelegate(registry); | ||
} | ||
|
||
/** | ||
* Resets registry to the original, empty one. | ||
*/ | ||
public static void resetRegistry() { | ||
globalRegistry.setDelegate(initialRegistry); | ||
} | ||
|
||
/** | ||
* Retrieves the current global instance. | ||
* @return Global registry. | ||
*/ | ||
public static ObservationRegistry getGlobalRegistry() { | ||
return globalRegistry; | ||
} | ||
|
||
private static final class DelegatingObservationRegistry implements ObservationRegistry { | ||
|
||
private final AtomicReference<ObservationRegistry> delegate = new AtomicReference<>(ObservationRegistry.NOOP); | ||
|
||
DelegatingObservationRegistry(ObservationRegistry delegate) { | ||
setDelegate(delegate); | ||
} | ||
|
||
void setDelegate(ObservationRegistry delegate) { | ||
this.delegate.set(Objects.requireNonNull(delegate, "Delegate must not be null")); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Observation getCurrentObservation() { | ||
return delegate.get().getCurrentObservation(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Observation.Scope getCurrentObservationScope() { | ||
return delegate.get().getCurrentObservationScope(); | ||
} | ||
|
||
@Override | ||
public void setCurrentObservationScope(@Nullable Observation.Scope current) { | ||
delegate.get().setCurrentObservationScope(current); | ||
} | ||
|
||
@Override | ||
public ObservationConfig observationConfig() { | ||
return delegate.get().observationConfig(); | ||
} | ||
|
||
@Override | ||
public boolean isNoop() { | ||
return delegate.get().isNoop(); | ||
} | ||
|
||
} | ||
|
||
} |
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,19 @@ | ||
plugins { | ||
id 'java' | ||
alias(libs.plugins.aspectj) | ||
} | ||
|
||
description 'AspectJ compile-time weaving test for Micrometer aspects' | ||
|
||
dependencies { | ||
implementation project(':micrometer-core') | ||
aspect project(':micrometer-core') | ||
aspect project(':micrometer-observation') | ||
|
||
testImplementation libs.junitJupiter | ||
testImplementation libs.assertj | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/micrometer-test-aspectj-ctw/src/main/java/io/micrometer/test/ctw/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,48 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* 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.test.ctw; | ||
|
||
import io.micrometer.core.annotation.Counted; | ||
import io.micrometer.core.annotation.Timed; | ||
import io.micrometer.observation.annotation.Observed; | ||
|
||
@Observed | ||
@Counted | ||
@Timed | ||
public class MeasuredClass { | ||
|
||
@Timed | ||
public void timedMethod() { | ||
} | ||
|
||
@Counted | ||
public void countedMethod() { | ||
} | ||
|
||
@Observed | ||
public void observedMethod() { | ||
} | ||
|
||
public void classLevelTimedMethod() { | ||
} | ||
|
||
public void classLevelCountedMethod() { | ||
} | ||
|
||
public void classLevelObservedMethod() { | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
tests/micrometer-test-aspectj-ctw/src/main/java/io/micrometer/test/ctw/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,21 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* 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. | ||
*/ | ||
@NonNullApi | ||
@NonNullFields | ||
package io.micrometer.test.ctw; | ||
|
||
import io.micrometer.common.lang.NonNullApi; | ||
import io.micrometer.common.lang.NonNullFields; |
Oops, something went wrong.