content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.jdbc;
  
  import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
  import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;
  import static net.bytebuddy.matcher.ElementMatchers.isPublic;
  import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
  import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
  import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
  
  import io.opentelemetry.context.Context;
  import io.opentelemetry.context.Scope;
  import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
  import io.opentelemetry.instrumentation.jdbc.internal.JdbcData;
  import io.opentelemetry.javaagent.bootstrap.CallDepth;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import java.sql.PreparedStatement;
  import java.sql.Statement;
  import java.util.Map;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  public class StatementInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<ClassLoader> classLoaderOptimization() {
      return hasClassesNamed("java.sql.Statement");
    }
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return implementsInterface(named("java.sql.Statement"));
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          nameStartsWith("execute").and(takesArgument(0, String.class)).and(isPublic()),
          StatementInstrumentation.class.getName() + "$StatementAdvice");
      transformer.applyAdviceToMethod(
          named("addBatch").and(takesArgument(0, String.class)).and(isPublic()),
          StatementInstrumentation.class.getName() + "$AddBatchAdvice");
      transformer.applyAdviceToMethod(
          named("clearBatch").and(isPublic()),
          StatementInstrumentation.class.getName() + "$ClearBatchAdvice");
      transformer.applyAdviceToMethod(
          namedOneOf("executeBatch", "executeLargeBatch").and(takesNoArguments()).and(isPublic()),
          StatementInstrumentation.class.getName() + "$ExecuteBatchAdvice");
      transformer.applyAdviceToMethod(
          named("close").and(isPublic()).and(takesNoArguments()),
          StatementInstrumentation.class.getName() + "$CloseAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class StatementAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static void onEnter(
          @Advice.Argument(0) String sql,
          @Advice.This Statement statement,
          @Advice.Local("otelCallDepth") CallDepth callDepth,
          @Advice.Local("otelRequest") DbRequest request,
          @Advice.Local("otelContext") Context context,
          @Advice.Local("otelScope") Scope scope) {
        // Connection#getMetaData() may execute a Statement or PreparedStatement to retrieve DB info
        // this happens before the DB CLIENT span is started (and put in the current context), so this
        // instrumentation runs again and the shouldStartSpan() check always returns true - and so on
        // until we get a StackOverflowError
        // using CallDepth prevents this, because this check happens before Connection#getMetadata()
        // is called - the first recursive Statement call is just skipped and we do not create a span
        // for it
        callDepth = CallDepth.forClass(Statement.class);
        if (callDepth.getAndIncrement() > 0) {
          return;
        }
  
        Context parentContext = currentContext();
        request = DbRequest.create(statement, sql);
  
        if (request == null || !statementInstrumenter().shouldStart(parentContext, request)) {
          return;
        }
  
        context = statementInstrumenter().start(parentContext, request);
        scope = context.makeCurrent();
      }
  
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void stopSpan(
          @Advice.Thrown Throwable throwable,
          @Advice.Local("otelCallDepth") CallDepth callDepth,
          @Advice.Local("otelRequest") DbRequest request,
          @Advice.Local("otelContext") Context context,
          @Advice.Local("otelScope") Scope scope) {
        if (callDepth.decrementAndGet() > 0) {
          return;
        }
  
        if (scope != null) {
          scope.close();
          statementInstrumenter().end(context, request, null, throwable);
        }
      }
    }
  
    @SuppressWarnings("unused")
    public static class AddBatchAdvice {
  
      @Advice.OnMethodExit(suppress = Throwable.class)
      public static void addBatch(@Advice.This Statement statement, @Advice.Argument(0) String sql) {
        if (statement instanceof PreparedStatement) {
          return;
        }
        JdbcData.addStatementBatch(statement, sql);
      }
    }
  
    @SuppressWarnings("unused")
    public static class ClearBatchAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static void clearBatch(@Advice.This Statement statement) {
        JdbcData.clearBatch(statement);
      }
    }
  
    @SuppressWarnings("unused")
    public static class ExecuteBatchAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static void onEnter(
          @Advice.This Statement statement,
          @Advice.Local("otelCallDepth") CallDepth callDepth,
          @Advice.Local("otelRequest") DbRequest request,
          @Advice.Local("otelContext") Context context,
          @Advice.Local("otelScope") Scope scope) {
        // Connection#getMetaData() may execute a Statement or PreparedStatement to retrieve DB info
        // this happens before the DB CLIENT span is started (and put in the current context), so this
        // instrumentation runs again and the shouldStartSpan() check always returns true - and so on
        // until we get a StackOverflowError
        // using CallDepth prevents this, because this check happens before Connection#getMetadata()
        // is called - the first recursive Statement call is just skipped and we do not create a span
        // for it
        callDepth = CallDepth.forClass(Statement.class);
        if (callDepth.getAndIncrement() > 0) {
          return;
        }
  
        Context parentContext = currentContext();
        if (statement instanceof PreparedStatement) {
          String sql = JdbcData.preparedStatement.get((PreparedStatement) statement);
          if (sql == null) {
            return;
          }
          Long batchSize = JdbcData.getPreparedStatementBatchSize((PreparedStatement) statement);
          Map<String, String> parameters = JdbcData.getParameters((PreparedStatement) statement);
          request = DbRequest.create(statement, sql, batchSize, parameters);
        } else {
          JdbcData.StatementBatchInfo batchInfo = JdbcData.getStatementBatchInfo(statement);
          if (batchInfo == null) {
            request = DbRequest.create(statement, null);
          } else {
            request =
                DbRequest.create(statement, batchInfo.getStatements(), batchInfo.getBatchSize());
          }
        }
  
        if (request == null || !statementInstrumenter().shouldStart(parentContext, request)) {
          return;
        }
  
        context = statementInstrumenter().start(parentContext, request);
        scope = context.makeCurrent();
      }
  
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void stopSpan(
          @Advice.Thrown Throwable throwable,
          @Advice.Local("otelCallDepth") CallDepth callDepth,
          @Advice.Local("otelRequest") DbRequest request,
          @Advice.Local("otelContext") Context context,
          @Advice.Local("otelScope") Scope scope) {
        if (callDepth.decrementAndGet() > 0) {
          return;
        }
  
        if (scope != null) {
          scope.close();
          statementInstrumenter().end(context, request, null, throwable);
        }
      }
    }
  
    @SuppressWarnings("unused")
    public static class CloseAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static void closeStatement(@Advice.This Statement statement) {
        JdbcData.close(statement);
      }
    }
  }
  