content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7;
  
  import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
  import static java.util.Collections.singletonList;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  
  import com.google.auto.service.AutoService;
  import io.opentelemetry.javaagent.extension.instrumentation.HelperResourceBuilder;
  import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
  import io.opentelemetry.javaagent.extension.instrumentation.internal.injection.ClassInjector;
  import io.opentelemetry.javaagent.extension.instrumentation.internal.injection.InjectionMode;
  import java.util.List;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  @AutoService(InstrumentationModule.class)
  public class DubboInstrumentationModule extends InstrumentationModule
      implements ExperimentalInstrumentationModule {
    public DubboInstrumentationModule() {
      super("apache-dubbo", "apache-dubbo-2.7");
    }
  
    @Override
    public void registerHelperResources(HelperResourceBuilder helperResourceBuilder) {
      helperResourceBuilder.register(
          "META-INF/services/org.apache.dubbo.rpc.Filter",
          "apache-dubbo-2.7/META-INF/org.apache.dubbo.rpc.Filter");
    }
  
    @Override
    public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
      return hasClassesNamed("org.apache.dubbo.rpc.Filter");
    }
  
    @Override
    public void injectClasses(ClassInjector injector) {
      injector
          .proxyBuilder(
              "io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryClientFilter")
          .inject(InjectionMode.CLASS_ONLY);
      injector
          .proxyBuilder(
              "io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryServerFilter")
          .inject(InjectionMode.CLASS_ONLY);
    }
  
    @Override
    public List<TypeInstrumentation> typeInstrumentations() {
      return singletonList(new ResourceInjectingTypeInstrumentation());
    }
  
    // A type instrumentation is needed to trigger resource injection.
    public static class ResourceInjectingTypeInstrumentation implements TypeInstrumentation {
      @Override
      public ElementMatcher<TypeDescription> typeMatcher() {
        return named("org.apache.dubbo.common.extension.ExtensionLoader");
      }
  
      @Override
      public void transform(TypeTransformer transformer) {
        // Nothing to transform, this type instrumentation is only used for injecting resources.
      }
    }
  }
  