Skip to content

Commit

Permalink
support instrument proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Jan 15, 2025
1 parent 12e289d commit 974c315
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@
package io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_3;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_3.JsonRpcSingletons.CLIENT_INSTRUMENTER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.googlecode.jsonrpc4j.IJsonRpcClient;
import com.googlecode.jsonrpc4j.ReflectionUtil;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcRequest;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcResponse;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand All @@ -47,7 +38,7 @@ public void transform(TypeTransformer transformer) {
this.getClass().getName() + "$CreateClientProxyAdvice");
}

@SuppressWarnings({"unused", "unchecked"})
@SuppressWarnings({"unused"})
public static class CreateClientProxyAdvice {

@Advice.OnMethodExit(suppress = Throwable.class)
Expand All @@ -59,47 +50,8 @@ public static <T> void onExit(
@Advice.Return(readOnly = false) Object proxy) {

proxy =
(T)
Proxy.newProxyInstance(
classLoader,
new Class<?>[] {proxyInterface},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {

Object arguments = ReflectionUtil.parseArguments(method, args);
String methodName = method.getName(); // todo

// before invoke
Context parentContext = Context.current();
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(method, args);
if (!CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
return client.invoke(
methodName, arguments, method.getGenericReturnType(), extraHeaders);
}

Context context = CLIENT_INSTRUMENTER.start(parentContext, request);
Scope scope = context.makeCurrent();
try {
Object result =
client.invoke(
methodName, arguments, method.getGenericReturnType(), extraHeaders);
scope.close();
CLIENT_INSTRUMENTER.end(
context,
new SimpleJsonRpcRequest(method, args),
new SimpleJsonRpcResponse(result),
null);
return result;
} catch (Throwable t) {
// after invoke
scope.close();
CLIENT_INSTRUMENTER.end(context, request, null, t);
throw t;
}
}
});
JsonRpcSingletons.instrumentCreateClientProxy(
classLoader, proxyInterface, client, extraHeaders, proxy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

package io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_3;

import com.googlecode.jsonrpc4j.IJsonRpcClient;
import com.googlecode.jsonrpc4j.InvocationListener;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.JsonRpcTelemetry;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcRequest;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcResponse;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

public final class JsonRpcSingletons {

Expand All @@ -26,4 +33,49 @@ public final class JsonRpcSingletons {
}

private JsonRpcSingletons() {}

@SuppressWarnings({"unchecked"})
public static <T> T instrumentCreateClientProxy(
ClassLoader classLoader,
Class<T> proxyInterface,
IJsonRpcClient client,
Map<String, String> extraHeaders,
Object proxy) {

return (T)
Proxy.newProxyInstance(
classLoader,
new Class<?>[] {proxyInterface},
new InvocationHandler() {
@Override
public Object invoke(Object proxy1, Method method, Object[] args) throws Throwable {
// before invoke
Context parentContext = Context.current();
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(method, args);
if (!CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
return method.invoke(proxy, args);
}

Context context = CLIENT_INSTRUMENTER.start(parentContext, request);
Scope scope = context.makeCurrent();
try {
Object result = method.invoke(proxy, args);
// after invoke
scope.close();
CLIENT_INSTRUMENTER.end(
context,
new SimpleJsonRpcRequest(method, args),
new SimpleJsonRpcResponse(result),
null);
return result;

} catch (Throwable t) {
// after invoke
scope.close();
CLIENT_INSTRUMENTER.end(context, request, null, t);
throw t;
}
}
});
}
}

0 comments on commit 974c315

Please sign in to comment.