-
Notifications
You must be signed in to change notification settings - Fork 321
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
Exclude virtual threads from inferred spans feature #3244
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
764dfa2
Added fast check vor virtual threads, added profiler test case
JonasKunz e194c2a
Fix method lookup
JonasKunz 7e4293a
Added profiling active check
JonasKunz 692555b
Exclude virtual threads from profiling
JonasKunz a2c892e
Add license notice
JonasKunz 2273280
Added fast check vor virtual threads, added profiler test case
JonasKunz 51b3fbd
Fix method lookup
JonasKunz 87be763
Added profiling active check
JonasKunz 93fbda0
Exclude virtual threads from profiling
JonasKunz 8680956
Add license notice
JonasKunz 70c0d92
Merge remote-tracking branch 'origin/loom-async-profiler' into loom-a…
JonasKunz 03a7cbf
Merge remote-tracking branch 'elastic/main' into loom-async-profiler
JonasKunz 855a463
Updated docs, added changelog
JonasKunz 3a9c68d
Merge branch 'main' into loom-async-profiler
SylvainJuge 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
79 changes: 79 additions & 0 deletions
79
apm-agent-plugin-sdk/src/main/java/co/elastic/apm/agent/sdk/internal/ThreadUtil.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,79 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 | ||
* | ||
* http://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 co.elastic.apm.agent.sdk.internal; | ||
|
||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | ||
import net.bytebuddy.implementation.MethodCall; | ||
import net.bytebuddy.matcher.ElementMatchers; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
public class ThreadUtil { | ||
|
||
interface VirtualChecker { | ||
boolean isVirtual(Thread thread); | ||
} | ||
|
||
private static final VirtualChecker VIRTUAL_CHECKER = generateVirtualChecker(); | ||
|
||
|
||
public static boolean isVirtual(Thread thread) { | ||
return VIRTUAL_CHECKER.isVirtual(thread); | ||
} | ||
|
||
/** | ||
* Generates a VirtualChecker based on the current JVM. | ||
* If the JVM does not support virtual threads, a VirtualChecker which always returns false is returned. | ||
* <p> | ||
* Otherwise we runtime generate an implementation which invokes Thread.isVirtual(). | ||
* We use runtime proxy generation because Thread.isVirtual() has been added in Java 19 as preview and Java 21 as non preview. | ||
* Therefore we would require a compilation with Java 19 (non-LTS), because Java 20+ does not allow targeting Java 7. | ||
* <p> | ||
* Alternatively we could simply invoke Thread.isVirtual via reflection. | ||
* However, because this check can be used very frequently we want to avoid the penalty / missing inline capability of reflection. | ||
* | ||
* @return the implementation for {@link VirtualChecker}. | ||
*/ | ||
private static VirtualChecker generateVirtualChecker() { | ||
Method isVirtual = null; | ||
try { | ||
isVirtual = Thread.class.getMethod("isVirtual"); | ||
isVirtual.invoke(Thread.currentThread()); //invoke to ensure it does not throw exceptions for preview versions | ||
Class<? extends VirtualChecker> impl = new ByteBuddy() | ||
.subclass(VirtualChecker.class) | ||
.method(ElementMatchers.named("isVirtual")) | ||
.intercept(MethodCall.invoke(isVirtual).onArgument(0)) | ||
.make() | ||
.load(VirtualChecker.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION) | ||
.getLoaded(); | ||
return impl.getConstructor().newInstance(); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
return new VirtualChecker() { | ||
@Override | ||
public boolean isVirtual(Thread thread) { | ||
return false; //virtual threads are not supported, therefore no thread is virtual | ||
} | ||
}; | ||
} catch (InstantiationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
apm-agent-plugin-sdk/src/test/java/co/elastic/apm/agent/sdk/internal/ThreadUtilTest.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,43 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 | ||
* | ||
* http://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 co.elastic.apm.agent.sdk.internal; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledForJreRange; | ||
import org.junit.jupiter.api.condition.JRE; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class ThreadUtilTest { | ||
|
||
@Test | ||
public void checkPlatformThreadVirtual() { | ||
Thread t1 = new Thread(); | ||
assertThat(ThreadUtil.isVirtual(t1)).isFalse(); | ||
} | ||
|
||
@Test | ||
@DisabledForJreRange(max = JRE.JAVA_20) | ||
public void checkVirtualThreadVirtual() throws Exception { | ||
Runnable task = () -> { | ||
}; | ||
Thread thread = (Thread) Thread.class.getMethod("startVirtualThread", Runnable.class).invoke(null, task); | ||
assertThat(ThreadUtil.isVirtual(thread)).isTrue(); | ||
} | ||
} |
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
Oops, something went wrong.
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.
Nice ! I think that's definitely a good approach we could use for other deprecated or soon-to-be-removed APIs like the security manager.