content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.spring.webmvc.v6_0;
  
  import static io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource.CONTROLLER;
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
  import static io.opentelemetry.javaagent.instrumentation.spring.webmvc.v6_0.SpringWebMvcSingletons.handlerInstrumenter;
  import static net.bytebuddy.matcher.ElementMatchers.isMethod;
  import static net.bytebuddy.matcher.ElementMatchers.isPublic;
  import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
  import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
  
  import io.opentelemetry.api.trace.Span;
  import io.opentelemetry.context.Context;
  import io.opentelemetry.context.Scope;
  import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import io.opentelemetry.javaagent.instrumentation.spring.webmvc.IsGrailsHandler;
  import jakarta.servlet.http.HttpServletRequest;
  import javax.annotation.Nullable;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  public class HandlerAdapterInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<ClassLoader> classLoaderOptimization() {
      return hasClassesNamed("org.springframework.web.servlet.HandlerAdapter");
    }
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return implementsInterface(named("org.springframework.web.servlet.HandlerAdapter"));
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isMethod()
              .and(isPublic())
              .and(nameStartsWith("handle"))
              .and(takesArgument(0, named("jakarta.servlet.http.HttpServletRequest")))
              .and(takesArguments(3)),
          HandlerAdapterInstrumentation.class.getName() + "$ControllerAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class ControllerAdvice {
  
      public static class AdviceScope {
        private final Context context;
        private final Scope scope;
  
        private AdviceScope(Context context, Scope scope) {
          this.context = context;
          this.scope = scope;
        }
  
        @Nullable
        public static AdviceScope enter(HttpServletRequest request, Object handler) {
          // TODO (trask) should there be a way to customize Instrumenter.shouldStart()?
          if (IsGrailsHandler.isGrailsHandler(handler)) {
            // skip creating handler span for grails, grails instrumentation will take care of it
            return null;
          }
  
          Context parentContext = Context.current();
  
          // don't start a new top-level span
          if (!Span.fromContext(parentContext).getSpanContext().isValid()) {
            return null;
          }
  
          // Name the parent span based on the matching pattern
          HttpServerRoute.update(
              parentContext, CONTROLLER, SpringWebMvcServerSpanNaming.SERVER_SPAN_NAME, request);
  
          if (!handlerInstrumenter().shouldStart(parentContext, handler)) {
            return null;
          }
  
          // Now create a span for handler/controller execution.
          Context context = handlerInstrumenter().start(parentContext, handler);
          return new AdviceScope(context, context.makeCurrent());
        }
  
        public void exit(Object handler, @Nullable Throwable throwable) {
          scope.close();
          handlerInstrumenter().end(context, handler, null, throwable);
        }
      }
  
      @Nullable
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static AdviceScope nameResourceAndStartSpan(
          @Advice.Argument(0) HttpServletRequest request, @Advice.Argument(2) Object handler) {
        return AdviceScope.enter(request, handler);
      }
  
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void stopSpan(
          @Advice.Argument(2) Object handler,
          @Advice.Thrown @Nullable Throwable throwable,
          @Advice.Enter @Nullable AdviceScope adviceScope) {
        if (adviceScope != null) {
          adviceScope.exit(handler, throwable);
        }
      }
    }
  }
  