Skip to content

Commit f259187

Browse files
authored
make jaxrs indy-ready (#14168)
1 parent 4bd47bf commit f259187

File tree

38 files changed

+734
-529
lines changed

38 files changed

+734
-529
lines changed

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

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
2323
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource;
2424
import io.opentelemetry.javaagent.bootstrap.CallDepth;
25-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2625
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2726
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2827
import java.lang.reflect.Method;
28+
import javax.annotation.Nullable;
2929
import javax.ws.rs.Path;
3030
import net.bytebuddy.asm.Advice;
3131
import net.bytebuddy.description.type.TypeDescription;
@@ -67,52 +67,58 @@ public void transform(TypeTransformer transformer) {
6767
@SuppressWarnings("unused")
6868
public static class JaxRsAnnotationsAdvice {
6969

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;
81-
}
70+
public static class AdviceScope {
71+
private final HandlerData handlerData;
72+
private final CallDepth callDepth;
73+
private final Context context;
74+
private final Scope scope;
75+
76+
public AdviceScope(CallDepth callDepth, Class<?> type, Method method) {
77+
this.callDepth = callDepth;
78+
if (callDepth.getAndIncrement() > 0) {
79+
handlerData = null;
80+
context = null;
81+
scope = null;
82+
return;
83+
}
8284

83-
Context parentContext = Java8BytecodeBridge.currentContext();
84-
handlerData = new HandlerData(target.getClass(), method);
85+
Context parentContext = Context.current();
86+
handlerData = new HandlerData(type, method);
8587

86-
HttpServerRoute.update(
87-
parentContext,
88-
HttpServerRouteSource.CONTROLLER,
89-
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
90-
handlerData);
88+
HttpServerRoute.update(
89+
parentContext,
90+
HttpServerRouteSource.CONTROLLER,
91+
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
92+
handlerData);
9193

92-
if (!instrumenter().shouldStart(parentContext, handlerData)) {
93-
return;
94+
if (!instrumenter().shouldStart(parentContext, handlerData)) {
95+
scope = null;
96+
context = null;
97+
return;
98+
}
99+
100+
context = instrumenter().start(parentContext, handlerData);
101+
scope = context.makeCurrent();
94102
}
95103

96-
context = instrumenter().start(parentContext, handlerData);
97-
scope = context.makeCurrent();
104+
public void exit(@Nullable Throwable throwable) {
105+
if (callDepth.decrementAndGet() > 0 || scope == null) {
106+
return;
107+
}
108+
scope.close();
109+
instrumenter().end(context, handlerData, null, throwable);
110+
}
111+
}
112+
113+
@Advice.OnMethodEnter(suppress = Throwable.class)
114+
public static AdviceScope nameSpan(@Advice.This Object target, @Advice.Origin Method method) {
115+
return new AdviceScope(CallDepth.forClass(Path.class), target.getClass(), method);
98116
}
99117

100118
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
101119
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);
120+
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
121+
adviceScope.exit(throwable);
116122
}
117123
}
118124
}

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
}

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/DefaultRequestContextInstrumentation.java

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import io.opentelemetry.context.Scope;
1212
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
1313
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource;
14-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1514
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
1615
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsServerSpanNaming;
1716
import java.lang.reflect.Method;
17+
import javax.annotation.Nullable;
1818
import javax.ws.rs.container.ContainerRequestContext;
1919
import net.bytebuddy.asm.Advice;
20-
import net.bytebuddy.asm.Advice.Local;
2120

2221
/**
2322
* Default context instrumentation.
@@ -37,18 +36,60 @@ protected String abortAdviceName() {
3736
@SuppressWarnings("unused")
3837
public static class ContainerRequestContextAdvice {
3938

39+
public static class AdviceScope {
40+
private final Context context;
41+
private final Scope scope;
42+
private final Jaxrs2HandlerData handlerData;
43+
44+
private AdviceScope(Context context, Scope scope, Jaxrs2HandlerData handlerData) {
45+
this.context = context;
46+
this.scope = scope;
47+
this.handlerData = handlerData;
48+
}
49+
50+
@Nullable
51+
public static AdviceScope enter(Class<?> filterClass, Method method) {
52+
53+
Context parentContext = Context.current();
54+
Jaxrs2HandlerData handlerData = new Jaxrs2HandlerData(filterClass, method);
55+
56+
HttpServerRoute.update(
57+
parentContext,
58+
HttpServerRouteSource.CONTROLLER,
59+
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
60+
handlerData);
61+
62+
if (!instrumenter().shouldStart(parentContext, handlerData)) {
63+
return null;
64+
}
65+
Context context = instrumenter().start(parentContext, handlerData);
66+
return new AdviceScope(context, context.makeCurrent(), handlerData);
67+
}
68+
69+
public void exit(Throwable throwable) {
70+
if (scope == null) {
71+
return;
72+
}
73+
scope.close();
74+
instrumenter().end(context, handlerData, null, throwable);
75+
}
76+
}
77+
78+
@Nullable
4079
@Advice.OnMethodEnter(suppress = Throwable.class)
41-
public static void createGenericSpan(
42-
@Advice.This ContainerRequestContext requestContext,
43-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
44-
@Local("otelContext") Context context,
45-
@Local("otelScope") Scope scope) {
80+
public static AdviceScope createGenericSpan(
81+
@Advice.This ContainerRequestContext requestContext) {
82+
4683
if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null) {
47-
return;
84+
return null;
4885
}
4986

5087
Class<?> filterClass =
5188
(Class<?>) requestContext.getProperty(JaxrsConstants.ABORT_FILTER_CLASS);
89+
if (filterClass == null) {
90+
return null;
91+
}
92+
5293
Method method = null;
5394
try {
5495
method = filterClass.getMethod("filter", ContainerRequestContext.class);
@@ -57,39 +98,20 @@ public static void createGenericSpan(
5798
// can only be aborted inside the filter method
5899
}
59100

60-
if (filterClass == null || method == null) {
61-
return;
62-
}
63-
64-
Context parentContext = Java8BytecodeBridge.currentContext();
65-
handlerData = new Jaxrs2HandlerData(filterClass, method);
66-
67-
HttpServerRoute.update(
68-
parentContext,
69-
HttpServerRouteSource.CONTROLLER,
70-
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
71-
handlerData);
72-
73-
if (!instrumenter().shouldStart(parentContext, handlerData)) {
74-
return;
101+
if (method == null) {
102+
return null;
75103
}
76-
77-
context = instrumenter().start(parentContext, handlerData);
78-
scope = context.makeCurrent();
104+
return AdviceScope.enter(filterClass, method);
79105
}
80106

81107
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
82108
public static void stopSpan(
83-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
84-
@Local("otelContext") Context context,
85-
@Local("otelScope") Scope scope,
86-
@Advice.Thrown Throwable throwable) {
87-
if (scope == null) {
88-
return;
89-
}
109+
@Advice.Thrown @Nullable Throwable throwable,
110+
@Advice.Enter @Nullable AdviceScope adviceScope) {
90111

91-
scope.close();
92-
instrumenter().end(context, handlerData, null, throwable);
112+
if (adviceScope != null) {
113+
adviceScope.exit(throwable);
114+
}
93115
}
94116
}
95117
}

0 commit comments

Comments
 (0)