Class HelperInjector

java.lang.Object
io.opentelemetry.javaagent.tooling.HelperInjector
All Implemented Interfaces:
net.bytebuddy.agent.builder.AgentBuilder.Transformer

public class HelperInjector extends Object implements net.bytebuddy.agent.builder.AgentBuilder.Transformer
Injects instrumentation helper classes into the user's class loader.

Care must be taken when using this class. It is used both by the javaagent during its runtime and by gradle muzzle verification plugin during build time. And some code paths in this class require the usage of Instrumentation, which is available for the former, but not for the latter. Unfortunately, these two "modes of operations" and not easily discernible just by reading source code. Be careful.

In a nutshell, an instance of Instrumentation is needed for class injection into the bootstrap class loader. This should NOT happen during build-time muzzle verification phase.

  • Constructor Details

    • HelperInjector

      public HelperInjector(String requestingName, List<String> helperClassNames, List<HelperResource> helperResources, ClassLoader helpersSource, Instrumentation instrumentation)
      Construct HelperInjector.
      Parameters:
      helperClassNames - binary names of the helper classes to inject. These class names must be resolvable by the class loader returned by io.opentelemetry.javaagent.tooling.Utils#getAgentClassLoader(). Classes are injected in the order provided. This is important if there is interdependency between helper classes that requires them to be injected in a specific order. And be careful, the class's package in library will be renamed like 'io.opentelemetry.instrumentation' to 'io.opentelemetry.javaagent.shaded.instrumentation'
  • Method Details

    • forDynamicTypes

      public static HelperInjector forDynamicTypes(String requestingName, Collection<net.bytebuddy.dynamic.DynamicType.Unloaded<?>> helpers, Instrumentation instrumentation)
    • setHelperInjectorListener

      public static void setHelperInjectorListener(HelperInjectorListener listener)
    • transform

      public net.bytebuddy.dynamic.DynamicType.Builder<?> transform(net.bytebuddy.dynamic.DynamicType.Builder<?> builder, net.bytebuddy.description.type.TypeDescription typeDescription, ClassLoader classLoader, net.bytebuddy.utility.JavaModule javaModule, ProtectionDomain protectionDomain)
      Specified by:
      transform in interface net.bytebuddy.agent.builder.AgentBuilder.Transformer
    • isInjectedClass

      public static boolean isInjectedClass(Class<?> clazz)
    • isInjectedClass

      public static boolean isInjectedClass(ClassLoader classLoader, String className)
    • loadHelperClass

      public static Class<?> loadHelperClass(ClassLoader classLoader, String className)