|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.struts.v7_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource.CONTROLLER; |
| 9 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 10 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 11 | +import static io.opentelemetry.javaagent.instrumentation.struts.v7_0.StrutsSingletons.instrumenter; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 15 | + |
| 16 | +import io.opentelemetry.context.Context; |
| 17 | +import io.opentelemetry.context.Scope; |
| 18 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; |
| 19 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
| 20 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 21 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 22 | +import net.bytebuddy.asm.Advice; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.matcher.ElementMatcher; |
| 25 | +import org.apache.struts2.ActionInvocation; |
| 26 | + |
| 27 | +public class ActionInvocationInstrumentation implements TypeInstrumentation { |
| 28 | + |
| 29 | + @Override |
| 30 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 31 | + return hasClassesNamed("org.apache.struts2.ActionInvocation"); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 36 | + return implementsInterface(named("org.apache.struts2.ActionInvocation")); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void transform(TypeTransformer transformer) { |
| 41 | + transformer.applyAdviceToMethod( |
| 42 | + isMethod().and(isPublic()).and(named("invokeActionOnly")), |
| 43 | + this.getClass().getName() + "$InvokeActionOnlyAdvice"); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("unused") |
| 47 | + public static class InvokeActionOnlyAdvice { |
| 48 | + |
| 49 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 50 | + public static void onEnter( |
| 51 | + @Advice.This ActionInvocation actionInvocation, |
| 52 | + @Advice.Local("otelContext") Context context, |
| 53 | + @Advice.Local("otelScope") Scope scope) { |
| 54 | + Context parentContext = Java8BytecodeBridge.currentContext(); |
| 55 | + |
| 56 | + HttpServerRoute.update( |
| 57 | + parentContext, |
| 58 | + CONTROLLER, |
| 59 | + StrutsServerSpanNaming.SERVER_SPAN_NAME, |
| 60 | + actionInvocation.getProxy()); |
| 61 | + |
| 62 | + if (!instrumenter().shouldStart(parentContext, actionInvocation)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + context = instrumenter().start(parentContext, actionInvocation); |
| 67 | + scope = context.makeCurrent(); |
| 68 | + } |
| 69 | + |
| 70 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 71 | + public static void stopSpan( |
| 72 | + @Advice.Thrown Throwable throwable, |
| 73 | + @Advice.This ActionInvocation actionInvocation, |
| 74 | + @Advice.Local("otelContext") Context context, |
| 75 | + @Advice.Local("otelScope") Scope scope) { |
| 76 | + if (scope == null) { |
| 77 | + return; |
| 78 | + } |
| 79 | + scope.close(); |
| 80 | + |
| 81 | + instrumenter().end(context, actionInvocation, null, throwable); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments