-
Notifications
You must be signed in to change notification settings - Fork 966
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
Changes from all commits
1a4dd16
29b003f
ded899c
34b3bcf
037305d
14a08ed
9e78261
440f998
42c9868
1d0c2c9
706ef1a
a1c9d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
plugins { | ||
alias(libs.plugins.kotlin19) | ||
alias(libs.plugins.aspectj) | ||
id 'me.champeau.mrjar' version "0.1.1" | ||
} | ||
|
||
|
@@ -81,7 +82,8 @@ dependencies { | |
} | ||
|
||
// Aspects | ||
optionalApi 'org.aspectj:aspectjweaver' | ||
optionalApi libs.aspectjrt | ||
java11RuntimeOnly libs.aspectjrt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. If I do that then we get 😞
|
||
|
||
// instrumentation options | ||
optionalApi 'io.dropwizard.metrics:metrics-core' // TODO move dropwizard out of core module? DropwizardMeterRegistry for e.g. JMX registry | ||
|
@@ -209,6 +211,8 @@ dependencies { | |
testImplementation 'io.grpc:grpc-inprocess' | ||
testImplementation 'io.grpc:grpc-testing-proto' | ||
testImplementation 'com.squareup.retrofit2:retrofit' | ||
|
||
testImplementation 'org.aspectj:aspectjweaver' | ||
} | ||
|
||
task shenandoahTest(type: Test) { | ||
|
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(); | ||
} | ||
|
||
} | ||
|
||
} |
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() | ||
} |
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() { | ||
} | ||
|
||
} |
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; |
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.