|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.mybatis.v3_2; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.mybatis.v3_2.MyBatisSingletons.instrumenter; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 11 | + |
| 12 | +import io.opentelemetry.context.Context; |
| 13 | +import io.opentelemetry.context.Scope; |
| 14 | +import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 17 | +import net.bytebuddy.asm.Advice; |
| 18 | +import net.bytebuddy.description.type.TypeDescription; |
| 19 | +import net.bytebuddy.matcher.ElementMatcher; |
| 20 | +import org.apache.ibatis.binding.MapperMethod.SqlCommand; |
| 21 | + |
| 22 | +public class MapperMethodInstrumentation implements TypeInstrumentation { |
| 23 | + |
| 24 | + @Override |
| 25 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 26 | + return named("org.apache.ibatis.binding.MapperMethod"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void transform(TypeTransformer transformer) { |
| 31 | + transformer.applyAdviceToMethod( |
| 32 | + named("execute"), MapperMethodInstrumentation.class.getName() + "$ExecuteAdvice"); |
| 33 | + } |
| 34 | + |
| 35 | + @SuppressWarnings("unused") |
| 36 | + public static class ExecuteAdvice { |
| 37 | + |
| 38 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 39 | + public static void getMapperInfo( |
| 40 | + @Advice.FieldValue("command") SqlCommand command, |
| 41 | + @Advice.Local("otelRequest") ClassAndMethod request, |
| 42 | + @Advice.Local("otelContext") Context context, |
| 43 | + @Advice.Local("otelScope") Scope scope) { |
| 44 | + if (command == null) { |
| 45 | + return; |
| 46 | + } |
| 47 | + request = SqlCommandUtil.getClassAndMethod(command); |
| 48 | + if (request == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + Context parentContext = currentContext(); |
| 52 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + context = instrumenter().start(parentContext, request); |
| 56 | + scope = context.makeCurrent(); |
| 57 | + } |
| 58 | + |
| 59 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 60 | + public static void stopSpan( |
| 61 | + @Advice.Thrown Throwable throwable, |
| 62 | + @Advice.Local("otelRequest") ClassAndMethod request, |
| 63 | + @Advice.Local("otelContext") Context context, |
| 64 | + @Advice.Local("otelScope") Scope scope) { |
| 65 | + if (scope != null) { |
| 66 | + scope.close(); |
| 67 | + instrumenter().end(context, request, null, throwable); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments