Skip to content

Commit a5e1a43

Browse files
committed
resolve some comments
1 parent 466971e commit a5e1a43

File tree

11 files changed

+42
-110
lines changed

11 files changed

+42
-110
lines changed

instrumentation/jsonrpc4j-1.6/javaagent/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val jsonrpcVersion = "1.6"
1515

1616
dependencies {
1717
implementation(project(":instrumentation:jsonrpc4j-1.6:library"))
18-
implementation("com.github.briandilley.jsonrpc4j:jsonrpc4j:$jsonrpcVersion")
18+
library("com.github.briandilley.jsonrpc4j:jsonrpc4j:$jsonrpcVersion")
1919
testImplementation(project(":instrumentation:jsonrpc4j-1.6:testing"))
2020
}
2121

Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
10+
import static io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_6.JsonRpcSingletons.CLIENT_INSTRUMENTER;
1011
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1112
import static net.bytebuddy.matcher.ElementMatchers.named;
1213
import static net.bytebuddy.matcher.ElementMatchers.returns;
@@ -15,7 +16,6 @@
1516

1617
import io.opentelemetry.context.Context;
1718
import io.opentelemetry.context.Scope;
18-
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.HeadersSetter;
1919
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcRequest;
2020
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcResponse;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
@@ -25,7 +25,7 @@
2525
import net.bytebuddy.description.type.TypeDescription;
2626
import net.bytebuddy.matcher.ElementMatcher;
2727

28-
public class JsonRpcClientBuilderInstrumentation implements TypeInstrumentation {
28+
public class JsonRpcClientInstrumentation implements TypeInstrumentation {
2929

3030
@Override
3131
public ElementMatcher<ClassLoader> classLoaderOptimization() {
@@ -64,15 +64,11 @@ public static void onEnter(
6464
@Advice.Local("otelScope") Scope scope) {
6565
Context parentContext = Context.current();
6666
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(methodName, argument);
67-
if (!JsonRpcSingletons.CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
67+
if (!CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) {
6868
return;
6969
}
7070

71-
context = JsonRpcSingletons.CLIENT_INSTRUMENTER.start(parentContext, request);
72-
JsonRpcSingletons.PROPAGATORS
73-
.getTextMapPropagator()
74-
.inject(context, extraHeaders, HeadersSetter.INSTANCE);
75-
71+
context = CLIENT_INSTRUMENTER.start(parentContext, request);
7672
scope = context.makeCurrent();
7773
}
7874

@@ -90,7 +86,7 @@ public static void onExit(
9086
}
9187

9288
scope.close();
93-
JsonRpcSingletons.CLIENT_INSTRUMENTER.end(
89+
CLIENT_INSTRUMENTER.end(
9490
context,
9591
new SimpleJsonRpcRequest(methodName, argument),
9692
new SimpleJsonRpcResponse(result),

instrumentation/jsonrpc4j-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsonrpc4j/v1_6/JsonRpcInstrumentationModule.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public JsonRpcInstrumentationModule() {
2020

2121
@Override
2222
public List<TypeInstrumentation> typeInstrumentations() {
23-
return asList(
24-
new JsonRpcServerBuilderInstrumentation(),
25-
new JsonServiceExporterBuilderInstrumentation(),
26-
new JsonRpcClientBuilderInstrumentation());
23+
return asList(new JsonRpcServerInstrumentation(), new JsonRpcClientInstrumentation());
2724
}
2825
}
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
99
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
10+
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1011
import static net.bytebuddy.matcher.ElementMatchers.named;
1112

1213
import com.googlecode.jsonrpc4j.InvocationListener;
1314
import com.googlecode.jsonrpc4j.JsonRpcBasicServer;
15+
import com.googlecode.jsonrpc4j.MultipleInvocationListener;
1416
import io.opentelemetry.instrumentation.api.util.VirtualField;
1517
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1618
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1719
import net.bytebuddy.asm.Advice;
1820
import net.bytebuddy.description.type.TypeDescription;
21+
import net.bytebuddy.implementation.bytecode.assign.Assigner;
1922
import net.bytebuddy.matcher.ElementMatcher;
2023

21-
public class JsonRpcServerBuilderInstrumentation implements TypeInstrumentation {
24+
public class JsonRpcServerInstrumentation implements TypeInstrumentation {
2225

2326
@Override
2427
public ElementMatcher<ClassLoader> classLoaderOptimization() {
@@ -34,6 +37,10 @@ public ElementMatcher<TypeDescription> typeMatcher() {
3437
public void transform(TypeTransformer transformer) {
3538
transformer.applyAdviceToMethod(
3639
isConstructor(), this.getClass().getName() + "$ConstructorAdvice");
40+
41+
transformer.applyAdviceToMethod(
42+
isMethod().and(named("setInvocationListener")),
43+
this.getClass().getName() + "$SetInvocationListenerAdvice");
3744
}
3845

3946
@SuppressWarnings("unused")
@@ -42,11 +49,34 @@ public static class ConstructorAdvice {
4249
@Advice.OnMethodExit(suppress = Throwable.class)
4350
public static void setInvocationListener(
4451
@Advice.This JsonRpcBasicServer jsonRpcServer,
45-
@Advice.FieldValue("invocationListener") InvocationListener invocationListener) {
52+
@Advice.FieldValue(value = "invocationListener", readOnly = false)
53+
InvocationListener invocationListener) {
54+
invocationListener = JsonRpcSingletons.SERVER_INVOCATION_LISTENER;
55+
}
56+
}
57+
58+
@SuppressWarnings("unused")
59+
public static class SetInvocationListenerAdvice {
60+
61+
@Advice.OnMethodEnter(suppress = Throwable.class)
62+
public static void setInvocationListener(
63+
@Advice.This JsonRpcBasicServer jsonRpcServer,
64+
@Advice.Argument(value = 0, readOnly = false, typing = Assigner.Typing.DYNAMIC)
65+
InvocationListener invocationListener) {
4666
VirtualField<JsonRpcBasicServer, Boolean> instrumented =
4767
VirtualField.find(JsonRpcBasicServer.class, Boolean.class);
4868
if (!Boolean.TRUE.equals(instrumented.get(jsonRpcServer))) {
49-
jsonRpcServer.setInvocationListener(JsonRpcSingletons.SERVER_INVOCATION_LISTENER);
69+
if (invocationListener == null) {
70+
invocationListener = JsonRpcSingletons.SERVER_INVOCATION_LISTENER;
71+
} else if (invocationListener instanceof MultipleInvocationListener) {
72+
((MultipleInvocationListener) invocationListener)
73+
.addInvocationListener(JsonRpcSingletons.SERVER_INVOCATION_LISTENER);
74+
} else {
75+
invocationListener =
76+
new MultipleInvocationListener(
77+
invocationListener, JsonRpcSingletons.SERVER_INVOCATION_LISTENER);
78+
}
79+
5080
instrumented.set(jsonRpcServer, true);
5181
}
5282
}

instrumentation/jsonrpc4j-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsonrpc4j/v1_6/JsonRpcSingletons.java

-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import com.googlecode.jsonrpc4j.InvocationListener;
99
import io.opentelemetry.api.GlobalOpenTelemetry;
10-
import io.opentelemetry.context.propagation.ContextPropagators;
1110
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1211
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.JsonRpcTelemetry;
1312
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcRequest;
@@ -19,14 +18,11 @@ public final class JsonRpcSingletons {
1918

2019
public static final Instrumenter<SimpleJsonRpcRequest, SimpleJsonRpcResponse> CLIENT_INSTRUMENTER;
2120

22-
public static final ContextPropagators PROPAGATORS;
23-
2421
static {
2522
JsonRpcTelemetry telemetry = JsonRpcTelemetry.builder(GlobalOpenTelemetry.get()).build();
2623

2724
SERVER_INVOCATION_LISTENER = telemetry.newServerInvocationListener();
2825
CLIENT_INSTRUMENTER = telemetry.getClientInstrumenter();
29-
PROPAGATORS = telemetry.getPropagators();
3026
}
3127

3228
private JsonRpcSingletons() {}

instrumentation/jsonrpc4j-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsonrpc4j/v1_6/JsonServiceExporterBuilderInstrumentation.java

-53
This file was deleted.

instrumentation/jsonrpc4j-1.6/library/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ val jsonrpcVersion = "1.6"
66
val jacksonVersion = "2.13.3"
77

88
dependencies {
9-
implementation("com.github.briandilley.jsonrpc4j:jsonrpc4j:$jsonrpcVersion")
9+
library("com.github.briandilley.jsonrpc4j:jsonrpc4j:$jsonrpcVersion")
1010

1111
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
1212

instrumentation/jsonrpc4j-1.6/library/src/main/java/io/opentelemetry/instrumentation/jsonrpc4j/v1_6/HeadersSetter.java

-19
This file was deleted.

instrumentation/jsonrpc4j-1.6/library/src/main/java/io/opentelemetry/instrumentation/jsonrpc4j/v1_6/JsonRpcClientAttributesExtractor.java

-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
final class JsonRpcClientAttributesExtractor
1515
implements AttributesExtractor<SimpleJsonRpcRequest, SimpleJsonRpcResponse> {
1616

17-
// private final JsonRpcClientAttributesGetter getter;
18-
//
19-
//
20-
// JsonRpcClientAttributesExtractor(JsonRpcClientAttributesGetter getter) {
21-
// this.getter = getter;
22-
// }
23-
2417
@Override
2518
public void onStart(
2619
AttributesBuilder attributes, Context parentContext, SimpleJsonRpcRequest jsonRpcRequest) {

instrumentation/jsonrpc4j-1.6/library/src/main/java/io/opentelemetry/instrumentation/jsonrpc4j/v1_6/JsonRpcServerAttributesExtractor.java

-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ final class JsonRpcServerAttributesExtractor
2525
private static final AttributeKey<String> RPC_JSONRPC_ERROR_MESSAGE =
2626
AttributeKey.stringKey("rpc.jsonrpc.error_message");
2727

28-
// private final JsonRpcServerAttributesGetter getter;
29-
//
30-
//
31-
// JsonRpcServerAttributesExtractor(JsonRpcServerAttributesGetter getter) {
32-
// this.getter = getter;
33-
// }
34-
3528
@Override
3629
public void onStart(
3730
AttributesBuilder attributes, Context parentContext, JsonRpcRequest jsonRpcRequest) {

instrumentation/jsonrpc4j-1.6/library/src/main/java/io/opentelemetry/instrumentation/jsonrpc4j/v1_6/JsonRpcServerAttributesGetter.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.instrumentation.jsonrpc4j.v1_6;
77

8-
import com.googlecode.jsonrpc4j.JsonRpcService;
98
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcAttributesGetter;
109

1110
// Check
@@ -21,7 +20,7 @@ public String getSystem(JsonRpcRequest request) {
2120

2221
@Override
2322
public String getService(JsonRpcRequest request) {
24-
return request.getMethod().getDeclaringClass().getAnnotation(JsonRpcService.class).value();
23+
return request.getMethod().getDeclaringClass().getName();
2524
}
2625

2726
@Override

0 commit comments

Comments
 (0)