content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.tapir;
  
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
  
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import org.apache.pekko.http.scaladsl.server.RequestContext;
  import org.apache.pekko.http.scaladsl.server.RouteResult;
  import scala.Function1;
  import scala.concurrent.Future;
  import sttp.tapir.server.ServerEndpoint;
  
  public class TapirPathInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          named("toRoute").and(takesArgument(0, named("sttp.tapir.server.ServerEndpoint"))),
          this.getClass().getName() + "$ApplyAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class ApplyAdvice {
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void onExit(
          @Advice.Argument(0) ServerEndpoint<?, ?> endpoint,
          @Advice.Return(readOnly = false) Function1<RequestContext, Future<RouteResult>> route) {
        route = new RouteWrapper(endpoint, route);
      }
    }
  }
  