content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.spring.aws;
  
  import static net.bytebuddy.matcher.ElementMatchers.named;
  
  import io.opentelemetry.context.Scope;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import java.util.Collection;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  import org.springframework.messaging.Message;
  
  public class AcknowledgementExecutionContextInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named(
          "io.awspring.cloud.sqs.listener.acknowledgement.BatchingAcknowledgementProcessor$AcknowledgementExecutionContext");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(named("execute"), this.getClass().getName() + "$ExecuteAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class ExecuteAdvice {
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static Scope methodEnter(@Advice.Argument(0) Collection<Message<?>> messages) {
        return SpringAwsUtil.handleBatch(messages);
      }
  
      @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
      public static void methodExit(@Advice.Enter Scope scope) {
        if (scope != null) {
          scope.close();
        }
      }
    }
  }
  