Skip to content

Commit c5dfc76

Browse files
committed
jaxrs-2.0-jersey
1 parent 4e552d7 commit c5dfc76

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-jersey-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JerseyInstrumentationModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
4040
new JerseyServletContainerInstrumentation(),
4141
new JerseyResourceMethodDispatcherInstrumentation());
4242
}
43+
44+
@Override
45+
public boolean isIndyReady() {
46+
return true;
47+
}
4348
}

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-jersey-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JerseyRequestContextInstrumentation.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import io.opentelemetry.context.Scope;
1212
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
1313
import java.lang.reflect.Method;
14+
import javax.annotation.Nullable;
1415
import javax.ws.rs.container.ContainerRequestContext;
1516
import javax.ws.rs.container.ResourceInfo;
1617
import javax.ws.rs.core.UriInfo;
1718
import net.bytebuddy.asm.Advice;
18-
import net.bytebuddy.asm.Advice.Local;
1919

2020
/**
2121
* Jersey specific context instrumentation.
@@ -35,47 +35,62 @@ protected String abortAdviceName() {
3535
@SuppressWarnings("unused")
3636
public static class ContainerRequestContextAdvice {
3737

38+
public static class AdviceScope {
39+
private Jaxrs2HandlerData handlerData;
40+
private Context context;
41+
private Scope scope;
42+
43+
public AdviceScope enter(
44+
Class<?> resourceClass, Method method, ContainerRequestContext requestContext) {
45+
handlerData = new Jaxrs2HandlerData(resourceClass, method);
46+
context =
47+
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
48+
instrumenter(), requestContext, handlerData);
49+
if (context != null) {
50+
scope = context.makeCurrent();
51+
}
52+
return this;
53+
}
54+
55+
public void exit(@Nullable Throwable throwable) {
56+
if (scope == null) {
57+
return;
58+
}
59+
scope.close();
60+
instrumenter().end(context, handlerData, null, throwable);
61+
}
62+
}
63+
64+
@Nullable
3865
@Advice.OnMethodEnter(suppress = Throwable.class)
39-
public static void decorateAbortSpan(
40-
@Advice.This ContainerRequestContext requestContext,
41-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
42-
@Local("otelContext") Context context,
43-
@Local("otelScope") Scope scope) {
66+
public static AdviceScope decorateAbortSpan(
67+
@Advice.This ContainerRequestContext requestContext) {
68+
4469
UriInfo uriInfo = requestContext.getUriInfo();
4570

4671
if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null
4772
|| !(uriInfo instanceof ResourceInfo)) {
48-
return;
73+
return null;
4974
}
5075

5176
ResourceInfo resourceInfo = (ResourceInfo) uriInfo;
5277
Method method = resourceInfo.getResourceMethod();
5378
Class<?> resourceClass = resourceInfo.getResourceClass();
5479

5580
if (resourceClass == null || method == null) {
56-
return;
81+
return null;
5782
}
5883

59-
handlerData = new Jaxrs2HandlerData(resourceClass, method);
60-
context =
61-
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
62-
instrumenter(), requestContext, handlerData);
63-
if (context != null) {
64-
scope = context.makeCurrent();
65-
}
84+
AdviceScope adviceScope = new AdviceScope();
85+
return adviceScope.enter(resourceClass, method, requestContext);
6686
}
6787

6888
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6989
public static void stopSpan(
70-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
71-
@Local("otelContext") Context context,
72-
@Local("otelScope") Scope scope,
73-
@Advice.Thrown Throwable throwable) {
74-
if (scope == null) {
75-
return;
90+
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
91+
if (adviceScope != null) {
92+
adviceScope.exit(throwable);
7693
}
77-
scope.close();
78-
instrumenter().end(context, handlerData, null, throwable);
7994
}
8095
}
8196
}

0 commit comments

Comments
 (0)