-
Notifications
You must be signed in to change notification settings - Fork 355
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
Changes from all commits
d38bdd4
dd6a4ac
8666030
af1a4cb
dfa4605
b2e3704
2a0c4b3
5704828
7d9966d
b90f639
6d82a50
a8cdba9
d569e4e
04b01e4
eb22da6
9cea7a8
5241bcf
e1a67f9
9139e4e
6892d24
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 |
|---|---|---|
| @@ -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 = { | ||
|
Contributor
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. SHARED_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) { | ||
|
Contributor
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. FWIW I found |
||
| 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; | ||
| } | ||
| } | ||
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.