content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.finaglehttp.v23_11;
  
  import static net.bytebuddy.matcher.ElementMatchers.isMethod;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  
  import io.opentelemetry.context.Context;
  import io.opentelemetry.context.Scope;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import scala.Option;
  
  public class ChannelTransportInstrumentation implements TypeInstrumentation {
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("com.twitter.finagle.netty4.transport.ChannelTransport");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isMethod().and(named("write")),
          ChannelTransportInstrumentation.class.getName() + "$WriteAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class WriteAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static Scope methodEnter() {
        Option<Context> ref = Helpers.CONTEXT_LOCAL.apply();
        if (ref.isDefined()) {
          return ref.get().makeCurrent();
        }
        return null;
      }
  
      @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
      public static void methodExit(@Advice.Enter Scope scope, @Advice.Thrown Throwable thrown) {
        if (scope != null) {
          scope.close();
        }
      }
    }
  }
  