Skip to content

Commit 91b80f7

Browse files
committed
rmi
1 parent f146ca2 commit 91b80f7

File tree

3 files changed

+61
-45
lines changed

3 files changed

+61
-45
lines changed

instrumentation/spring/spring-rmi-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/rmi/v4_0/SpringRmiInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import io.opentelemetry.javaagent.instrumentation.spring.rmi.v4_0.client.ClientInstrumentation;
1415
import io.opentelemetry.javaagent.instrumentation.spring.rmi.v4_0.server.ServerInstrumentation;
1516
import java.util.List;
1617

1718
@AutoService(InstrumentationModule.class)
18-
public class SpringRmiInstrumentationModule extends InstrumentationModule {
19+
public class SpringRmiInstrumentationModule extends InstrumentationModule
20+
implements ExperimentalInstrumentationModule {
1921

2022
public SpringRmiInstrumentationModule() {
2123
super("spring-rmi", "spring-rmi-4.0");
@@ -25,4 +27,9 @@ public SpringRmiInstrumentationModule() {
2527
public List<TypeInstrumentation> typeInstrumentations() {
2628
return asList(new ClientInstrumentation(), new ServerInstrumentation());
2729
}
30+
31+
@Override
32+
public boolean isIndyReady() {
33+
return true;
34+
}
2835
}

instrumentation/spring/spring-rmi-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/rmi/v4_0/client/ClientInstrumentation.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1818
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
1919
import java.lang.reflect.Method;
20+
import javax.annotation.Nullable;
2021
import net.bytebuddy.asm.Advice;
2122
import net.bytebuddy.description.type.TypeDescription;
2223
import net.bytebuddy.matcher.ElementMatcher;
@@ -42,35 +43,42 @@ public void transform(TypeTransformer transformer) {
4243
@SuppressWarnings("unused")
4344
public static class InvokeMethodAdvice {
4445

45-
@Advice.OnMethodEnter(suppress = Throwable.class)
46-
public static void onEnter(
47-
@Advice.Argument(0) MethodInvocation methodInv,
48-
@Advice.Local("method") Method method,
49-
@Advice.Local("otelContext") Context context,
50-
@Advice.Local("otelScope") Scope scope) {
46+
public static class AdviceScope {
47+
public Method method;
48+
public Context context;
49+
public Scope scope;
50+
51+
public AdviceScope(Method method, Context context, Scope scope) {
52+
this.method = method;
53+
this.context = context;
54+
this.scope = scope;
55+
}
5156

52-
method = methodInv.getMethod();
57+
public void exit(@Nullable Throwable throwable) {
58+
scope.close();
59+
clientInstrumenter().end(context, method, null, throwable);
60+
}
61+
}
62+
63+
@Nullable
64+
@Advice.OnMethodEnter(suppress = Throwable.class)
65+
public static AdviceScope onEnter(@Advice.Argument(0) MethodInvocation methodInv) {
66+
Method method = methodInv.getMethod();
5367
Context parentContext = Java8BytecodeBridge.currentContext();
5468
if (!clientInstrumenter().shouldStart(parentContext, method)) {
55-
return;
69+
return null;
5670
}
57-
58-
context = clientInstrumenter().start(parentContext, method);
59-
scope = context.makeCurrent();
71+
Context context = clientInstrumenter().start(parentContext, method);
72+
return new AdviceScope(method, context, context.makeCurrent());
6073
}
6174

6275
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6376
public static void stopSpan(
64-
@Advice.Local("method") Method method,
65-
@Advice.Thrown Throwable throwable,
66-
@Advice.Local("otelContext") Context context,
67-
@Advice.Local("otelScope") Scope scope) {
68-
69-
if (scope == null) {
70-
return;
77+
@Advice.Thrown @Nullable Throwable throwable,
78+
@Advice.Enter @Nullable AdviceScope adviceScope) {
79+
if (adviceScope != null) {
80+
adviceScope.exit(throwable);
7181
}
72-
scope.close();
73-
clientInstrumenter().end(context, method, null, throwable);
7482
}
7583
}
7684
}

instrumentation/spring/spring-rmi-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/rmi/v4_0/server/ServerInstrumentation.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,50 @@ public void transform(TypeTransformer transformer) {
4141
@SuppressWarnings("unused")
4242
public static class InvokeMethodAdvice {
4343

44+
public static class AdviceLocals {
45+
public CallDepth callDepth;
46+
public ClassAndMethod request;
47+
public Context context;
48+
public Scope scope;
49+
}
50+
4451
@Advice.OnMethodEnter(suppress = Throwable.class)
45-
public static void onEnter(
46-
@Advice.This RmiBasedExporter thisObject,
47-
@Advice.Argument(0) RemoteInvocation remoteInv,
48-
@Advice.Local("otelCallDepth") CallDepth callDepth,
49-
@Advice.Local("otelRequest") ClassAndMethod request,
50-
@Advice.Local("otelContext") Context context,
51-
@Advice.Local("otelScope") Scope scope) {
52+
public static AdviceLocals onEnter(
53+
@Advice.This RmiBasedExporter thisObject, @Advice.Argument(0) RemoteInvocation remoteInv) {
5254

53-
callDepth = CallDepth.forClass(RmiBasedExporter.class);
54-
if (callDepth.getAndIncrement() > 0) {
55-
return;
55+
AdviceLocals locals = new AdviceLocals();
56+
57+
locals.callDepth = CallDepth.forClass(RmiBasedExporter.class);
58+
if (locals.callDepth.getAndIncrement() > 0) {
59+
return locals;
5660
}
5761

5862
Context parentContext = THREAD_LOCAL_CONTEXT.getAndResetContext();
5963
Class<?> serverClass = thisObject.getService().getClass();
6064
String methodName = remoteInv.getMethodName();
61-
request = ClassAndMethod.create(serverClass, methodName);
65+
locals.request = ClassAndMethod.create(serverClass, methodName);
6266

63-
if (!serverInstrumenter().shouldStart(parentContext, request)) {
64-
return;
67+
if (!serverInstrumenter().shouldStart(parentContext, locals.request)) {
68+
return locals;
6569
}
6670

67-
context = serverInstrumenter().start(parentContext, request);
68-
scope = context.makeCurrent();
71+
locals.context = serverInstrumenter().start(parentContext, locals.request);
72+
locals.scope = locals.context.makeCurrent();
73+
return locals;
6974
}
7075

7176
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7277
public static void stopSpan(
73-
@Advice.Thrown Throwable throwable,
74-
@Advice.Local("otelCallDepth") CallDepth callDepth,
75-
@Advice.Local("otelRequest") ClassAndMethod request,
76-
@Advice.Local("otelContext") Context context,
77-
@Advice.Local("otelScope") Scope scope) {
78+
@Advice.Thrown Throwable throwable, @Advice.Enter AdviceLocals locals) {
7879

79-
if (callDepth.decrementAndGet() > 0) {
80+
if (locals.callDepth.decrementAndGet() > 0) {
8081
return;
8182
}
82-
if (scope == null) {
83+
if (locals.scope == null) {
8384
return;
8485
}
85-
scope.close();
86-
serverInstrumenter().end(context, request, null, throwable);
86+
locals.scope.close();
87+
serverInstrumenter().end(locals.context, locals.request, null, throwable);
8788
}
8889
}
8990
}

0 commit comments

Comments
 (0)