Skip to content

Commit a450d41

Browse files
committed
more deferred stuff
1 parent 92b0afc commit a450d41

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public static AdviceScope enter(HttpServletRequest request, Object handler) {
7373
}
7474

7575
Context parentContext = Context.current();
76+
// TODO: Handle async context propagation for DeferredResult
77+
// Context storedAsyncContext = WebAsyncManagerInstrumentation.getAsyncContext(request);
78+
// if (storedAsyncContext != null) {
79+
// parentContext = storedAsyncContext;
80+
// }
7681

7782
// don't start a new top-level span
7883
if (!Span.fromContext(parentContext).getSpanContext().isValid()) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public String getModuleGroup() {
5151

5252
@Override
5353
public List<TypeInstrumentation> typeInstrumentations() {
54-
return asList(new DispatcherServletInstrumentation(), new HandlerAdapterInstrumentation());
54+
return asList(
55+
new DispatcherServletInstrumentation(),
56+
new HandlerAdapterInstrumentation());
57+
// new WebAsyncManagerInstrumentation());
5558
}
5659

5760
@Override

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1515
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1616
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
17+
import jakarta.servlet.http.HttpServletRequest;
1718
import javax.annotation.Nullable;
1819
import net.bytebuddy.asm.Advice;
1920
import net.bytebuddy.description.type.TypeDescription;
@@ -50,19 +51,8 @@ public static void onEnter() {
5051
// This is the context that includes the async work (e.g., the async-call-child span)
5152
// and should be used when the handler is redispatched
5253
Context currentContext = Java8BytecodeBridge.currentContext();
53-
WebAsyncManagerContext.setViaRequestContextHolder(currentContext);
54-
}
55-
}
56-
57-
// Helper class to store and retrieve context for async processing
58-
public static class WebAsyncManagerContext {
59-
public static final String CONTEXT_ATTRIBUTE =
60-
"io.opentelemetry.javaagent.spring.webmvc.async.Context";
6154

62-
private WebAsyncManagerContext() {}
63-
64-
static void setViaRequestContextHolder(Context context) {
65-
// Use Spring's RequestContextHolder to access the current request and store the context
55+
// Store context in request attributes via Spring's RequestContextHolder
6656
try {
6757
Class<?> requestContextHolderClass =
6858
Class.forName("org.springframework.web.context.request.RequestContextHolder");
@@ -72,25 +62,30 @@ static void setViaRequestContextHolder(Context context) {
7262
requestAttributes
7363
.getClass()
7464
.getMethod("setAttribute", String.class, Object.class, int.class)
75-
.invoke(requestAttributes, CONTEXT_ATTRIBUTE, context, 0); // 0 = REQUEST_SCOPE
65+
.invoke(
66+
requestAttributes,
67+
"io.opentelemetry.javaagent.spring.webmvc.async.Context",
68+
currentContext,
69+
0); // 0 = REQUEST_SCOPE
7670
}
7771
} catch (Exception e) {
7872
// Ignore - best effort
7973
}
8074
}
75+
}
8176

82-
@Nullable
83-
public static Context getFromRequest(Object request) {
84-
try {
85-
return (Context)
86-
request
87-
.getClass()
88-
.getMethod("getAttribute", String.class)
89-
.invoke(request, CONTEXT_ATTRIBUTE);
90-
} catch (Exception e) {
91-
// Ignore - best effort
92-
return null;
93-
}
77+
// Helper method to retrieve the stored async context from the request
78+
@Nullable
79+
public static Context getAsyncContext(HttpServletRequest request) {
80+
try {
81+
return (Context)
82+
request
83+
.getClass()
84+
.getMethod("getAttribute", String.class)
85+
.invoke(request, "io.opentelemetry.javaagent.spring.webmvc.async.Context");
86+
} catch (Exception e) {
87+
// Ignore - best effort
88+
return null;
9489
}
9590
}
9691
}

0 commit comments

Comments
 (0)