content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.hibernate.v3_3;
  
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
  import static io.opentelemetry.javaagent.instrumentation.hibernate.OperationNameUtil.getOperationNameForQuery;
  import static io.opentelemetry.javaagent.instrumentation.hibernate.v3_3.Hibernate3Singletons.instrumenter;
  import static net.bytebuddy.matcher.ElementMatchers.isMethod;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
  
  import io.opentelemetry.context.Context;
  import io.opentelemetry.instrumentation.api.util.VirtualField;
  import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperation;
  import io.opentelemetry.javaagent.instrumentation.hibernate.HibernateOperationScope;
  import io.opentelemetry.javaagent.instrumentation.hibernate.SessionInfo;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import org.hibernate.Query;
  
  public class QueryInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<ClassLoader> classLoaderOptimization() {
      return hasClassesNamed("org.hibernate.Query");
    }
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return implementsInterface(named("org.hibernate.Query"));
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isMethod().and(namedOneOf("list", "executeUpdate", "uniqueResult", "iterate", "scroll")),
          QueryInstrumentation.class.getName() + "$QueryMethodAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class QueryMethodAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static HibernateOperationScope startMethod(@Advice.This Query query) {
  
        if (HibernateOperationScope.enterDepthSkipCheck()) {
          return null;
        }
  
        VirtualField<Query, SessionInfo> queryVirtualField =
            VirtualField.find(Query.class, SessionInfo.class);
        SessionInfo sessionInfo = queryVirtualField.get(query);
  
        Context parentContext = Java8BytecodeBridge.currentContext();
        HibernateOperation hibernateOperation =
            new HibernateOperation(getOperationNameForQuery(query.getQueryString()), sessionInfo);
  
        return HibernateOperationScope.start(hibernateOperation, parentContext, instrumenter());
      }
  
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void endMethod(
          @Advice.Thrown Throwable throwable, @Advice.Enter HibernateOperationScope scope) {
  
        HibernateOperationScope.end(scope, throwable);
      }
    }
  }
  