Skip to content

Commit ef81cdd

Browse files
committed
jaxrs-2.0-resteasy
1 parent f8dad3c commit ef81cdd

File tree

2 files changed

+69
-44
lines changed

2 files changed

+69
-44
lines changed

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-resteasy-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/Resteasy30RequestContextInstrumentation.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
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 net.bytebuddy.asm.Advice;
16-
import net.bytebuddy.asm.Advice.Local;
1717
import org.jboss.resteasy.core.ResourceMethodInvoker;
1818
import org.jboss.resteasy.core.interception.PostMatchContainerRequestContext;
1919

@@ -36,42 +36,54 @@ protected String abortAdviceName() {
3636
@SuppressWarnings("unused")
3737
public static class ContainerRequestContextAdvice {
3838

39+
public static class AdviceScope {
40+
private final Jaxrs2HandlerData handlerData;
41+
private final Context context;
42+
private final Scope scope;
43+
44+
public AdviceScope(
45+
Class<?> resourceClass, Method method, ContainerRequestContext requestContext) {
46+
handlerData = new Jaxrs2HandlerData(resourceClass, method);
47+
context =
48+
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
49+
instrumenter(), requestContext, handlerData);
50+
scope = context != null ? context.makeCurrent() : null;
51+
}
52+
53+
public void exit(Throwable throwable) {
54+
if (scope == null) {
55+
return;
56+
}
57+
scope.close();
58+
instrumenter().end(context, handlerData, null, throwable);
59+
}
60+
}
61+
62+
@Nullable
3963
@Advice.OnMethodEnter(suppress = Throwable.class)
40-
public static void decorateAbortSpan(
41-
@Advice.This ContainerRequestContext requestContext,
42-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
43-
@Local("otelContext") Context context,
44-
@Local("otelScope") Scope scope) {
64+
public static AdviceScope decorateAbortSpan(
65+
@Advice.This ContainerRequestContext requestContext) {
66+
4567
if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null
4668
|| !(requestContext instanceof PostMatchContainerRequestContext)) {
47-
return;
69+
return null;
4870
}
4971

5072
ResourceMethodInvoker resourceMethodInvoker =
5173
((PostMatchContainerRequestContext) requestContext).getResourceMethod();
5274
Method method = resourceMethodInvoker.getMethod();
5375
Class<?> resourceClass = resourceMethodInvoker.getResourceClass();
5476

55-
handlerData = new Jaxrs2HandlerData(resourceClass, method);
56-
context =
57-
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
58-
instrumenter(), requestContext, handlerData);
59-
if (context != null) {
60-
scope = context.makeCurrent();
61-
}
77+
return new AdviceScope(resourceClass, method, requestContext);
6278
}
6379

6480
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6581
public static void stopSpan(
66-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
67-
@Local("otelContext") Context context,
68-
@Local("otelScope") Scope scope,
69-
@Advice.Thrown Throwable throwable) {
70-
if (scope == null) {
71-
return;
82+
@Advice.Thrown @Nullable Throwable throwable,
83+
@Advice.Enter @Nullable AdviceScope adviceScope) {
84+
if (adviceScope != null) {
85+
adviceScope.exit(throwable);
7286
}
73-
scope.close();
74-
instrumenter().end(context, handlerData, null, throwable);
7587
}
7688
}
7789
}

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-resteasy-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/Resteasy31RequestContextInstrumentation.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
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 net.bytebuddy.asm.Advice;
16-
import net.bytebuddy.asm.Advice.Local;
1717
import org.jboss.resteasy.core.ResourceMethodInvoker;
1818
import org.jboss.resteasy.core.interception.jaxrs.PostMatchContainerRequestContext;
1919

@@ -36,42 +36,55 @@ protected String abortAdviceName() {
3636
@SuppressWarnings("unused")
3737
public static class ContainerRequestContextAdvice {
3838

39+
public static class AdviceScope {
40+
private final Jaxrs2HandlerData handlerData;
41+
private final Context context;
42+
private final Scope scope;
43+
44+
public AdviceScope(
45+
Class<?> resourceClass, Method method, ContainerRequestContext requestContext) {
46+
handlerData = new Jaxrs2HandlerData(resourceClass, method);
47+
context =
48+
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
49+
instrumenter(), requestContext, handlerData);
50+
51+
scope = context != null ? context.makeCurrent() : null;
52+
}
53+
54+
public void exit(Throwable throwable) {
55+
if (scope == null) {
56+
return;
57+
}
58+
scope.close();
59+
instrumenter().end(context, handlerData, null, throwable);
60+
}
61+
}
62+
63+
@Nullable
3964
@Advice.OnMethodEnter(suppress = Throwable.class)
40-
public static void decorateAbortSpan(
41-
@Advice.This ContainerRequestContext requestContext,
42-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
43-
@Local("otelContext") Context context,
44-
@Local("otelScope") Scope scope) {
65+
public static AdviceScope decorateAbortSpan(
66+
@Advice.This ContainerRequestContext requestContext) {
67+
4568
if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null
4669
|| !(requestContext instanceof PostMatchContainerRequestContext)) {
47-
return;
70+
return null;
4871
}
4972

5073
ResourceMethodInvoker resourceMethodInvoker =
5174
((PostMatchContainerRequestContext) requestContext).getResourceMethod();
5275
Method method = resourceMethodInvoker.getMethod();
5376
Class<?> resourceClass = resourceMethodInvoker.getResourceClass();
5477

55-
handlerData = new Jaxrs2HandlerData(resourceClass, method);
56-
context =
57-
Jaxrs2RequestContextHelper.createOrUpdateAbortSpan(
58-
instrumenter(), requestContext, handlerData);
59-
if (context != null) {
60-
scope = context.makeCurrent();
61-
}
78+
return new AdviceScope(resourceClass, method, requestContext);
6279
}
6380

6481
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6582
public static void stopSpan(
66-
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
67-
@Local("otelContext") Context context,
68-
@Local("otelScope") Scope scope,
69-
@Advice.Thrown Throwable throwable) {
70-
if (scope == null) {
71-
return;
83+
@Advice.Thrown @Nullable Throwable throwable,
84+
@Advice.Enter @Nullable AdviceScope adviceScope) {
85+
if (adviceScope != null) {
86+
adviceScope.exit(throwable);
7287
}
73-
scope.close();
74-
instrumenter().end(context, handlerData, null, throwable);
7588
}
7689
}
7790
}

0 commit comments

Comments
 (0)