Skip to content

Commit 861450e

Browse files
committed
support instrument proxy client
1 parent 12e289d commit 861450e

File tree

2 files changed

+59
-52
lines changed

2 files changed

+59
-52
lines changed

Diff for: instrumentation/jsonrpc4j-1.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsonrpc4j/v1_3/JsonRpcProxyInstrumentation.java

+3-52
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@
66
package io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_3;
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
9-
import static io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_3.JsonRpcSingletons.CLIENT_INSTRUMENTER;
109
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1110
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
1211
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
1312
import static net.bytebuddy.matcher.ElementMatchers.named;
1413

1514
import com.googlecode.jsonrpc4j.IJsonRpcClient;
16-
import com.googlecode.jsonrpc4j.ReflectionUtil;
17-
import io.opentelemetry.context.Context;
18-
import io.opentelemetry.context.Scope;
19-
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcRequest;
20-
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcResponse;
2115
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2216
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
23-
import java.lang.reflect.InvocationHandler;
24-
import java.lang.reflect.Method;
25-
import java.lang.reflect.Proxy;
2617
import java.util.Map;
2718
import net.bytebuddy.asm.Advice;
2819
import net.bytebuddy.description.type.TypeDescription;
@@ -47,7 +38,7 @@ public void transform(TypeTransformer transformer) {
4738
this.getClass().getName() + "$CreateClientProxyAdvice");
4839
}
4940

50-
@SuppressWarnings({"unused", "unchecked"})
41+
@SuppressWarnings({"unused"})
5142
public static class CreateClientProxyAdvice {
5243

5344
@Advice.OnMethodExit(suppress = Throwable.class)
@@ -57,49 +48,9 @@ public static <T> void onExit(
5748
@Advice.Argument(2) IJsonRpcClient client,
5849
@Advice.Argument(3) Map<String, String> extraHeaders,
5950
@Advice.Return(readOnly = false) Object proxy) {
60-
6151
proxy =
62-
(T)
63-
Proxy.newProxyInstance(
64-
classLoader,
65-
new Class<?>[] {proxyInterface},
66-
new InvocationHandler() {
67-
@Override
68-
public Object invoke(Object proxy, Method method, Object[] args)
69-
throws Throwable {
70-
71-
Object arguments = ReflectionUtil.parseArguments(method, args);
72-
String methodName = method.getName(); // todo
73-
74-
// before invoke
75-
Context parentContext = Context.current();
76-
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(method, args);
77-
if (!CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
78-
return client.invoke(
79-
methodName, arguments, method.getGenericReturnType(), extraHeaders);
80-
}
81-
82-
Context context = CLIENT_INSTRUMENTER.start(parentContext, request);
83-
Scope scope = context.makeCurrent();
84-
try {
85-
Object result =
86-
client.invoke(
87-
methodName, arguments, method.getGenericReturnType(), extraHeaders);
88-
scope.close();
89-
CLIENT_INSTRUMENTER.end(
90-
context,
91-
new SimpleJsonRpcRequest(method, args),
92-
new SimpleJsonRpcResponse(result),
93-
null);
94-
return result;
95-
} catch (Throwable t) {
96-
// after invoke
97-
scope.close();
98-
CLIENT_INSTRUMENTER.end(context, request, null, t);
99-
throw t;
100-
}
101-
}
102-
});
52+
JsonRpcSingletons.instrumentCreateClientProxy(
53+
classLoader, proxyInterface, client, extraHeaders);
10354
}
10455
}
10556
}

Diff for: instrumentation/jsonrpc4j-1.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsonrpc4j/v1_3/JsonRpcSingletons.java

+56
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55

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

8+
import com.googlecode.jsonrpc4j.IJsonRpcClient;
89
import com.googlecode.jsonrpc4j.InvocationListener;
10+
import com.googlecode.jsonrpc4j.ReflectionUtil;
911
import io.opentelemetry.api.GlobalOpenTelemetry;
12+
import io.opentelemetry.context.Context;
13+
import io.opentelemetry.context.Scope;
1014
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1115
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.JsonRpcTelemetry;
1216
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcRequest;
1317
import io.opentelemetry.instrumentation.jsonrpc4j.v1_3.SimpleJsonRpcResponse;
18+
import java.lang.reflect.InvocationHandler;
19+
import java.lang.reflect.Method;
20+
import java.lang.reflect.Proxy;
21+
import java.util.Map;
1422

1523
public final class JsonRpcSingletons {
1624

@@ -26,4 +34,52 @@ public final class JsonRpcSingletons {
2634
}
2735

2836
private JsonRpcSingletons() {}
37+
38+
@SuppressWarnings({"unchecked"})
39+
public static <T> T instrumentCreateClientProxy(
40+
ClassLoader classLoader,
41+
Class<T> proxyInterface,
42+
IJsonRpcClient client,
43+
Map<String, String> extraHeaders) {
44+
return (T)
45+
Proxy.newProxyInstance(
46+
classLoader,
47+
new Class<?>[] {proxyInterface},
48+
new InvocationHandler() {
49+
@Override
50+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
51+
52+
Object arguments = ReflectionUtil.parseArguments(method, args);
53+
String methodName = method.getName(); // todo
54+
55+
// before invoke
56+
Context parentContext = Context.current();
57+
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(method, args);
58+
if (!CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
59+
return client.invoke(
60+
methodName, arguments, method.getGenericReturnType(), extraHeaders);
61+
}
62+
63+
Context context = CLIENT_INSTRUMENTER.start(parentContext, request);
64+
Scope scope = context.makeCurrent();
65+
try {
66+
Object result =
67+
client.invoke(
68+
methodName, arguments, method.getGenericReturnType(), extraHeaders);
69+
scope.close();
70+
CLIENT_INSTRUMENTER.end(
71+
context,
72+
new SimpleJsonRpcRequest(method, args),
73+
new SimpleJsonRpcResponse(result),
74+
null);
75+
return result;
76+
} catch (Throwable t) {
77+
// after invoke
78+
scope.close();
79+
CLIENT_INSTRUMENTER.end(context, request, null, t);
80+
throw t;
81+
}
82+
}
83+
});
84+
}
2985
}

0 commit comments

Comments
 (0)