1111import io .opentelemetry .context .Scope ;
1212import io .opentelemetry .javaagent .instrumentation .jaxrs .JaxrsConstants ;
1313import java .lang .reflect .Method ;
14+ import javax .annotation .Nullable ;
1415import javax .ws .rs .container .ContainerRequestContext ;
1516import net .bytebuddy .asm .Advice ;
16- import net .bytebuddy .asm .Advice .Local ;
1717import org .jboss .resteasy .core .ResourceMethodInvoker ;
1818import 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