Skip to content

Commit def3f87

Browse files
committed
jaxrs-1.0
1 parent 09544aa commit def3f87

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxrsAnnotationsInstrumentation.java

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2727
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2828
import java.lang.reflect.Method;
29+
import javax.annotation.Nullable;
2930
import javax.ws.rs.Path;
3031
import net.bytebuddy.asm.Advice;
3132
import net.bytebuddy.description.type.TypeDescription;
@@ -67,52 +68,62 @@ public void transform(TypeTransformer transformer) {
6768
@SuppressWarnings("unused")
6869
public static class JaxRsAnnotationsAdvice {
6970

70-
@Advice.OnMethodEnter(suppress = Throwable.class)
71-
public static void nameSpan(
72-
@Advice.This Object target,
73-
@Advice.Origin Method method,
74-
@Advice.Local("otelCallDepth") CallDepth callDepth,
75-
@Advice.Local("otelHandlerData") HandlerData handlerData,
76-
@Advice.Local("otelContext") Context context,
77-
@Advice.Local("otelScope") Scope scope) {
78-
callDepth = CallDepth.forClass(Path.class);
79-
if (callDepth.getAndIncrement() > 0) {
80-
return;
71+
public static class AdviceScope {
72+
private HandlerData handlerData;
73+
private final CallDepth callDepth;
74+
private Context context;
75+
private Scope scope;
76+
77+
public AdviceScope(CallDepth callDepth) {
78+
this.callDepth = callDepth;
8179
}
8280

83-
Context parentContext = Java8BytecodeBridge.currentContext();
84-
handlerData = new HandlerData(target.getClass(), method);
81+
public AdviceScope enter(Class<?> type, Method method) {
82+
if (callDepth.getAndIncrement() > 0) {
83+
return this;
84+
}
85+
86+
Context parentContext = Java8BytecodeBridge.currentContext();
87+
handlerData = new HandlerData(type, method);
88+
89+
HttpServerRoute.update(
90+
parentContext,
91+
HttpServerRouteSource.CONTROLLER,
92+
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
93+
handlerData);
94+
95+
if (!instrumenter().shouldStart(parentContext, handlerData)) {
96+
return this;
97+
}
98+
99+
context = instrumenter().start(parentContext, handlerData);
100+
scope = context.makeCurrent();
101+
return this;
102+
}
85103

86-
HttpServerRoute.update(
87-
parentContext,
88-
HttpServerRouteSource.CONTROLLER,
89-
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
90-
handlerData);
104+
public void exit(@Nullable Throwable throwable) {
105+
if (callDepth.decrementAndGet() > 0) {
106+
return;
107+
}
91108

92-
if (!instrumenter().shouldStart(parentContext, handlerData)) {
93-
return;
109+
if (scope == null) {
110+
return;
111+
}
112+
scope.close();
113+
instrumenter().end(context, handlerData, null, throwable);
94114
}
115+
}
95116

96-
context = instrumenter().start(parentContext, handlerData);
97-
scope = context.makeCurrent();
117+
@Advice.OnMethodEnter(suppress = Throwable.class)
118+
public static AdviceScope nameSpan(@Advice.This Object target, @Advice.Origin Method method) {
119+
AdviceScope adviceScope = new AdviceScope(CallDepth.forClass(Path.class));
120+
return adviceScope.enter(target.getClass(), method);
98121
}
99122

100123
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
101124
public static void stopSpan(
102-
@Advice.Thrown Throwable throwable,
103-
@Advice.Local("otelCallDepth") CallDepth callDepth,
104-
@Advice.Local("otelHandlerData") HandlerData handlerData,
105-
@Advice.Local("otelContext") Context context,
106-
@Advice.Local("otelScope") Scope scope) {
107-
if (callDepth.decrementAndGet() > 0) {
108-
return;
109-
}
110-
111-
if (scope == null) {
112-
return;
113-
}
114-
scope.close();
115-
instrumenter().end(context, handlerData, null, throwable);
125+
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
126+
adviceScope.exit(throwable);
116127
}
117128
}
118129
}

instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxrsInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
1414
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1515
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
16+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1617
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1718
import java.util.List;
1819
import net.bytebuddy.matcher.ElementMatcher;
1920

2021
@AutoService(InstrumentationModule.class)
21-
public class JaxrsInstrumentationModule extends InstrumentationModule {
22+
public class JaxrsInstrumentationModule extends InstrumentationModule
23+
implements ExperimentalInstrumentationModule {
2224
public JaxrsInstrumentationModule() {
2325
super("jaxrs", "jaxrs-1.0");
2426
}
@@ -41,4 +43,9 @@ public boolean defaultEnabled(ConfigProperties config) {
4143
// This instrumentation uses complex type matcher, disabling it can improve startup performance.
4244
return super.defaultEnabled(config) && ExperimentalConfig.get().controllerTelemetryEnabled();
4345
}
46+
47+
@Override
48+
public boolean isIndyReady() {
49+
return true;
50+
}
4451
}

0 commit comments

Comments
 (0)