|
23 | 23 | import co.elastic.apm.agent.bci.HelperClassManager;
|
24 | 24 | import co.elastic.apm.agent.bci.VisibleForAdvice;
|
25 | 25 | import co.elastic.apm.agent.impl.ElasticApmTracer;
|
| 26 | +import co.elastic.apm.agent.impl.transaction.Transaction; |
26 | 27 | import net.bytebuddy.asm.Advice;
|
27 | 28 | import net.bytebuddy.description.NamedElement;
|
28 | 29 | import net.bytebuddy.description.method.MethodDescription;
|
|
52 | 53 | * {@code javax.servlet}, as these are inlined into the matching methods.
|
53 | 54 | * The agent itself does not have access to the Servlet API classes, as they are loaded by a child class loader.
|
54 | 55 | * See https://github.com/raphw/byte-buddy/issues/465 for more information.
|
55 |
| - * However, the helper class {@link StartAsyncAdviceHelper} has access to the Servlet API, |
| 56 | + * However, the helper class {@link AsyncContextAdviceHelper} has access to the Servlet API, |
56 | 57 | * as it is loaded by the child classloader of {@link AsyncContext}
|
57 |
| - * (see {@link StartAsyncAdvice#onExitStartAsync(HttpServletRequest, AsyncContext)}). |
| 58 | + * (see {@link StartAsyncInstrumentation.StartAsyncAdvice#onExitStartAsync(HttpServletRequest, AsyncContext)} |
| 59 | + * and {@link AsyncContextInstrumentation.AsyncContextStartAdvice#onEnterAsyncContextStart(Runnable)}). |
58 | 60 | */
|
59 |
| -public class AsyncInstrumentation extends ElasticApmInstrumentation { |
| 61 | +public abstract class AsyncInstrumentation extends ElasticApmInstrumentation { |
60 | 62 |
|
61 | 63 | private static final String SERVLET_API_ASYNC_GROUP_NAME = "servlet-api-async";
|
62 | 64 | @Nullable
|
63 | 65 | @VisibleForAdvice
|
64 | 66 | // referring to AsyncContext is legal because of type erasure
|
65 |
| - public static HelperClassManager<StartAsyncAdviceHelper<AsyncContext>> asyncHelper; |
| 67 | + public static HelperClassManager<AsyncContextAdviceHelper<AsyncContext>> asyncHelper; |
66 | 68 |
|
67 | 69 | @Override
|
68 | 70 | public void init(ElasticApmTracer tracer) {
|
69 | 71 | asyncHelper = HelperClassManager.ForSingleClassLoader.of(tracer,
|
70 |
| - "co.elastic.apm.agent.servlet.helper.StartAsyncAdviceHelperImpl", |
| 72 | + "co.elastic.apm.agent.servlet.helper.AsyncContextAdviceHelperImpl", |
71 | 73 | "co.elastic.apm.agent.servlet.helper.ApmAsyncListener");
|
72 | 74 | }
|
73 | 75 |
|
74 | 76 | @Override
|
75 |
| - public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() { |
76 |
| - return nameContains("Request"); |
| 77 | + public Collection<String> getInstrumentationGroupNames() { |
| 78 | + return Arrays.asList(ServletInstrumentation.SERVLET_API, SERVLET_API_ASYNC_GROUP_NAME); |
77 | 79 | }
|
78 | 80 |
|
79 |
| - @Override |
80 |
| - public ElementMatcher<? super TypeDescription> getTypeMatcher() { |
81 |
| - return not(isInterface()) |
82 |
| - .and(hasSuperType(named("javax.servlet.http.HttpServletRequest"))); |
| 81 | + public interface AsyncContextAdviceHelper<T> { |
| 82 | + void onExitStartAsync(T asyncContext); |
83 | 83 | }
|
84 | 84 |
|
85 |
| - /** |
86 |
| - * Matches |
87 |
| - * <ul> |
88 |
| - * <li>{@link HttpServletRequest#startAsync()}</li> |
89 |
| - * <li>{@link HttpServletRequest#startAsync(ServletRequest, ServletResponse)}</li> |
90 |
| - * </ul> |
91 |
| - * |
92 |
| - * @return |
93 |
| - */ |
94 |
| - @Override |
95 |
| - public ElementMatcher<? super MethodDescription> getMethodMatcher() { |
96 |
| - return named("startAsync") |
97 |
| - .and(returns(named("javax.servlet.AsyncContext"))) |
98 |
| - .and(takesArguments(0) |
99 |
| - .or( |
100 |
| - takesArgument(0, named("javax.servlet.ServletRequest")) |
101 |
| - .and(takesArgument(1, named("javax.servlet.ServletResponse"))) |
102 |
| - ) |
103 |
| - ) |
104 |
| - .and(isPublic()); |
105 |
| - } |
| 85 | + public static class StartAsyncInstrumentation extends AsyncInstrumentation { |
| 86 | + @Override |
| 87 | + public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() { |
| 88 | + return nameContains("Request"); |
| 89 | + } |
106 | 90 |
|
107 |
| - @Override |
108 |
| - public Collection<String> getInstrumentationGroupNames() { |
109 |
| - return Arrays.asList(ServletInstrumentation.SERVLET_API, SERVLET_API_ASYNC_GROUP_NAME); |
110 |
| - } |
| 91 | + @Override |
| 92 | + public ElementMatcher<? super TypeDescription> getTypeMatcher() { |
| 93 | + return not(isInterface()) |
| 94 | + .and(hasSuperType(named("javax.servlet.http.HttpServletRequest"))); |
| 95 | + } |
111 | 96 |
|
112 |
| - @Override |
113 |
| - public Class<?> getAdviceClass() { |
114 |
| - return StartAsyncAdvice.class; |
115 |
| - } |
| 97 | + /** |
| 98 | + * Matches |
| 99 | + * <ul> |
| 100 | + * <li>{@link HttpServletRequest#startAsync()}</li> |
| 101 | + * <li>{@link HttpServletRequest#startAsync(ServletRequest, ServletResponse)}</li> |
| 102 | + * </ul> |
| 103 | + * |
| 104 | + * @return |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public ElementMatcher<? super MethodDescription> getMethodMatcher() { |
| 108 | + return isPublic() |
| 109 | + .and(named("startAsync")) |
| 110 | + .and(returns(named("javax.servlet.AsyncContext"))) |
| 111 | + .and(takesArguments(0) |
| 112 | + .or( |
| 113 | + takesArgument(0, named("javax.servlet.ServletRequest")) |
| 114 | + .and(takesArgument(1, named("javax.servlet.ServletResponse"))) |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
116 | 118 |
|
117 |
| - public interface StartAsyncAdviceHelper<T> { |
118 |
| - void onExitStartAsync(T asyncContext); |
119 |
| - } |
| 119 | + @Override |
| 120 | + public Class<?> getAdviceClass() { |
| 121 | + return StartAsyncAdvice.class; |
| 122 | + } |
120 | 123 |
|
121 |
| - @VisibleForAdvice |
122 |
| - public static class StartAsyncAdvice { |
| 124 | + @VisibleForAdvice |
| 125 | + public static class StartAsyncAdvice { |
123 | 126 |
|
124 |
| - @Advice.OnMethodExit(suppress = Throwable.class) |
125 |
| - private static void onExitStartAsync(@Advice.This HttpServletRequest request, @Advice.Return AsyncContext asyncContext) { |
126 |
| - if (tracer != null) { |
127 |
| - if (asyncHelper != null) { |
128 |
| - asyncHelper.getForClassLoaderOfClass(AsyncContext.class).onExitStartAsync(asyncContext); |
| 127 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 128 | + private static void onExitStartAsync(@Advice.This HttpServletRequest request, @Advice.Return AsyncContext asyncContext) { |
| 129 | + if (tracer != null) { |
| 130 | + if (asyncHelper != null) { |
| 131 | + asyncHelper.getForClassLoaderOfClass(AsyncContext.class).onExitStartAsync(asyncContext); |
| 132 | + } |
129 | 133 | }
|
130 | 134 | }
|
131 | 135 | }
|
132 | 136 | }
|
133 | 137 |
|
| 138 | + public static class AsyncContextInstrumentation extends AsyncInstrumentation { |
| 139 | + @Override |
| 140 | + public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() { |
| 141 | + return nameContains("AsyncContext"); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public ElementMatcher<? super TypeDescription> getTypeMatcher() { |
| 146 | + return not(isInterface()) |
| 147 | + .and(hasSuperType(named("javax.servlet.AsyncContext"))); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public ElementMatcher<? super MethodDescription> getMethodMatcher() { |
| 152 | + return isPublic() |
| 153 | + .and(named("start")) |
| 154 | + .and(takesArguments(Runnable.class)); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Class<?> getAdviceClass() { |
| 159 | + return AsyncContextStartAdvice.class; |
| 160 | + } |
| 161 | + |
| 162 | + @VisibleForAdvice |
| 163 | + public static class AsyncContextStartAdvice { |
| 164 | + |
| 165 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 166 | + private static void onEnterAsyncContextStart(@Advice.Argument(value = 0, readOnly = false) @Nullable Runnable runnable) { |
| 167 | + if (tracer != null) { |
| 168 | + final Transaction transaction = tracer.currentTransaction(); |
| 169 | + if (transaction != null && runnable != null && asyncHelper != null) { |
| 170 | + runnable = tracer.wrapRunnable(runnable, transaction); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
134 | 176 | }
|
0 commit comments