Why my interceptor never get executed ? #1836
Answered
by
raphw
OldFarmer86
asked this question in
Q&A
-
Hi, I am using bytebuddy 1.11.16 with OpneJDK 8 I want to intercept instrumentation: AgentBuilder.Transformer methodsTransformer = new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
return builder.method(isPublic().and(not(isAbstract())).and(not(isDefaultMethod()))).intercept(MethodDelegation.to(SocketInterceptor.class));
}
};
AgentBuilder.Listener listener = new AgentBuilder.Listener.WithErrorsOnly(
new AgentBuilder.Listener.StreamWriting(new PrintStream(new LoggingOutputStream(log, LoggingOutputStream.LogLevel.ERROR))));
// ElementMatcher.Junction<NamedElement> startMatcher = any();
if (recorder == null) {
recorder = "";
}
if (recorder.equals("default")) {
log.info("using default in-memory recorder.");
SocketInterceptor.recorder = new DefaultRecorder();
} else {
log.info("using default in-memory recorder.");
SocketInterceptor.recorder = new DefaultRecorder();
}
new AgentBuilder.Default().type(is(java.net.Socket.class))
.transform(methodsTransformer).with(listener).installOn(inst); interceptor: @RuntimeType
public static Object interceptor(@Origin Class<?> clazz, @Origin Method method, @SuperCall Callable<?> callable, @net.bytebuddy.implementation.bind.annotation.This Object inst, @AllArguments Object[] args) throws Exception {
System.out.println("--------------------------------------->>> intercepting socket methods");
// this is equal to original method, will not cause inner calls to other
// matching methods get intercepted.
Object o = callable.call();
log.debug("intercepting {}#{}", clazz.getName(), method.getName());
int hashCode = System.identityHashCode(inst);
if (java.net.Socket.class.isAssignableFrom(clazz)) {
// ConnectionInfo ci = recorder.getConnectionInfo(hashCode);
// if (ci == null) {
// log.trace("connection@{} is not in records, while intercepting {}#{}", hashCode, clazz.getName(), method.getName());
// }
if (method.getName().equals("connect")) {
ConnectionInfo ci = new ConnectionInfo();
ci.setHash(hashCode);
ci.setCreationTime(System.currentTimeMillis());
ci.setInstance((Socket) inst);
String stackTrace = StringUtils.getCurrentThreadStackTrace();
String shash = StringUtils.md5(stackTrace);
ci.setStackHash(shash);
log.info("connection@{} of type {} acquired by stack@{}", hashCode, method.getDeclaringClass().getName(), shash);
log.debug(stackTrace);
SocketMetrics.ACTIVE_CON.labels(SocketTracer.getConfig("app")).inc();
SocketMetrics.TOTAL_CON.labels(SocketTracer.getConfig("app")).inc();
Timer timer = SocketMetrics.CON_DURATION_TIME.labels(SocketTracer.getConfig("app"), Integer.toString(hashCode), shash).startTimer();
conTimers.put(hashCode, timer);
recorder.connectionCreated(ci, stackTrace);
} else if (method.getName().equals("close")) {
log.info("connection@{} released", hashCode);
SocketMetrics.ACTIVE_CON.labels(SocketTracer.getConfig("app")).dec();
SocketMetrics.TOTAL_CON_CLOSED.labels(SocketTracer.getConfig("app")).inc();
conTimers.get(hashCode).observeDuration();
conTimers.remove(hashCode);
recorder.connectionClosed(hashCode);
}
} else if (java.nio.channels.SocketChannel.class.isAssignableFrom(clazz)) {
}
return o;
} any hint on this ? |
Beta Was this translation helpful? Give feedback.
Answered by
raphw
Jul 9, 2025
Replies: 1 comment
-
Multiple problems:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
OldFarmer86
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Multiple problems:
RetransformationStrategy
for this.ignore
matcher. By default Byte Buddy ignores all JVM classes.Advice
inlines code what does not imply this problem. (You will likely need to inject your helper classes into the boot loader.)