All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.StatementScopeAgentRule Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the Java SQL package.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import com.undefinedlabs.scope.rules.sql.model.PreparedStatementQuery;
import com.undefinedlabs.scope.rules.sql.model.PreparedStatementQueryParameter;
import com.undefinedlabs.scope.rules.sql.provider.PreparedStatementQueryProviderRegistry;
import com.undefinedlabs.scope.rules.sql.provider.internal.PreparedStatementQueryUtils;
import com.undefinedlabs.scope.rules.transformer.ScopeAgentAdvicedTransformer;
import com.undefinedlabs.scope.tracing.events.EventFieldsFactory;
import com.undefinedlabs.scope.tracing.events.exception.ThrowableEvent;
import com.undefinedlabs.scope.tracing.logger.ScopeLogger;
import com.undefinedlabs.scope.tracing.utils.event.EventValues;
import com.undefinedlabs.scope.tracing.utils.sourcecode.ExceptionSourceCodeFactory;
import com.undefinedlabs.scope.tracing.utils.sourcecode.ExceptionSourceCodeFrame;
import com.undefinedlabs.scope.tracing.utils.tag.TagKeys;
import com.undefinedlabs.scope.tracing.utils.tag.TagValues;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import org.slf4j.Logger;

import java.sql.*;
import java.util.Collections;
import java.util.HashMap;

import static net.bytebuddy.matcher.ElementMatchers.*;

public class StatementScopeAgentRule extends AbstractScopeAgentRule {

    public static final Logger LOGGER = ScopeLogger.INSTANCE;

    @Override
    protected String instrumentedClassName() {
        return "java.sql.Statement";
    }

    @Override
    protected Iterable transformers() {
        return Collections.singleton(new AgentBuilder.Default()
            .ignore(none())
            .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
            .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
            .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
            .type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(StatementExecuteNativeAdvice.class, named("executeQuery").and(takesArgument(0, String.class))))
            .type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(StatementExecuteNativeAdvice.class, named("executeUpdate").and(takesArgument(0, String.class))))
            .type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(PreparedStatementExecuteAdvice.class, named("execute").and(takesArguments(0))))
            .type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(PreparedStatementExecuteAdvice.class, named("executeQuery").and(takesArguments(0))))
            .type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(PreparedStatementExecuteAdvice.class, named("executeUpdate").and(takesArguments(0))))
        );
    }

    public static class StatementExecuteNativeAdvice {

        @Advice.OnMethodEnter
        public static Span enter(@Advice.This Object thiz, @Advice.AllArguments Object[] args) {
            if(!(thiz instanceof Statement) && args.length == 0){
                return null;
            }

            final Tracer tracer = GlobalTracer.get();

            try {
                final String sql = (String) args[0];

                final Statement statement = (Statement) thiz;
                final Connection connection = statement.getConnection();
                final DatabaseMetaData metaData = connection.getMetaData();

                final Span span = tracer.buildSpan(metaData.getDatabaseProductName()+":"+PreparedStatementQueryUtils.INSTANCE.extractSqlMethod(sql))
                        .withTag(TagKeys.SPAN_KIND, Tags.SPAN_KIND_CLIENT)
                        .withTag(TagKeys.DB.DB_CONNECTION, metaData.getURL())
                        .withTag(TagKeys.DB.DB_TYPE, TagValues.DB.TYPE_SQL)
                        .withTag(TagKeys.DB.DB_USER, metaData.getUserName())
                        .withTag(TagKeys.DB.DB_INSTANCE, connection.getCatalog())
                        .withTag(TagKeys.DB.DB_STATEMENT, sql)
                        .withTag(TagKeys.DB.DB_PARAMS, PreparedStatementQueryUtils.INSTANCE.generateSqlParamsAsString(Collections.unmodifiableMap(new HashMap())))
                        .withTag(TagKeys.DB.DB_PRODUCT_NAME, metaData.getDatabaseProductName())
                        .withTag(TagKeys.DB.DB_PRODUCT_VERSION, metaData.getDatabaseProductVersion())
                        .withTag(TagKeys.DB.DB_DRIVER_NAME, metaData.getDriverName())
                        .withTag(TagKeys.DB.DB_DRIVER_VERSION, metaData.getDriverVersion())
                        .start();

                LOGGER.info("StatementExecuteNativeQueryAdvice#enter - Started Span: " + span);
                return span;

            } catch(Exception e){
                throw new RuntimeException(e);
            }


        }

        @Advice.OnMethodExit(onThrowable = SQLException.class)
        public static void exit(@Advice.This Object thiz, @Advice.Enter Object spanObj, @Advice.Thrown Throwable throwable) {
            if(!(thiz instanceof Statement) || spanObj == null){
                return;
            }

            final Span span = (Span) spanObj;

            if(throwable != null){
                span.setTag(TagKeys.ERROR, true);

                final ExceptionSourceCodeFrame exceptionSourceCodeFrame = ExceptionSourceCodeFactory.INSTANCE.createFrame(throwable);
                final ThrowableEvent.Builder throwableEventBuilder = ThrowableEvent.newBuilder();
                throwableEventBuilder
                        .withEventType(EventValues.General.ERROR)
                        .withThrowable(exceptionSourceCodeFrame.getUserThrowable())
                        .withSource(exceptionSourceCodeFrame.getSourceCodeFrame().getLinkPath());

                span.log(EventFieldsFactory.INSTANCE.createFields(throwableEventBuilder.build()));
            }

            span.finish();
            LOGGER.info("StatementExecuteNativeQueryAdvice#exit - Finished Span: " + span);
        }

    }


    public static class PreparedStatementExecuteAdvice {

        @Advice.OnMethodEnter
        public static Span enter(@Advice.This Object thiz) {
            if(!(thiz instanceof PreparedStatement)){
                return null;
            }

            final Tracer tracer = GlobalTracer.get();

            try {
                final PreparedStatement preparedStatement = (PreparedStatement) thiz;
                final Connection connection = preparedStatement.getConnection();
                final DatabaseMetaData metaData = connection.getMetaData();
                final PreparedStatementQuery preparedStatementQuery = PreparedStatementQueryProviderRegistry.INSTANCE.getProvider(preparedStatement).create(preparedStatement);

                if(preparedStatementQuery.equals(PreparedStatementQuery.EMPTY)){
                    return null;
                }

                final Span span = tracer.buildSpan(metaData.getDatabaseProductName()+":"+preparedStatementQuery.getSqlMethod())
                        .withTag(TagKeys.SPAN_KIND, Tags.SPAN_KIND_CLIENT)
                        .withTag(TagKeys.DB.DB_CONNECTION, metaData.getURL())
                        .withTag(TagKeys.DB.DB_TYPE, TagValues.DB.TYPE_SQL)
                        .withTag(TagKeys.DB.DB_USER, metaData.getUserName())
                        .withTag(TagKeys.DB.DB_PARAMS, PreparedStatementQueryUtils.INSTANCE.generateSqlParamsAsString(Collections.unmodifiableMap(preparedStatementQuery.getSqlParameterMap())))
                        .withTag(TagKeys.DB.DB_INSTANCE, connection.getCatalog())
                        .withTag(TagKeys.DB.DB_STATEMENT, preparedStatementQuery.getSqlStatement())
                        .withTag(TagKeys.DB.DB_PRODUCT_NAME, metaData.getDatabaseProductName())
                        .withTag(TagKeys.DB.DB_PRODUCT_VERSION, metaData.getDatabaseProductVersion())
                        .withTag(TagKeys.DB.DB_DRIVER_NAME, metaData.getDriverName())
                        .withTag(TagKeys.DB.DB_DRIVER_VERSION, metaData.getDriverVersion())
                        .start();

                LOGGER.info("PreparedStatementExecuteQueryAdvice#enter - Started Span: " + span);
                return span;

            } catch(Exception e){
                throw new RuntimeException(e);
            }

        }

        @Advice.OnMethodExit(onThrowable = SQLException.class)
        public static void exit(@Advice.This Object thiz, @Advice.Enter Object spanObj, @Advice.Thrown Throwable throwable) {
            if(!(thiz instanceof PreparedStatement) || spanObj == null){
                return;
            }

            final Span span = (Span) spanObj;

            if(throwable != null){
                span.setTag(TagKeys.ERROR, true);

                final ExceptionSourceCodeFrame exceptionSourceCodeFrame = ExceptionSourceCodeFactory.INSTANCE.createFrame(throwable);
                final ThrowableEvent.Builder throwableEventBuilder = ThrowableEvent.newBuilder();
                throwableEventBuilder
                        .withEventType(EventValues.General.ERROR)
                        .withThrowable(exceptionSourceCodeFrame.getUserThrowable())
                        .withSource(exceptionSourceCodeFrame.getSourceCodeFrame().getLinkPath());

                span.log(EventFieldsFactory.INSTANCE.createFields(throwableEventBuilder.build()));
            }

            span.finish();
            LOGGER.info("PreparedStatementExecuteQueryAdvice#exit - Finished Span: " + span);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy