Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.mybatis.v3_2;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.mybatis.v3_2.MyBatisSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.named;

Expand All @@ -14,6 +13,7 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand All @@ -35,36 +35,52 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class ExecuteAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void getMapperInfo(
@Advice.FieldValue("command") SqlCommand command,
@Advice.Local("otelRequest") ClassAndMethod request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (command == null) {
return;
public static class AdviceScope {
private final ClassAndMethod classAndMethod;
private final Context context;
private final Scope scope;

private AdviceScope(ClassAndMethod classAndMethod, Context context, Scope scope) {
this.classAndMethod = classAndMethod;
this.context = context;
this.scope = scope;
}
request = SqlCommandUtil.getClassAndMethod(command);
if (request == null) {
return;

@Nullable
public static AdviceScope start(@Nullable SqlCommand command) {
if (command == null) {
return null;
}
ClassAndMethod classAndMethod = SqlCommandUtil.getClassAndMethod(command);
if (classAndMethod == null) {
return null;
}
Context parentContext = Context.current();
if (!instrumenter().shouldStart(parentContext, classAndMethod)) {
return null;
}
Context context = instrumenter().start(parentContext, classAndMethod);
return new AdviceScope(classAndMethod, context, context.makeCurrent());
}
Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, request)) {
return;

public void end(@Nullable Throwable throwable) {
scope.close();
instrumenter().end(context, classAndMethod, null, throwable);
}
context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope getMapperInfo(@Advice.FieldValue("command") SqlCommand command) {
return AdviceScope.start(command);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") ClassAndMethod request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope != null) {
scope.close();
instrumenter().end(context, request, null, throwable);
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class MyBatisInstrumentationModule extends InstrumentationModule {
public class MyBatisInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public MyBatisInstrumentationModule() {
super("mybatis", "mybatis-3.2");
Expand All @@ -29,4 +31,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
public boolean defaultEnabled(ConfigProperties config) {
return false;
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading