Search in sources :

Example 1 with ResettableClassFileTransformer

use of net.bytebuddy.agent.builder.ResettableClassFileTransformer in project opentelemetry-java-instrumentation by open-telemetry.

the class AgentInstaller method installBytebuddyAgent.

/**
 * Install the core bytebuddy agent along with all implementations of {@link
 * InstrumentationModule}.
 *
 * @param inst Java Instrumentation used to install bytebuddy
 * @return the agent's class transformer
 */
public static ResettableClassFileTransformer installBytebuddyAgent(Instrumentation inst, Iterable<AgentListener> agentListeners) {
    WeakRefAsyncOperationEndStrategies.initialize();
    Config config = Config.get();
    setBootstrapPackages(config);
    // If noop OpenTelemetry is enabled, autoConfiguredSdk will be null and AgentListeners are not
    // called
    AutoConfiguredOpenTelemetrySdk autoConfiguredSdk = null;
    if (config.getBoolean(JAVAAGENT_NOOP_CONFIG, false)) {
        logger.info("Tracing and metrics are disabled because noop is enabled.");
        GlobalOpenTelemetry.set(NoopOpenTelemetry.getInstance());
    } else {
        autoConfiguredSdk = installOpenTelemetrySdk(config);
    }
    if (autoConfiguredSdk != null) {
        runBeforeAgentListeners(agentListeners, config, autoConfiguredSdk);
    }
    AgentBuilder agentBuilder = new AgentBuilder.Default().disableClassFormatChanges().with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION).with(new RedefinitionDiscoveryStrategy()).with(AgentBuilder.DescriptionStrategy.Default.POOL_ONLY).with(AgentTooling.poolStrategy()).with(new ClassLoadListener()).with(AgentTooling.locationStrategy(Utils.getBootstrapProxy()));
    if (JavaModule.isSupported()) {
        agentBuilder = agentBuilder.with(new ExposeAgentBootstrapListener(inst));
    }
    agentBuilder = configureIgnoredTypes(config, agentBuilder);
    if (logger.isDebugEnabled()) {
        agentBuilder = agentBuilder.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION).with(new RedefinitionDiscoveryStrategy()).with(new RedefinitionLoggingListener()).with(new TransformLoggingListener());
    }
    int numberOfLoadedExtensions = 0;
    for (AgentExtension agentExtension : loadOrdered(AgentExtension.class)) {
        logger.debug("Loading extension {} [class {}]", agentExtension.extensionName(), agentExtension.getClass().getName());
        try {
            agentBuilder = agentExtension.extend(agentBuilder);
            numberOfLoadedExtensions++;
        } catch (Exception | LinkageError e) {
            logger.error("Unable to load extension {} [class {}]", agentExtension.extensionName(), agentExtension.getClass().getName(), e);
        }
    }
    logger.debug("Installed {} extension(s)", numberOfLoadedExtensions);
    ResettableClassFileTransformer resettableClassFileTransformer = agentBuilder.installOn(inst);
    ClassFileTransformerHolder.setClassFileTransformer(resettableClassFileTransformer);
    if (autoConfiguredSdk != null) {
        runAfterAgentListeners(agentListeners, config, autoConfiguredSdk);
    }
    return resettableClassFileTransformer;
}
Also used : Config(io.opentelemetry.instrumentation.api.config.Config) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) AgentBuilder(net.bytebuddy.agent.builder.AgentBuilder) ResettableClassFileTransformer(net.bytebuddy.agent.builder.ResettableClassFileTransformer)

Example 2 with ResettableClassFileTransformer

use of net.bytebuddy.agent.builder.ResettableClassFileTransformer in project aeron by real-logic.

the class EventLogAgent method removeTransformer.

public static void removeTransformer() {
    if (logTransformer != null) {
        readerAgentRunner.close();
        instrumentation.removeTransformer(logTransformer);
        final ElementMatcher.Junction<TypeDescription> orClause = nameEndsWith("DriverConductor").or(nameEndsWith("ClientProxy")).or(nameEndsWith("ClientCommandAdapter")).or(nameEndsWith("SenderProxy")).or(nameEndsWith("ReceiverProxy")).or(inheritsAnnotation(EventLog.class));
        final ResettableClassFileTransformer transformer = new AgentBuilder.Default().type(orClause).transform(AgentBuilder.Transformer.NoOp.INSTANCE).installOn(instrumentation);
        instrumentation.removeTransformer(transformer);
        readerAgentRunner = null;
        instrumentation = null;
        logTransformer = null;
    }
}
Also used : ElementMatcher(net.bytebuddy.matcher.ElementMatcher) AgentBuilder(net.bytebuddy.agent.builder.AgentBuilder) ResettableClassFileTransformer(net.bytebuddy.agent.builder.ResettableClassFileTransformer) EventLog(io.aeron.driver.EventLog) TypeDescription(net.bytebuddy.description.type.TypeDescription)

Example 3 with ResettableClassFileTransformer

use of net.bytebuddy.agent.builder.ResettableClassFileTransformer in project apm-agent-java by elastic.

the class ElasticApmAgent method reset.

/**
 * Reverts instrumentation of classes and re-transforms them to their state without the agent.
 * <p>
 * NOTE: THIS IS ONLY TO BE USED FOR UNIT TESTS
 * NOTE2: THIS METHOD MUST BE CALLED AFTER AGENT WAS INITIALIZED
 * </p>
 */
public static synchronized void reset() {
    if (instrumentation == null) {
        return;
    }
    GlobalTracer.get().stop();
    GlobalTracer.setNoop();
    Exception exception = null;
    if (resettableClassFileTransformer != null) {
        try {
            resettableClassFileTransformer.reset(instrumentation, RedefinitionStrategy.RETRANSFORMATION);
        } catch (Exception e) {
            exception = e;
        }
        resettableClassFileTransformer = null;
    }
    dynamicallyInstrumentedClasses.clear();
    for (ResettableClassFileTransformer transformer : dynamicClassFileTransformers) {
        try {
            transformer.reset(instrumentation, RedefinitionStrategy.RETRANSFORMATION);
        } catch (Exception e) {
            if (exception != null) {
                exception.addSuppressed(e);
            } else {
                exception = e;
            }
        }
    }
    dynamicClassFileTransformers.clear();
    instrumentation = null;
    IndyPluginClassLoaderFactory.clear();
    adviceClassName2instrumentationClassLoader.clear();
    pluginPackages2pluginClassLoaderRootPackages.clear();
}
Also used : ResettableClassFileTransformer(net.bytebuddy.agent.builder.ResettableClassFileTransformer) IOException(java.io.IOException)

Aggregations

ResettableClassFileTransformer (net.bytebuddy.agent.builder.ResettableClassFileTransformer)3 AgentBuilder (net.bytebuddy.agent.builder.AgentBuilder)2 EventLog (io.aeron.driver.EventLog)1 Config (io.opentelemetry.instrumentation.api.config.Config)1 AutoConfiguredOpenTelemetrySdk (io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk)1 IOException (java.io.IOException)1 TypeDescription (net.bytebuddy.description.type.TypeDescription)1 ElementMatcher (net.bytebuddy.matcher.ElementMatcher)1