123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /*
- * Copyright The OpenTelemetry Authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package io.opentelemetry.auto.tooling;
- import static io.opentelemetry.auto.tooling.ClassLoaderMatcher.BOOTSTRAP_CLASSLOADER;
- import static io.opentelemetry.instrumentation.auto.api.WeakMap.Provider.newWeakMap;
- import io.opentelemetry.instrumentation.auto.api.WeakMap;
- import java.io.File;
- import java.io.IOException;
- import java.lang.ref.WeakReference;
- import java.nio.file.Files;
- import java.security.SecureClassLoader;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.CopyOnWriteArrayList;
- import net.bytebuddy.agent.builder.AgentBuilder.Transformer;
- import net.bytebuddy.description.type.TypeDescription;
- import net.bytebuddy.dynamic.ClassFileLocator;
- import net.bytebuddy.dynamic.DynamicType;
- import net.bytebuddy.dynamic.loading.ClassInjector;
- import net.bytebuddy.utility.JavaModule;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /** Injects instrumentation helper classes into the user's classloader. */
- public class HelperInjector implements Transformer {
- private static final Logger log = LoggerFactory.getLogger(HelperInjector.class);
- // Need this because we can't put null into the injectedClassLoaders map.
- private static final ClassLoader BOOTSTRAP_CLASSLOADER_PLACEHOLDER =
- new SecureClassLoader(null) {
- @Override
- public String toString() {
- return "<bootstrap>";
- }
- };
- private final String requestingName;
- private final Set<String> helperClassNames;
- private final Map<String, byte[]> dynamicTypeMap = new LinkedHashMap<>();
- private final WeakMap<ClassLoader, Boolean> injectedClassLoaders = newWeakMap();
- private final List<WeakReference<Object>> helperModules = new CopyOnWriteArrayList<>();
- /**
- * Construct HelperInjector.
- *
- * @param helperClassNames binary names of the helper classes to inject. These class names must be
- * resolvable by the classloader returned by
- * io.opentelemetry.auto.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.
- */
- public HelperInjector(final String requestingName, final String... helperClassNames) {
- this.requestingName = requestingName;
- this.helperClassNames = new LinkedHashSet<>(Arrays.asList(helperClassNames));
- }
- public HelperInjector(final String requestingName, final Map<String, byte[]> helperMap) {
- this.requestingName = requestingName;
- helperClassNames = helperMap.keySet();
- dynamicTypeMap.putAll(helperMap);
- }
- public static HelperInjector forDynamicTypes(
- final String requestingName, final Collection<DynamicType.Unloaded<?>> helpers) {
- Map<String, byte[]> bytes = new HashMap<>(helpers.size());
- for (DynamicType.Unloaded<?> helper : helpers) {
- bytes.put(helper.getTypeDescription().getName(), helper.getBytes());
- }
- return new HelperInjector(requestingName, bytes);
- }
- private Map<String, byte[]> getHelperMap() throws IOException {
- if (dynamicTypeMap.isEmpty()) {
- Map<String, byte[]> classnameToBytes = new LinkedHashMap<>();
- ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(Utils.getAgentClassLoader());
- for (String helperClassName : helperClassNames) {
- byte[] classBytes = locator.locate(helperClassName).resolve();
- classnameToBytes.put(helperClassName, classBytes);
- }
- return classnameToBytes;
- } else {
- return dynamicTypeMap;
- }
- }
- @Override
- public DynamicType.Builder<?> transform(
- final DynamicType.Builder<?> builder,
- final TypeDescription typeDescription,
- ClassLoader classLoader,
- final JavaModule module) {
- if (!helperClassNames.isEmpty()) {
- if (classLoader == BOOTSTRAP_CLASSLOADER) {
- classLoader = BOOTSTRAP_CLASSLOADER_PLACEHOLDER;
- }
- if (!injectedClassLoaders.containsKey(classLoader)) {
- try {
- log.debug("Injecting classes onto classloader {} -> {}", classLoader, helperClassNames);
- Map<String, byte[]> classnameToBytes = getHelperMap();
- Map<String, Class<?>> classes;
- if (classLoader == BOOTSTRAP_CLASSLOADER_PLACEHOLDER) {
- classes = injectBootstrapClassLoader(classnameToBytes);
- } else {
- classes = injectClassLoader(classLoader, classnameToBytes);
- }
- // All agent helper classes are in the unnamed module
- // And there's exactly one unnamed module per classloader
- // Use the module of the first class for convenience
- if (JavaModule.isSupported()) {
- JavaModule javaModule = JavaModule.ofType(classes.values().iterator().next());
- helperModules.add(new WeakReference<>(javaModule.unwrap()));
- }
- } catch (final Exception e) {
- if (log.isErrorEnabled()) {
- log.error(
- "Error preparing helpers while processing {} for {}. Failed to inject helper classes into instance {}",
- typeDescription,
- requestingName,
- classLoader,
- e);
- }
- throw new RuntimeException(e);
- }
- injectedClassLoaders.put(classLoader, true);
- }
- ensureModuleCanReadHelperModules(module);
- }
- return builder;
- }
- private Map<String, Class<?>> injectBootstrapClassLoader(
- final Map<String, byte[]> classnameToBytes) throws IOException {
- // Mar 2020: Since we're proactively cleaning up tempDirs, we cannot share dirs per thread.
- // If this proves expensive, we could do a per-process tempDir with
- // a reference count -- but for now, starting simple.
- // Failures to create a tempDir are propagated as IOException and handled by transform
- File tempDir = createTempDir();
- try {
- return ClassInjector.UsingInstrumentation.of(
- tempDir,
- ClassInjector.UsingInstrumentation.Target.BOOTSTRAP,
- AgentInstaller.getInstrumentation())
- .injectRaw(classnameToBytes);
- } finally {
- // Delete fails silently
- deleteTempDir(tempDir);
- }
- }
- private Map<String, Class<?>> injectClassLoader(
- final ClassLoader classLoader, final Map<String, byte[]> classnameToBytes) {
- return new ClassInjector.UsingReflection(classLoader).injectRaw(classnameToBytes);
- }
- private void ensureModuleCanReadHelperModules(final JavaModule target) {
- if (JavaModule.isSupported() && target != JavaModule.UNSUPPORTED && target.isNamed()) {
- for (WeakReference<Object> helperModuleReference : helperModules) {
- Object realModule = helperModuleReference.get();
- if (realModule != null) {
- JavaModule helperModule = JavaModule.of(realModule);
- if (!target.canRead(helperModule)) {
- log.debug("Adding module read from {} to {}", target, helperModule);
- target.modify(
- AgentInstaller.getInstrumentation(),
- Collections.singleton(helperModule),
- Collections.<String, Set<JavaModule>>emptyMap(),
- Collections.<String, Set<JavaModule>>emptyMap(),
- Collections.<Class<?>>emptySet(),
- Collections.<Class<?>, List<Class<?>>>emptyMap());
- }
- }
- }
- }
- }
- private static File createTempDir() throws IOException {
- return Files.createTempDirectory("opentelemetry-temp-jars").toFile();
- }
- private static void deleteTempDir(final File file) {
- // Not using Files.delete for deleting the directory because failures
- // create Exceptions which may prove expensive. Instead using the
- // older File API which simply returns a boolean.
- boolean deleted = file.delete();
- if (!deleted) {
- file.deleteOnExit();
- }
- }
- }
|