content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.hibernate.reactive.v1_0.mutiny;
  
  import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.returns;
  
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import io.smallrye.mutiny.Uni;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  public class MutinySessionFactoryInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          nameStartsWith("open")
              .or(nameStartsWith("with"))
              .and(returns(named("io.smallrye.mutiny.Uni"))),
          this.getClass().getName() + "$ContextAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class ContextAdvice {
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void onExit(@Advice.Return(readOnly = false) Uni<?> uni) {
        uni = ContextOperator.plug(uni);
      }
    }
  }
  