-
Notifications
You must be signed in to change notification settings - Fork 318
Map OpenTelemetry muzzle references to Datadog equivalent #7142
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b447d08
Use extended class-loader to lookup static Muzzle classes
mcculls c19edee
Generate empty static Muzzle classes for OpenTelemetry extensions
mcculls f1941f2
Retain original getMuzzleReferences method
mcculls 93b743d
Map unshaded ASM package to the shaded copy in byte-buddy
mcculls ad8d0ca
Provide Datadog equivalent of OpenTelemetry ClassRefBuilder
mcculls 0df097c
Map OpenTelemetry ClassRefBuilder to OtelMuzzleRefBuilder and related…
mcculls ca35c7b
Lazily build OpenTelemetry muzzle references
mcculls 50953df
Remove injected helper classes from OpenTelemetry's generated muzzle map
mcculls 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
138 changes: 138 additions & 0 deletions
138
...t-otel/otel-tooling/src/main/java/datadog/opentelemetry/tooling/OtelMuzzleRefBuilder.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,138 @@ | ||
| package datadog.opentelemetry.tooling; | ||
|
|
||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_INTERFACE; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_FINAL; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_INTERFACE; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_PRIVATE; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_NON_STATIC; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_PUBLIC; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_PUBLIC_OR_PROTECTED; | ||
| import static datadog.trace.agent.tooling.muzzle.Reference.EXPECTS_STATIC; | ||
|
|
||
| import datadog.trace.agent.tooling.muzzle.Reference; | ||
| import java.util.Collection; | ||
| import net.bytebuddy.jar.asm.Type; | ||
|
|
||
| /** Maps OpenTelemetry muzzle references to the Datadog equivalent. */ | ||
| public final class OtelMuzzleRefBuilder { | ||
| private final Reference.Builder builder; | ||
|
|
||
| OtelMuzzleRefBuilder(String className) { | ||
| this.builder = new Reference.Builder(className); | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder setSuperClassName(String superName) { | ||
| builder.withSuperName(superName); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addInterfaceNames(Collection<String> interfaceNames) { | ||
| for (String interfaceName : interfaceNames) { | ||
| builder.withInterface(interfaceName); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addInterfaceName(String interfaceName) { | ||
| builder.withInterface(interfaceName); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addSource(String sourceName) { | ||
| builder.withSource(sourceName, 0); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addSource(String sourceName, int line) { | ||
| builder.withSource(sourceName, line); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addFlag(Flag flag) { | ||
| builder.withFlag(flag.bit); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addField( | ||
| Source[] sources, Flag[] flags, String fieldName, Type fieldType, boolean isDeclared) { | ||
| builder.withField(Source.flatten(sources), Flag.flatten(flags), fieldName, fieldType); | ||
| return this; | ||
| } | ||
|
|
||
| public OtelMuzzleRefBuilder addMethod( | ||
| Source[] sources, Flag[] flags, String methodName, Type returnType, Type... argumentTypes) { | ||
| builder.withMethod( | ||
| Source.flatten(sources), Flag.flatten(flags), methodName, returnType, argumentTypes); | ||
| return this; | ||
| } | ||
|
|
||
| public ClassRef build() { | ||
| return new ClassRef(builder.build()); | ||
| } | ||
|
|
||
| public static final class ClassRef extends Reference { | ||
| public ClassRef(Reference ref) { | ||
| super( | ||
| ref.sources, | ||
| ref.flags, | ||
| ref.className, | ||
| ref.superName, | ||
| ref.interfaces, | ||
| ref.fields, | ||
| ref.methods); | ||
| } | ||
|
|
||
| public static OtelMuzzleRefBuilder builder(String className) { | ||
| return new OtelMuzzleRefBuilder(className); | ||
| } | ||
| } | ||
|
|
||
| public enum Flag { | ||
| PUBLIC(EXPECTS_PUBLIC), | ||
| PROTECTED_OR_HIGHER(EXPECTS_PUBLIC_OR_PROTECTED), | ||
| PROTECTED(EXPECTS_PUBLIC_OR_PROTECTED), | ||
| PACKAGE_OR_HIGHER(EXPECTS_NON_PRIVATE), | ||
| PACKAGE(EXPECTS_NON_PRIVATE), | ||
| PRIVATE_OR_HIGHER(0), | ||
| PRIVATE(0), | ||
| ABSTRACT(0), | ||
| FINAL(0), | ||
| NON_FINAL(EXPECTS_NON_FINAL), | ||
| INTERFACE(EXPECTS_INTERFACE), | ||
| NON_INTERFACE(EXPECTS_NON_INTERFACE), | ||
| STATIC(EXPECTS_STATIC), | ||
| NON_STATIC(EXPECTS_NON_STATIC); | ||
|
|
||
| final int bit; | ||
|
|
||
| Flag(int bit) { | ||
| this.bit = bit; | ||
| } | ||
|
|
||
| public static int flatten(Flag[] flags) { | ||
| int bits = 0; | ||
| for (Flag flag : flags) { | ||
| bits |= flag.bit; | ||
| } | ||
| return bits; | ||
| } | ||
| } | ||
|
|
||
| public static final class Source { | ||
| final String name; | ||
| final int line; | ||
|
|
||
| public Source(String name, int line) { | ||
| this.name = name; | ||
| this.line = line; | ||
| } | ||
|
|
||
| public static String[] flatten(Source[] sources) { | ||
| String[] locations = new String[sources.length]; | ||
| for (int i = 0; i < sources.length; i++) { | ||
| locations[i] = sources[i].name + ":" + sources[i].line; | ||
| } | ||
| return locations; | ||
| } | ||
| } | ||
| } |
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
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.
Calling
getMuzzleReferences()will call some OTel instrumentation code to build references that is remapped the DDOtelMuzzleRefBuilderwhich builds our own toolingClassRef.As
getMuzzleReferences()returns aMapand there is type erasure, JVM don't complain at runtime we are now returning aMapwith DDClassRefinstances as values, rather than the original OTelClassRef, right?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.
We map all references to the original OTel
ClassRefinOtelInstrumentationMapperto our replacementClassRef. This replacement is binary compatible with the original, but it lets us adapt the data being built up using theseClassRefs to our expectedReferencetype. So from the JVM's perspective it's still dealing with a map ofClassRefs, just one that we control.Note the
getMuzzleReferences()method generated by OTel uses a builder calledClassRefBuilder, which is bootstrapped fromClassRef.builder(className)- so the process is as follows:MuzzleCheckloadStaticMuzzleReferencesto load the static class (empty for OTel extensions)buildReferencesmethod, which calls the generated OTelgetMuzzleReferencesmethodgetMuzzleReferencescreates aClassRefBuilder(which is binary compatible with the original OTel one) and feeds it reference dataClassRefs - which are just a thin layer aroundReferenceReferences and return them toMuzzleCheck