-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support instrumentation of repackaged libraries
- Loading branch information
Showing
8 changed files
with
136 additions
and
14 deletions.
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
30 changes: 30 additions & 0 deletions
30
...va-agent/agent-builder/src/main/java/datadog/trace/agent/tooling/ShadedAdviceLocator.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,30 @@ | ||
package datadog.trace.agent.tooling; | ||
|
||
import java.io.IOException; | ||
import net.bytebuddy.dynamic.ClassFileLocator; | ||
|
||
/** Locates and shades class-file resources from the advice class-loader. */ | ||
public final class ShadedAdviceLocator implements ClassFileLocator { | ||
private final ClassFileLocator adviceLocator; | ||
private final AdviceShader adviceShader; | ||
|
||
public ShadedAdviceLocator(ClassLoader adviceLoader, AdviceShader adviceShader) { | ||
this.adviceLocator = ClassFileLocator.ForClassLoader.of(adviceLoader); | ||
this.adviceShader = adviceShader; | ||
} | ||
|
||
@Override | ||
public Resolution locate(String className) throws IOException { | ||
final Resolution resolution = adviceLocator.locate(className); | ||
if (resolution.isResolved()) { | ||
return new Resolution.Explicit(adviceShader.shade(resolution.resolve())); | ||
} else { | ||
return resolution; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
adviceLocator.close(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AdviceShader.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,42 @@ | ||
package datadog.trace.agent.tooling; | ||
|
||
import datadog.trace.api.cache.DDCache; | ||
import datadog.trace.api.cache.DDCaches; | ||
import java.util.function.Function; | ||
import net.bytebuddy.jar.asm.ClassReader; | ||
import net.bytebuddy.jar.asm.ClassVisitor; | ||
import net.bytebuddy.jar.asm.ClassWriter; | ||
import net.bytebuddy.jar.asm.commons.ClassRemapper; | ||
import net.bytebuddy.jar.asm.commons.Remapper; | ||
|
||
/** Shades advice bytecode by applying a shading function to all references. */ | ||
public final class AdviceShader extends Remapper { | ||
private final DDCache<String, String> cache = DDCaches.newFixedSizeCache(64); | ||
private final Function<String, String> shading; | ||
|
||
public static AdviceShader with(Function<String, String> shading) { | ||
return shading != null ? new AdviceShader(shading) : null; | ||
} | ||
|
||
AdviceShader(Function<String, String> shading) { | ||
this.shading = shading; | ||
} | ||
|
||
/** Applies shading before calling the given {@link ClassVisitor}. */ | ||
public ClassVisitor shade(ClassVisitor cv) { | ||
return new ClassRemapper(cv, this); | ||
} | ||
|
||
/** Returns the result of shading the given bytecode. */ | ||
public byte[] shade(byte[] bytecode) { | ||
ClassReader cr = new ClassReader(bytecode); | ||
ClassWriter cw = new ClassWriter(null, 0); | ||
cr.accept(shade(cw), 0); | ||
return cw.toByteArray(); | ||
} | ||
|
||
@Override | ||
public String map(String name) { | ||
return cache.computeIfAbsent(name, shading); | ||
} | ||
} |
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