|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.grizzly; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 10 | + |
| 11 | +import io.opentelemetry.context.Scope; |
| 12 | +import io.opentelemetry.javaagent.bootstrap.CallDepth; |
| 13 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 16 | +import net.bytebuddy.asm.Advice; |
| 17 | +import net.bytebuddy.description.type.TypeDescription; |
| 18 | +import net.bytebuddy.matcher.ElementMatcher; |
| 19 | +import org.glassfish.grizzly.filterchain.FilterChainContext; |
| 20 | + |
| 21 | +public class FilterChainContextInstrumentation implements TypeInstrumentation { |
| 22 | + @Override |
| 23 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 24 | + return named("org.glassfish.grizzly.filterchain.FilterChainContext"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void transform(TypeTransformer transformer) { |
| 29 | + transformer.applyAdviceToMethod( |
| 30 | + named("resume").and(takesArguments(0)), |
| 31 | + FilterChainContextInstrumentation.class.getName() + "$ResumeAdvice"); |
| 32 | + transformer.applyAdviceToMethod( |
| 33 | + named("write"), FilterChainContextInstrumentation.class.getName() + "$WriteAdvice"); |
| 34 | + } |
| 35 | + |
| 36 | + @SuppressWarnings("unused") |
| 37 | + public static class ResumeAdvice { |
| 38 | + |
| 39 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 40 | + public static Scope onEnter() { |
| 41 | + return Java8BytecodeBridge.rootContext().makeCurrent(); |
| 42 | + } |
| 43 | + |
| 44 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 45 | + public static void onExit(@Advice.Enter Scope scope) { |
| 46 | + if (scope != null) { |
| 47 | + scope.close(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("unused") |
| 53 | + public static class WriteAdvice { |
| 54 | + |
| 55 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 56 | + public static void onEnter(@Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 57 | + callDepth = CallDepth.forClass(FilterChainContext.class); |
| 58 | + callDepth.getAndIncrement(); |
| 59 | + } |
| 60 | + |
| 61 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 62 | + public static void onExit( |
| 63 | + @Advice.This FilterChainContext filterChainContext, |
| 64 | + @Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 65 | + // When exiting the outermost call to write clear context & request from filter chain context. |
| 66 | + // Write makes a copy of the current filter chain context and passes it on. In older versions |
| 67 | + // new and old filter chain context share the attributes, but in newer versions the attributes |
| 68 | + // are also copied. We need to remove the attributes here to ensure that the next request |
| 69 | + // starts with clean state, failing to do so causes http pipelining test to fail with the |
| 70 | + // latest deps. |
| 71 | + if (callDepth.decrementAndGet() == 0) { |
| 72 | + GrizzlyStateStorage.removeContext(filterChainContext); |
| 73 | + GrizzlyStateStorage.removeRequest(filterChainContext); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments