com.undefinedlabs.scope.rules.ConnectionScopeAgentRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scope-rule-java-sql Show documentation
Show all versions of scope-rule-java-sql Show documentation
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.
package com.undefinedlabs.scope.rules;
import com.undefinedlabs.scope.rules.sql.provider.ConnectionTrackingCheckerRegistry;
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.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class ConnectionScopeAgentRule extends AbstractScopeAgentRule {
public static final Logger LOGGER = ScopeLogger.INSTANCE;
@Override
protected String instrumentedClassName() {
return "java.sql.Connection";
}
@Override
protected Iterable extends AgentBuilder> 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(ConnectionAdvice.class, named("commit")))
.type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(ConnectionAdvice.class, named("rollback")))
.type(isSubTypeOf(instrumentedClass()), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(ConnectionAdvice.class, named("close")))
);
}
public static class ConnectionAdvice {
@Advice.OnMethodEnter
public static Span enter(@Advice.This Object thiz, @Advice.Origin("#m") Object methodName) {
final Connection connection = (Connection) thiz;
final boolean mustTrace = ConnectionTrackingCheckerRegistry.INSTANCE.getChecker(connection).mustTrace(connection);
if(!mustTrace){
return null;
}
final Tracer tracer = GlobalTracer.get();
try {
final DatabaseMetaData metaData = connection.getMetaData();
final String method = (String) methodName;
final Span span = tracer.buildSpan(metaData.getDatabaseProductName()+":"+method)
.withTag(TagKeys.SPAN_KIND, Tags.SPAN_KIND_CLIENT)
.withTag(TagKeys.DB.DB_CONNECTION, metaData.getURL())
.withTag(TagKeys.DB.DB_STATEMENT, method)
.withTag(TagKeys.DB.DB_PARAMS, "{}")
.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_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("ConnectionAdvice#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(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("ConnectionAdvice#exit - Finished Span: " + span);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy