Created
December 29, 2016 02:59
-
-
Save wu-sheng/c86519a134025e8e03531947e38cc0c2 to your computer and use it in GitHub Desktop.
byte-buddy agentBuilder.transform
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
/** | |
* https://github.com/stagemonitor/stagemonitor/blob/80793ac6c582944c9078b420138dda4d23517a69/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java#L149-L166 | |
*/ | |
private static ClassFileTransformer initByteBuddyClassFileTransformer(AutoEvictingCachingBinaryLocator binaryLocator) { | |
AgentBuilder agentBuilder = createAgentBuilder(binaryLocator); | |
for (StagemonitorByteBuddyTransformer transformer : getStagemonitorByteBuddyTransformers()) { | |
agentBuilder = agentBuilder | |
.type(transformer.getMatcher()) | |
.transform(transformer.getTransformer()) | |
.asDecorator(); | |
} | |
final long start = System.currentTimeMillis(); | |
try { | |
return agentBuilder.installOn(instrumentation); | |
} finally { | |
if (corePlugin.isDebugInstrumentation()) { | |
logger.info("Installed agent in {} ms", System.currentTimeMillis() - start); | |
} | |
} | |
} | |
private static Iterable<StagemonitorByteBuddyTransformer> getStagemonitorByteBuddyTransformers() { | |
List<StagemonitorByteBuddyTransformer> transformers = new ArrayList<StagemonitorByteBuddyTransformer>(); | |
// ref source: | |
// https://github.com/stagemonitor/stagemonitor/blob/c1c76e57825d60b179ce1157c28ecac9369ee51d/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/StagemonitorByteBuddyTransformer.java | |
for (StagemonitorByteBuddyTransformer transformer : ServiceLoader.load(StagemonitorByteBuddyTransformer.class, Stagemonitor.class.getClassLoader())) { | |
if (transformer.isActive() && !isExcluded(transformer)) { | |
transformers.add(transformer); | |
if (corePlugin.isDebugInstrumentation()) { | |
logger.info("Registering {}", transformer.getClass().getSimpleName()); | |
} | |
} else if (corePlugin.isDebugInstrumentation()) { | |
logger.info("Excluding {}", transformer.getClass().getSimpleName()); | |
} | |
} | |
Collections.sort(transformers, new Comparator<StagemonitorByteBuddyTransformer>() { | |
@Override | |
public int compare(StagemonitorByteBuddyTransformer o1, StagemonitorByteBuddyTransformer o2) { | |
return o1.getOrder() > o2.getOrder() ? -1 : 1; | |
} | |
}); | |
return transformers; | |
} | |
/** | |
* https://github.com/stagemonitor/stagemonitor/blob/c1c76e57825d60b179ce1157c28ecac9369ee51d/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/StagemonitorByteBuddyTransformer.java#L99-L113 | |
*/ | |
public AgentBuilder.Transformer getTransformer() { | |
return new AgentBuilder.Transformer() { | |
private AsmVisitorWrapper.ForDeclaredMethods advice = registerDynamicValues() | |
.to(getAdviceClass()) | |
.on(timed("method", transformerName, getMethodElementMatcher())); | |
@Override | |
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader) { | |
beforeTransformation(typeDescription, classLoader); | |
return builder.visit(advice); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment