Skip to content

Commit 24ff6eb

Browse files
committed
webmvc
1 parent 004a5cd commit 24ff6eb

File tree

6 files changed

+122
-62
lines changed

6 files changed

+122
-62
lines changed

instrumentation/spring/spring-webmvc/spring-webmvc-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v3_1/DispatcherServletInstrumentation.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2121
import java.util.List;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -76,29 +77,40 @@ public static void afterRefresh(
7677
@SuppressWarnings("unused")
7778
public static class RenderAdvice {
7879

80+
public static class AdviceScope {
81+
private final Context context;
82+
private final Scope scope;
83+
84+
public AdviceScope(Context context, Scope scope) {
85+
this.context = context;
86+
this.scope = scope;
87+
}
88+
89+
public void exit(ModelAndView mv, @Nullable Throwable throwable) {
90+
scope.close();
91+
modelAndViewInstrumenter().end(context, mv, null, throwable);
92+
}
93+
}
94+
95+
@Nullable
7996
@Advice.OnMethodEnter(suppress = Throwable.class)
80-
public static void onEnter(
81-
@Advice.Argument(0) ModelAndView mv,
82-
@Advice.Local("otelContext") Context context,
83-
@Advice.Local("otelScope") Scope scope) {
97+
public static AdviceScope onEnter(@Advice.Argument(0) ModelAndView mv) {
8498
Context parentContext = currentContext();
85-
if (modelAndViewInstrumenter().shouldStart(parentContext, mv)) {
86-
context = modelAndViewInstrumenter().start(parentContext, mv);
87-
scope = context.makeCurrent();
99+
if (!modelAndViewInstrumenter().shouldStart(parentContext, mv)) {
100+
return null;
88101
}
102+
Context context = modelAndViewInstrumenter().start(parentContext, mv);
103+
return new AdviceScope(context, context.makeCurrent());
89104
}
90105

91106
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
92107
public static void stopSpan(
93108
@Advice.Argument(0) ModelAndView mv,
94-
@Advice.Thrown Throwable throwable,
95-
@Advice.Local("otelContext") Context context,
96-
@Advice.Local("otelScope") Scope scope) {
97-
if (scope == null) {
98-
return;
109+
@Advice.Thrown @Nullable Throwable throwable,
110+
@Advice.Enter @Nullable AdviceScope adviceScope) {
111+
if (adviceScope != null) {
112+
adviceScope.exit(mv, throwable);
99113
}
100-
scope.close();
101-
modelAndViewInstrumenter().end(context, mv, null, throwable);
102114
}
103115
}
104116
}

instrumentation/spring/spring-webmvc/spring-webmvc-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v3_1/HandlerAdapterInstrumentation.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2424
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2525
import io.opentelemetry.javaagent.instrumentation.spring.webmvc.IsGrailsHandler;
26+
import javax.annotation.Nullable;
2627
import javax.servlet.http.HttpServletRequest;
2728
import net.bytebuddy.asm.Advice;
2829
import net.bytebuddy.description.type.TypeDescription;
@@ -54,49 +55,61 @@ public void transform(TypeTransformer transformer) {
5455
@SuppressWarnings("unused")
5556
public static class ControllerAdvice {
5657

58+
public static class AdviceScope {
59+
private final Context context;
60+
private final Scope scope;
61+
62+
public AdviceScope(Context context, Scope scope) {
63+
this.context = context;
64+
this.scope = scope;
65+
}
66+
67+
public void exit(Object handler, @Nullable Throwable throwable) {
68+
scope.close();
69+
handlerInstrumenter().end(context, handler, null, throwable);
70+
}
71+
}
72+
73+
@Nullable
5774
@Advice.OnMethodEnter(suppress = Throwable.class)
58-
public static void nameResourceAndStartSpan(
59-
@Advice.Argument(0) HttpServletRequest request,
60-
@Advice.Argument(2) Object handler,
61-
@Advice.Local("otelContext") Context context,
62-
@Advice.Local("otelScope") Scope scope) {
75+
public static AdviceScope nameResourceAndStartSpan(
76+
@Advice.Argument(0) HttpServletRequest request, @Advice.Argument(2) Object handler) {
77+
6378
// TODO (trask) should there be a way to customize Instrumenter.shouldStart()?
6479
if (IsGrailsHandler.isGrailsHandler(handler)) {
6580
// skip creating handler span for grails, grails instrumentation will take care of it
66-
return;
81+
return null;
6782
}
6883

6984
Context parentContext = Java8BytecodeBridge.currentContext();
7085

7186
// don't start a new top-level span
7287
if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
73-
return;
88+
return null;
7489
}
7590

7691
// Name the parent span based on the matching pattern
7792
HttpServerRoute.update(
7893
parentContext, CONTROLLER, SpringWebMvcServerSpanNaming.SERVER_SPAN_NAME, request);
7994

8095
if (!handlerInstrumenter().shouldStart(parentContext, handler)) {
81-
return;
96+
return null;
8297
}
8398

8499
// Now create a span for handler/controller execution.
85-
context = handlerInstrumenter().start(parentContext, handler);
86-
scope = context.makeCurrent();
100+
Context context = handlerInstrumenter().start(parentContext, handler);
101+
return new AdviceScope(context, context.makeCurrent());
87102
}
88103

89104
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
90105
public static void stopSpan(
91106
@Advice.Argument(2) Object handler,
92-
@Advice.Thrown Throwable throwable,
93-
@Advice.Local("otelContext") Context context,
94-
@Advice.Local("otelScope") Scope scope) {
95-
if (scope == null) {
96-
return;
107+
@Advice.Thrown @Nullable Throwable throwable,
108+
@Advice.Enter @Nullable AdviceScope adviceScope) {
109+
110+
if (adviceScope != null) {
111+
adviceScope.exit(handler, throwable);
97112
}
98-
scope.close();
99-
handlerInstrumenter().end(context, handler, null, throwable);
100113
}
101114
}
102115
}

instrumentation/spring/spring-webmvc/spring-webmvc-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v3_1/SpringWebMvcInstrumentationModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public String getModuleGroup() {
5353
public List<TypeInstrumentation> typeInstrumentations() {
5454
return asList(new DispatcherServletInstrumentation(), new HandlerAdapterInstrumentation());
5555
}
56+
57+
@Override
58+
public boolean isIndyReady() {
59+
return true;
60+
}
5661
}

instrumentation/spring/spring-webmvc/spring-webmvc-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v6_0/DispatcherServletInstrumentation.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2121
import java.util.List;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -77,29 +78,41 @@ public static void afterRefresh(
7778
@SuppressWarnings("unused")
7879
public static class RenderAdvice {
7980

81+
public static class AdviceScope {
82+
private final Context context;
83+
private final Scope scope;
84+
85+
public AdviceScope(Context context, Scope scope) {
86+
this.context = context;
87+
this.scope = scope;
88+
}
89+
90+
public void exit(ModelAndView mv, @Nullable Throwable throwable) {
91+
scope.close();
92+
modelAndViewInstrumenter().end(context, mv, null, throwable);
93+
}
94+
}
95+
96+
@Nullable
8097
@Advice.OnMethodEnter(suppress = Throwable.class)
81-
public static void onEnter(
82-
@Advice.Argument(0) ModelAndView mv,
83-
@Advice.Local("otelContext") Context context,
84-
@Advice.Local("otelScope") Scope scope) {
98+
public static AdviceScope onEnter(@Advice.Argument(0) ModelAndView mv) {
99+
85100
Context parentContext = currentContext();
86-
if (modelAndViewInstrumenter().shouldStart(parentContext, mv)) {
87-
context = modelAndViewInstrumenter().start(parentContext, mv);
88-
scope = context.makeCurrent();
101+
if (!modelAndViewInstrumenter().shouldStart(parentContext, mv)) {
102+
return null;
89103
}
104+
Context context = modelAndViewInstrumenter().start(parentContext, mv);
105+
return new AdviceScope(context, context.makeCurrent());
90106
}
91107

92108
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
93109
public static void stopSpan(
94110
@Advice.Argument(0) ModelAndView mv,
95-
@Advice.Thrown Throwable throwable,
96-
@Advice.Local("otelContext") Context context,
97-
@Advice.Local("otelScope") Scope scope) {
98-
if (scope == null) {
99-
return;
111+
@Advice.Thrown @Nullable Throwable throwable,
112+
@Advice.Enter @Nullable AdviceScope adviceScope) {
113+
if (adviceScope != null) {
114+
adviceScope.exit(mv, throwable);
100115
}
101-
scope.close();
102-
modelAndViewInstrumenter().end(context, mv, null, throwable);
103116
}
104117
}
105118
}

instrumentation/spring/spring-webmvc/spring-webmvc-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v6_0/HandlerAdapterInstrumentation.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2525
import io.opentelemetry.javaagent.instrumentation.spring.webmvc.IsGrailsHandler;
2626
import jakarta.servlet.http.HttpServletRequest;
27+
import javax.annotation.Nullable;
2728
import net.bytebuddy.asm.Advice;
2829
import net.bytebuddy.description.type.TypeDescription;
2930
import net.bytebuddy.matcher.ElementMatcher;
@@ -54,49 +55,60 @@ public void transform(TypeTransformer transformer) {
5455
@SuppressWarnings("unused")
5556
public static class ControllerAdvice {
5657

58+
public static class AdviceScope {
59+
private final Context context;
60+
private final Scope scope;
61+
62+
public AdviceScope(Context context, Scope scope) {
63+
this.context = context;
64+
this.scope = scope;
65+
}
66+
67+
public void exit(Object handler, @Nullable Throwable throwable) {
68+
scope.close();
69+
handlerInstrumenter().end(context, handler, null, throwable);
70+
}
71+
}
72+
73+
@Nullable
5774
@Advice.OnMethodEnter(suppress = Throwable.class)
58-
public static void nameResourceAndStartSpan(
59-
@Advice.Argument(0) HttpServletRequest request,
60-
@Advice.Argument(2) Object handler,
61-
@Advice.Local("otelContext") Context context,
62-
@Advice.Local("otelScope") Scope scope) {
75+
public static AdviceScope nameResourceAndStartSpan(
76+
@Advice.Argument(0) HttpServletRequest request, @Advice.Argument(2) Object handler) {
77+
6378
// TODO (trask) should there be a way to customize Instrumenter.shouldStart()?
6479
if (IsGrailsHandler.isGrailsHandler(handler)) {
6580
// skip creating handler span for grails, grails instrumentation will take care of it
66-
return;
81+
return null;
6782
}
6883

6984
Context parentContext = Java8BytecodeBridge.currentContext();
7085

7186
// don't start a new top-level span
7287
if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
73-
return;
88+
return null;
7489
}
7590

7691
// Name the parent span based on the matching pattern
7792
HttpServerRoute.update(
7893
parentContext, CONTROLLER, SpringWebMvcServerSpanNaming.SERVER_SPAN_NAME, request);
7994

8095
if (!handlerInstrumenter().shouldStart(parentContext, handler)) {
81-
return;
96+
return null;
8297
}
8398

8499
// Now create a span for handler/controller execution.
85-
context = handlerInstrumenter().start(parentContext, handler);
86-
scope = context.makeCurrent();
100+
Context context = handlerInstrumenter().start(parentContext, handler);
101+
return new AdviceScope(context, context.makeCurrent());
87102
}
88103

89104
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
90105
public static void stopSpan(
91106
@Advice.Argument(2) Object handler,
92-
@Advice.Thrown Throwable throwable,
93-
@Advice.Local("otelContext") Context context,
94-
@Advice.Local("otelScope") Scope scope) {
95-
if (scope == null) {
96-
return;
107+
@Advice.Thrown @Nullable Throwable throwable,
108+
@Advice.Enter @Nullable AdviceScope adviceScope) {
109+
if (adviceScope != null) {
110+
adviceScope.exit(handler, throwable);
97111
}
98-
scope.close();
99-
handlerInstrumenter().end(context, handler, null, throwable);
100112
}
101113
}
102114
}

instrumentation/spring/spring-webmvc/spring-webmvc-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/webmvc/v6_0/SpringWebMvcInstrumentationModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public String getModuleGroup() {
5353
public List<TypeInstrumentation> typeInstrumentations() {
5454
return asList(new DispatcherServletInstrumentation(), new HandlerAdapterInstrumentation());
5555
}
56+
57+
@Override
58+
public boolean isIndyReady() {
59+
return true;
60+
}
5661
}

0 commit comments

Comments
 (0)