content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.mongoasync.v3_3;
  
  import static net.bytebuddy.matcher.ElementMatchers.declaresField;
  import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
  import static net.bytebuddy.matcher.ElementMatchers.isMethod;
  import static net.bytebuddy.matcher.ElementMatchers.isPublic;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
  import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
  
  import com.mongodb.async.client.MongoClientSettings;
  import com.mongodb.event.CommandListener;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import java.util.List;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  final class MongoClientSettingsBuildersInstrumentation implements TypeInstrumentation {
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("com.mongodb.async.client.MongoClientSettings$Builder")
          .and(
              declaresMethod(
                  named("addCommandListener")
                      .and(isPublic())
                      .and(
                          takesArguments(1)
                              .and(takesArgument(0, named("com.mongodb.event.CommandListener"))))))
          .and(declaresField(named("commandListeners")));
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
          this.getClass().getName() + "$BuildAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class BuildAdvice {
  
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static void injectTraceListener(
          @Advice.This MongoClientSettings.Builder builder,
          @Advice.FieldValue("commandListeners") List<CommandListener> commandListeners) {
        for (CommandListener commandListener : commandListeners) {
          if (MongoInstrumentationSingletons.isTracingListener(commandListener)) {
            return;
          }
        }
        builder.addCommandListener(MongoInstrumentationSingletons.LISTENER);
      }
    }
  }
  