-
Notifications
You must be signed in to change notification settings - Fork 352
Infer instrumentation helper classes at build time #12059
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
Open
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d38bdd4
Implement auto-inferring helper methods
sarahchen6 dd6a4ac
Migrate some instrumentations
sarahchen6 8666030
Fix class crawl logic
sarahchen6 af1a4cb
Clean comments
sarahchen6 dfa4605
Make helper-name resolution lazy
sarahchen6 b2e3704
Do not capture entire module instanc at prepareInstrumentation
sarahchen6 2a0c4b3
Undo lazy loading
sarahchen6 5704828
Use source file to determine helpers
sarahchen6 7d9966d
Avoid injecting build-time-only muzzle classes
sarahchen6 b90f639
Add tests for full helper inference for manual specification
sarahchen6 6d82a50
Clean up
sarahchen6 a8cdba9
Clean comments
sarahchen6 d569e4e
Write helpers into module bytecode directly
sarahchen6 04b01e4
Only auto-generate if not manually listed
sarahchen6 eb22da6
Use either manual list or auto-generation
sarahchen6 9cea7a8
Clean up
sarahchen6 5241bcf
Infer source folder instead of passing in as param
sarahchen6 e1a67f9
Address review comments
sarahchen6 9139e4e
Address ForbiddenAPI
sarahchen6 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 hidden or 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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
.../agent-tooling/src/main/java/datadog/trace/agent/tooling/muzzle/HelperClassPredicate.java
This file contains hidden or 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,65 @@ | ||
| package datadog.trace.agent.tooling.muzzle; | ||
|
|
||
| import datadog.trace.bootstrap.Constants; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * Classifies a referenced class as an injectable tracer helper, a bootstrap class, or a library | ||
| * class — similar to OpenTelemetry's {@code HelperClassPredicate#isHelperClass}. The primary signal | ||
| * is {@code ownOutput}: a class the instrumentation subproject compiled itself. | ||
| * | ||
| * <p>A subproject only injects helpers it owns; a helper owned by another subproject must be | ||
| * declared explicitly via {@code helperClassNames()}. {@link #HELPER_PREFIXES} lists the shared | ||
| * infrastructure subprojects that are not owned by a specific subproject and so are always treated | ||
| * as helpers. | ||
| */ | ||
| public final class HelperClassPredicate { | ||
|
|
||
| static final String[] HELPER_PREFIXES = { | ||
| "datadog.opentelemetry.shim.", | ||
| "datadog.trace.agent.tooling.iast.", | ||
| "datadog.trace.agent.tooling.nativeimage.", | ||
| }; | ||
|
|
||
| private final Predicate<String> ownOutput; | ||
|
|
||
| /** | ||
| * @param ownOutput tests whether a class name was compiled by the instrumentation subproject | ||
| * itself; injected so this classifier stays independent of the build directory layout. | ||
| */ | ||
| public HelperClassPredicate(final Predicate<String> ownOutput) { | ||
| this.ownOutput = ownOutput; | ||
| } | ||
|
|
||
| public boolean isHelperClass(final String className) { | ||
| return !isBootstrap(className) && (ownOutput.test(className) || matchesHelperPrefix(className)); | ||
| } | ||
|
|
||
| private static boolean matchesHelperPrefix(final String className) { | ||
| for (final String prefix : HELPER_PREFIXES) { | ||
| if (className.startsWith(prefix)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Whether the class is on the bootstrap class-path and so never injected. */ | ||
| public static boolean isBootstrap(final String className) { | ||
| if (className.startsWith("java.") | ||
| || className.startsWith("javax.") | ||
| || className.startsWith("jdk.") | ||
| || className.startsWith("com.sun.") | ||
| || className.startsWith("sun.") | ||
| || className.startsWith("org.slf4j.") | ||
| || className.startsWith("datadog.slf4j.")) { | ||
| return true; | ||
| } | ||
| for (final String prefix : Constants.BOOTSTRAP_PACKAGE_PREFIXES) { | ||
| if (className.startsWith(prefix)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
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.
MuzzleGeneratorneeds to callHelperScanner.withClassDependenciesat build-time to expand and order the helpers found, but there's no AgentClassLoader during the build (there is during runtime which is previously the only place we calledHelperScanner.withClassDependencies) - so we need to pass in the build classpath's locator to use.