|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.awssdk.v2_2; |
| 7 | + |
| 8 | +import java.util.logging.Level; |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +final class PluginImplUtil { |
| 12 | + private PluginImplUtil() {} |
| 13 | + |
| 14 | + private static final Logger logger = Logger.getLogger(PluginImplUtil.class.getName()); |
| 15 | + |
| 16 | + /** |
| 17 | + * Check if the given {@code moduleNameImpl} is present. |
| 18 | + * |
| 19 | + * <p>For library instrumentations, the Impls will always be available but might fail to load if |
| 20 | + * the corresponding SDK classes are not on the class path. For javaagent, the Impl is available |
| 21 | + * only when the corresponding InstrumentationModule was successfully applied (muzzle passed). |
| 22 | + * |
| 23 | + * <p>Note that an present-but-incompatible library can only be detected by Muzzle. In |
| 24 | + * library-mode, users need to ensure they are using a compatible SDK (component) versions |
| 25 | + * themselves. |
| 26 | + * |
| 27 | + * @param implSimpleClassName The simple name of the impl class, e.g. {@code "SqsImpl"}. * |
| 28 | + */ |
| 29 | + static boolean isImplPresent(String implSimpleClassName) { |
| 30 | + // We use getName().replace() instead of getPackage() because the latter is not guaranteed to |
| 31 | + // work in all cases (e.g., we might be loaded into a custom classloader that doesn't handle it) |
| 32 | + String implFullClassName = |
| 33 | + PluginImplUtil.class.getName().replace(".PluginImplUtil", "." + implSimpleClassName); |
| 34 | + try { |
| 35 | + // using package name here because library instrumentation classes are relocated when embedded |
| 36 | + // in the agent |
| 37 | + Class.forName(implFullClassName); |
| 38 | + return true; |
| 39 | + } catch (ClassNotFoundException | LinkageError e) { |
| 40 | + // ClassNotFoundException will happen when muzzle disabled us in javaagent mode; LinkageError |
| 41 | + // (most likely NoClassDefFoundError) should happen when the class is loaded in library mode |
| 42 | + // (where it is always available) but a dependency failed to load (most likely because the |
| 43 | + // corresponding SDK dependency is not on the class path). |
| 44 | + logger.log( |
| 45 | + Level.FINE, e, () -> implFullClassName + " not present, probably incompatible version"); |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments