|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.bootstrap.servlet; |
| 7 | + |
| 8 | +import static io.opentelemetry.context.ContextKey.named; |
| 9 | + |
| 10 | +import io.opentelemetry.context.Context; |
| 11 | +import io.opentelemetry.context.ContextKey; |
| 12 | +import io.opentelemetry.context.ImplicitContextKeyed; |
| 13 | +import javax.annotation.Nullable; |
| 14 | + |
| 15 | +public class ServletAsyncContext implements ImplicitContextKeyed { |
| 16 | + private static final ContextKey<ServletAsyncContext> CONTEXT_KEY = |
| 17 | + named("opentelemetry-servlet-async-context"); |
| 18 | + |
| 19 | + private boolean isAsyncListenerAttached; |
| 20 | + private Throwable throwable; |
| 21 | + private Object response; |
| 22 | + |
| 23 | + public static Context init(Context context) { |
| 24 | + if (context.get(CONTEXT_KEY) != null) { |
| 25 | + return context; |
| 26 | + } |
| 27 | + return context.with(new ServletAsyncContext()); |
| 28 | + } |
| 29 | + |
| 30 | + @Nullable |
| 31 | + public static ServletAsyncContext get(@Nullable Context context) { |
| 32 | + return context != null ? context.get(CONTEXT_KEY) : null; |
| 33 | + } |
| 34 | + |
| 35 | + public static boolean isAsyncListenerAttached(@Nullable Context context) { |
| 36 | + ServletAsyncContext servletAsyncContext = get(context); |
| 37 | + return servletAsyncContext != null && servletAsyncContext.isAsyncListenerAttached; |
| 38 | + } |
| 39 | + |
| 40 | + public static void setAsyncListenerAttached(@Nullable Context context, boolean value) { |
| 41 | + ServletAsyncContext servletAsyncContext = get(context); |
| 42 | + if (servletAsyncContext != null) { |
| 43 | + servletAsyncContext.isAsyncListenerAttached = value; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static Throwable getAsyncException(@Nullable Context context) { |
| 48 | + ServletAsyncContext servletAsyncContext = get(context); |
| 49 | + return servletAsyncContext != null ? servletAsyncContext.throwable : null; |
| 50 | + } |
| 51 | + |
| 52 | + public static void recordAsyncException(@Nullable Context context, Throwable throwable) { |
| 53 | + ServletAsyncContext servletAsyncContext = get(context); |
| 54 | + if (servletAsyncContext != null) { |
| 55 | + servletAsyncContext.throwable = throwable; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static Object getAsyncListenerResponse(@Nullable Context context) { |
| 60 | + ServletAsyncContext servletAsyncContext = get(context); |
| 61 | + return servletAsyncContext != null ? servletAsyncContext.response : null; |
| 62 | + } |
| 63 | + |
| 64 | + public static void setAsyncListenerResponse(@Nullable Context context, Object response) { |
| 65 | + ServletAsyncContext servletAsyncContext = get(context); |
| 66 | + if (servletAsyncContext != null) { |
| 67 | + servletAsyncContext.response = response; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Context storeInContext(Context context) { |
| 73 | + return context.with(CONTEXT_KEY, this); |
| 74 | + } |
| 75 | +} |
0 commit comments