|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.servlet.v2_2;
|
7 | 7 |
|
| 8 | +import static io.opentelemetry.javaagent.instrumentation.servlet.v2_2.Servlet2Singletons.RESPONSE_STATUS; |
8 | 9 | import static io.opentelemetry.javaagent.instrumentation.servlet.v2_2.Servlet2Singletons.helper;
|
9 | 10 |
|
10 | 11 | import io.opentelemetry.context.Context;
|
11 | 12 | import io.opentelemetry.context.Scope;
|
12 |
| -import io.opentelemetry.instrumentation.api.util.VirtualField; |
13 | 13 | import io.opentelemetry.javaagent.bootstrap.CallDepth;
|
14 |
| -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
15 | 14 | import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
|
16 | 15 | import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
|
17 | 16 | import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
|
| 17 | +import javax.annotation.Nullable; |
18 | 18 | import javax.servlet.ServletRequest;
|
19 | 19 | import javax.servlet.ServletResponse;
|
20 | 20 | import javax.servlet.http.HttpServletRequest;
|
|
25 | 25 | @SuppressWarnings("unused")
|
26 | 26 | public class Servlet2Advice {
|
27 | 27 |
|
28 |
| - @Advice.OnMethodEnter(suppress = Throwable.class) |
29 |
| - public static void onEnter( |
30 |
| - @Advice.Argument(0) ServletRequest request, |
31 |
| - @Advice.Argument(value = 1, typing = Assigner.Typing.DYNAMIC) ServletResponse response, |
32 |
| - @Advice.Local("otelCallDepth") CallDepth callDepth, |
33 |
| - @Advice.Local("otelRequest") ServletRequestContext<HttpServletRequest> requestContext, |
34 |
| - @Advice.Local("otelContext") Context context, |
35 |
| - @Advice.Local("otelScope") Scope scope) { |
| 28 | + public static class AdviceScope { |
| 29 | + |
| 30 | + private final CallDepth callDepth; |
| 31 | + private final ServletRequestContext<HttpServletRequest> requestContext; |
| 32 | + private final Context context; |
| 33 | + private final Scope scope; |
| 34 | + |
| 35 | + public AdviceScope( |
| 36 | + CallDepth callDepth, HttpServletRequest request, HttpServletResponse response) { |
| 37 | + this.callDepth = callDepth; |
| 38 | + callDepth.getAndIncrement(); |
| 39 | + |
| 40 | + Context serverContext = helper().getServerContext(request); |
| 41 | + if (serverContext != null) { |
| 42 | + Context updatedContext = helper().updateContext(serverContext, request); |
| 43 | + if (updatedContext != serverContext) { |
| 44 | + // updateContext updated context, need to re-scope |
| 45 | + scope = updatedContext.makeCurrent(); |
| 46 | + } else { |
| 47 | + scope = null; |
| 48 | + } |
| 49 | + requestContext = null; |
| 50 | + context = null; |
| 51 | + return; |
| 52 | + } |
36 | 53 |
|
37 |
| - if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { |
38 |
| - return; |
| 54 | + Context parentContext = Context.current(); |
| 55 | + requestContext = new ServletRequestContext<>(request); |
| 56 | + |
| 57 | + if (!helper().shouldStart(parentContext, requestContext)) { |
| 58 | + context = null; |
| 59 | + scope = null; |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + context = helper().start(parentContext, requestContext); |
| 64 | + scope = context.makeCurrent(); |
| 65 | + // reset response status from previous request |
| 66 | + // (some servlet containers reuse response objects to reduce memory allocations) |
| 67 | + RESPONSE_STATUS.set(response, null); |
| 68 | + |
| 69 | + HttpServerResponseCustomizerHolder.getCustomizer() |
| 70 | + .customize(context, response, Servlet2Accessor.INSTANCE); |
39 | 71 | }
|
40 | 72 |
|
41 |
| - HttpServletRequest httpServletRequest = (HttpServletRequest) request; |
| 73 | + public void exit( |
| 74 | + @Nullable Throwable throwable, HttpServletRequest request, HttpServletResponse response) { |
42 | 75 |
|
43 |
| - callDepth = CallDepth.forClass(AppServerBridge.getCallDepthKey()); |
44 |
| - callDepth.getAndIncrement(); |
| 76 | + if (scope != null) { |
| 77 | + scope.close(); |
| 78 | + } |
45 | 79 |
|
46 |
| - Context serverContext = helper().getServerContext(httpServletRequest); |
47 |
| - if (serverContext != null) { |
48 |
| - Context updatedContext = helper().updateContext(serverContext, httpServletRequest); |
49 |
| - if (updatedContext != serverContext) { |
50 |
| - // updateContext updated context, need to re-scope |
51 |
| - scope = updatedContext.makeCurrent(); |
| 80 | + boolean topLevel = callDepth.decrementAndGet() == 0; |
| 81 | + if (context == null && topLevel) { |
| 82 | + Context currentContext = Context.current(); |
| 83 | + // Something else is managing the context, we're in the outermost level of Servlet |
| 84 | + // instrumentation and we have an uncaught throwable. Let's add it to the current span. |
| 85 | + if (throwable != null) { |
| 86 | + helper().recordException(currentContext, throwable); |
| 87 | + } |
| 88 | + // also capture request parameters as servlet attributes |
| 89 | + helper().captureServletAttributes(currentContext, request); |
52 | 90 | }
|
53 |
| - return; |
54 |
| - } |
55 | 91 |
|
56 |
| - Context parentContext = Java8BytecodeBridge.currentContext(); |
57 |
| - requestContext = new ServletRequestContext<>(httpServletRequest); |
| 92 | + if (scope == null || context == null) { |
| 93 | + return; |
| 94 | + } |
58 | 95 |
|
59 |
| - if (!helper().shouldStart(parentContext, requestContext)) { |
60 |
| - return; |
61 |
| - } |
| 96 | + int responseStatusCode = HttpServletResponse.SC_OK; |
| 97 | + Integer responseStatus = RESPONSE_STATUS.get(response); |
| 98 | + if (responseStatus != null) { |
| 99 | + responseStatusCode = responseStatus; |
| 100 | + } |
62 | 101 |
|
63 |
| - context = helper().start(parentContext, requestContext); |
64 |
| - scope = context.makeCurrent(); |
65 |
| - // reset response status from previous request |
66 |
| - // (some servlet containers reuse response objects to reduce memory allocations) |
67 |
| - VirtualField.find(ServletResponse.class, Integer.class).set(response, null); |
| 102 | + helper().end(context, requestContext, response, responseStatusCode, throwable); |
| 103 | + } |
| 104 | + } |
68 | 105 |
|
69 |
| - HttpServerResponseCustomizerHolder.getCustomizer() |
70 |
| - .customize(context, (HttpServletResponse) response, Servlet2Accessor.INSTANCE); |
| 106 | + @Nullable |
| 107 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 108 | + public static AdviceScope onEnter( |
| 109 | + @Advice.Argument(0) ServletRequest request, |
| 110 | + @Advice.Argument(value = 1, typing = Assigner.Typing.DYNAMIC) ServletResponse response) { |
| 111 | + if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return new AdviceScope( |
| 115 | + CallDepth.forClass(AppServerBridge.getCallDepthKey()), |
| 116 | + (HttpServletRequest) request, |
| 117 | + (HttpServletResponse) response); |
71 | 118 | }
|
72 | 119 |
|
73 | 120 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
74 | 121 | public static void stopSpan(
|
75 | 122 | @Advice.Argument(0) ServletRequest request,
|
76 | 123 | @Advice.Argument(1) ServletResponse response,
|
77 |
| - @Advice.Thrown Throwable throwable, |
78 |
| - @Advice.Local("otelCallDepth") CallDepth callDepth, |
79 |
| - @Advice.Local("otelRequest") ServletRequestContext<HttpServletRequest> requestContext, |
80 |
| - @Advice.Local("otelContext") Context context, |
81 |
| - @Advice.Local("otelScope") Scope scope) { |
82 |
| - |
83 |
| - if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { |
| 124 | + @Advice.Thrown @Nullable Throwable throwable, |
| 125 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 126 | + if (adviceScope == null |
| 127 | + || !(request instanceof HttpServletRequest) |
| 128 | + || !(response instanceof HttpServletResponse)) { |
84 | 129 | return;
|
85 | 130 | }
|
86 |
| - |
87 |
| - if (scope != null) { |
88 |
| - scope.close(); |
89 |
| - } |
90 |
| - |
91 |
| - boolean topLevel = callDepth.decrementAndGet() == 0; |
92 |
| - if (context == null && topLevel) { |
93 |
| - Context currentContext = Java8BytecodeBridge.currentContext(); |
94 |
| - // Something else is managing the context, we're in the outermost level of Servlet |
95 |
| - // instrumentation and we have an uncaught throwable. Let's add it to the current span. |
96 |
| - if (throwable != null) { |
97 |
| - helper().recordException(currentContext, throwable); |
98 |
| - } |
99 |
| - // also capture request parameters as servlet attributes |
100 |
| - helper().captureServletAttributes(currentContext, (HttpServletRequest) request); |
101 |
| - } |
102 |
| - |
103 |
| - if (scope == null || context == null) { |
104 |
| - return; |
105 |
| - } |
106 |
| - |
107 |
| - int responseStatusCode = HttpServletResponse.SC_OK; |
108 |
| - Integer responseStatus = VirtualField.find(ServletResponse.class, Integer.class).get(response); |
109 |
| - if (responseStatus != null) { |
110 |
| - responseStatusCode = responseStatus; |
111 |
| - } |
112 |
| - |
113 |
| - helper() |
114 |
| - .end( |
115 |
| - context, requestContext, (HttpServletResponse) response, responseStatusCode, throwable); |
| 131 | + adviceScope.exit(throwable, (HttpServletRequest) request, (HttpServletResponse) response); |
116 | 132 | }
|
117 | 133 | }
|
0 commit comments